reidmorrison/rails_semantic_logger

Sidekiq jobs aren't Loggable with config.eager_load=true

jdelStrother opened this issue · 2 comments

Thanks for the recent sidekiq 7 support! I come bearing bugs:

Environment

  • Ruby Version: 3.2.4
  • Rails Version: 7.1.3.4
  • Semantic Logger Version: 4.15.0
  • Rails Semantic Logger Version: 4.16.0
  • Other Application/framework names and versions (e.g. Puma, etc.): Sidekiq 7.2.4
  • Rails configuration:
    config.eager_load = true

Expected Behavior

SemanticLogger::Loggable is consistently included into Sidekiq::Job

Actual Behavior

SemanticLogger::Loggable is included into Sidekiq::Job once RailsSemanticLogger's after_initialize hook has fired.
If Rails eager-loading is enabled, that means that all your sidekiq job classes get loaded (with the 'original' version of Sidekiq::Job), and then RailsSemanticLogger mixes in Loggable - which is too late.

I've added a sample repo here:

https://github.com/jdelStrother/semantic-sidekiq

It defines a SlowJob class which uses Sidekiq::Job.

You can try using that logger with something like this in console:

image

which behaves as-expected.

However, if you enable eager loading:

image

there's no SlowJob.logger class method. There's a logger instance method, but that's the regular Sidekiq::Job#logger method.

Possible fix?

Maybe RailsSemanticLogger's after_initialize hook should instead be initializer 'rails_semantic_logger', before: :eager_load! ? Or the sidekiq patches could possibly be moved to a separate earlier hook (before_initialize) ? I'm not super-familiar with the Railtie initialization process, so don't take any of those as gospel.

Worth giving Rails Semantic Logger v4.17.0 a try (release today), it moves the logger installation here:

::Sidekiq::Job.singleton_class.prepend(RailsSemanticLogger::Sidekiq::Loggable)

If not try pulling master and give your great options above a try to see if any resolve the class methods?

Looks good! I've only tested locally, but it seems to fix this eager load issue.