zormandi / logcraft

A zero-configuration structured logging solution for pure Ruby or Ruby on Rails projects.
MIT License
36 stars 2 forks source link

Conflicts with Sentry APM monitoring #6

Open lauer opened 9 months ago

lauer commented 9 months ago

So after some debug, I found out, that if you are using Sentry (https://docs.sentry.io/platforms/ruby/guides/rails/configuration/options/) together with this Gem, you will hit some strange logger error in the broadcast_logger.rb

[GEMSPATH]/gems/activesupport-7.1.2/lib/active_support/broadcast_logger.rb:102:in `block in debug': undefined method `debug' for #<Logging::Logger:0x0000000109c9f928 @name="Application", @parent=#<Logging::RootLogger:0x0000000108ffa4c0 @name="root", @appenders=[#<Logging::Appenders::Stdout:0x000000010b612130 @io=#<IO:<STDOUT>>, @close_method=:close, @buffer=[], @immediate=[], @auto_flushing=1, @async=false, @async_flusher=nil, @flush_period=nil, @name="stdout", @closed=false, @filters=[], @mutex=#<ReentrantMutex:0x0000000109d71f90 @locker=nil>, @layout=#<Logcraft::LogLayout:0x000000010ac03900 @global_context={}, @formatter=#<Proc:0x000000010ac4ab48 [GEMSPATH]/logcraft-2.2/lib/logcraft/log_layout.rb:7 (lambda)>, @level_formatter=#<Proc:0x000000010ac4aa58 [GEMSPATH]/gems/logcraft-2.2/lib/logcraft/log_layout.rb:8 (lambda)>>, @level=0, @encoding=#<Encoding:UTF-8>, @write_size=500>], @additive=false, @caller_tracing=false, @level=1>, @appenders=[], @additive=true, @level=1, @caller_tracing=false> (NoMethodError)

      dispatch { |logger| logger.debug(*args, &block) }

So the solution is in the Sentry configuration, to set the logger like config.logger = Sentry::Logger.new($stdout) And don't let is use the default Rails.logger which this gem overwrites.

This should maybe be added as a note some where in this documentation. Or maybe ensure that this does not happens.

zormandi commented 9 months ago

Thanks for reporting this, I'll take a look at it as soon as I have some time.

zormandi commented 9 months ago

Phew, this was an "interesting" one. It turns out that this issue is a confluence of two separate problems - neither of which is related to Logcraft. One is that sentry-rails - for some reason - duplicates the Rails logger stack (https://github.com/getsentry/sentry-ruby/blob/master/sentry-rails/lib/sentry/rails/configuration.rb#L15). The other one is that the logging gem (which Logcraft is based on) relies on metaprogramming to define its logging methods, hence it cannot be duplicated.

The problem should really be addressed in either of these gems but for now I pushed a workaround into Logcraft.

zormandi commented 9 months ago

New version is released with the fix.