elastic / elastic/ecs-logging-ruby
EcsLogging::Logger raises ArgumentError when used with ActiveSupport::TaggedLogging
- Dominant language
- Ruby
- Stars
- 8
- Forks
- 10
- PR merge metrics
- No merged PRs in 30d
Description
## Description
When using `EcsLogging::Logger` wrapped with `ActiveSupport::TaggedLogging`, an `ArgumentError` is raised because `EcsLogging::Logger` doesn't support the same method signatures as standard Ruby `Logger`.
## Spec to Reproduce
```ruby
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'ecs-logging'
gem 'activesupport', '>= 7.0'
gem 'elastic-apm'
gem 'rspec'
end
require 'active_support'
require 'active_support/isolated_execution_state'
require 'active_support/tagged_logging'
require 'ecs_logging/logger'
require 'elastic_apm'
require 'rspec/autorun'
RSpec.describe EcsLogging::Logger do
describe 'with ElasticAPM custom context' do
let(:output) { StringIO.new }
let(:ecs_logger) { EcsLogging::Logger.new(output) }
let(:tagged_logger) { ActiveSupport::TaggedLogging.new(ecs_logger) }
before do
allow(ElasticAPM).to receive(:running?).and_return(true)
allow(ElasticAPM).to receive(:set_custom_context)
end
it 'works with ElasticAPM custom context' do
expect {
ElasticAPM.set_custom_context(key: 'value', event: 'test_event')
tagged_logger.tagged('MyTag') do
tagged_logger.info('test message')
end
}.not_to raise_error
log_entry = JSON.parse(output.string)
expect(log_entry['message']).to include('[MyTag] test message')
end
end
end
```
Details
```bash
❯ ruby ecs_logging_reproduction.rb
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
WARN: Unresolved or ambiguous specs during Gem::Specification.reset:
psych (>= 4.0.0)
Available/installed versions of this gem:
- 5.3.1
- 5.1.2
erb (>= 0)
Available/installed versions of this gem:
- 6.0.2
- 4.0.3
WARN: Clearing out unresolved specs. Try 'gem cleanup '
Please report a bug if this causes problems.
.F
Failures:
1) EcsLogging::Logger ElasticAPM custom context compatibility works with ElasticAPM custom context
Failure/Error:
expect {
ElasticAPM.set_custom_context(key: 'value', event: 'test_event')
tagged_logger.tagged('MyTag') do
tagged_logger.info('test message')
end
}.not_to raise_error
expected no Exception, got # with backtrace:
# ecs_logging_reproduction.rb:50:in `block (5 levels) in '
# ecs_logging_reproduction.rb:49:in `block (4 levels) in '
# ecs_logging_reproduction.rb:47:in `block (3 levels) in '
# ecs_logging_reproduction.rb:47:in `block (3 levels) in '
Finished in 0.01716 seconds (files took 0.10125 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ecs_logging_reproduction.rb:46 # EcsLogging::Logger ElasticAPM custom context compatibility works with ElasticAPM custom context
```
## Expected Behavior
The logger should output a JSON log entry with the tag included, similar to how it works with standard `Logger`.
## Actual Behavior
An `ArgumentError` is raised because `ActiveSupport::TaggedLogging` calls methods on the underlying logger that `EcsLogging::Logger` doesn't implement with compatible signatures.
## Environment
- Ruby version: 3.2+
- Rails version: 7.x / 8.x
- ecs-logging-ruby version: latest
## Workaround
We worked around this by not wrapping the ECS logger with `TaggedLogging` and instead including the tag directly in the message:
```ruby
def logger
@logger ||= Rails.logger
end
def log(level, event, **attributes)
message = "[#{log_tag}] event=#{event} #{format_attributes(attributes)}"
logger.public_send(level, message)
end
```
## Suggestion
It would be helpful if `EcsLogging::Logger` could be compatible with `ActiveSupport::TaggedLogging`, or if the gem provided its own tagging mechanism that works with the ECS JSON format.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.