Skip to content

EcsLogging::Logger raises ArgumentError when used with ActiveSupport::TaggedLogging #79

Description

@lekemula

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

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 <gem>'
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 #<ArgumentError: wrong number of arguments (given 5, expected 4)> with backtrace:
         # ecs_logging_reproduction.rb:50:in `block (5 levels) in <main>'
         # ecs_logging_reproduction.rb:49:in `block (4 levels) in <main>'
         # ecs_logging_reproduction.rb:47:in `block (3 levels) in <main>'
     # ecs_logging_reproduction.rb:47:in `block (3 levels) in <main>'

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions