reidmorrison / semantic_logger

Semantic Logger is a feature rich logging framework, and replacement for existing Ruby & Rails loggers.
https://logger.rocketjob.io/
Apache License 2.0
873 stars 124 forks source link

Semantic Logger + Sidekiq 7 #243

Closed Petercopter closed 2 years ago

Petercopter commented 2 years ago

Looks like there's a problem with the new Sidekiq 7 logging:

Environment

Ruby 3.1.2 Rails 7.0.4 Puma 5.6.5 rspec-rails 6.0.1 Sidekiq 7.0

config/initializers/semantic_logger.rb:

SemanticLogger.add_appender(appender: :new_relic)

Expected Behavior

Test suite should run without errors

Actual Behavior

❯ bundle exec rspec

An error occurred while loading ./spec/channels/customers/notifications_channel_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `logger=' for Sidekiq:Module

        Sidekiq.logger       = SemanticLogger[Sidekiq] if defined?(Sidekiq)
               ^^^^^^^^^^^^^^^
  Did you mean?  logger
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/channels/customers/notifications_channel_spec.rb:1:in `require'
# ./spec/channels/customers/notifications_channel_spec.rb:1:in `<top (required)>'

Edit: Mike Perham has commented on the linked issue about what is needed to support Sidekiq 7.

reidmorrison commented 2 years ago

Since you are using Sidekiq 7, can you find out how to replace its default logger if it is no longer Sidekiq.logger=?

reidmorrison commented 2 years ago

Until we have more info on how to support Sidekiq 7, this commit will turn off built-in logging support for Sidekiq v7, allowing it to be setup manually: https://github.com/reidmorrison/rails_semantic_logger/commit/a64c48009f837ca875a451206e647ccba94cd0dd

To hazard a guess after pointing to the Rails Semantic Logger master branch, then something like the following would work:

Sidekiq.configure_{client,server,embed} do |config|
  config.logger = SemanticLogger[Sidekiq]
end
Petercopter commented 2 years ago

Confirmed 👍 . Switching to that commit for rails_semantic_logger and adding

Sidekiq.configure_client do |config|
  config.logger = SemanticLogger[Sidekiq]
end

Sidekiq.configure_server do |config|
  config.logger = SemanticLogger[Sidekiq]
end

to config/initializers/sidekiq.rb did the trick.