reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
328 stars 116 forks source link

uninitialized constant RailsSemanticLogger::Sidekiq (NameError) in upgrade 4.17.0 #233

Open hatsu38 opened 2 months ago

hatsu38 commented 2 months ago

Environment

Provide at least:

config/initialize/logger.rb

require "rails_semantic_logger"

Rails.application.config.after_initialize do
  Rails.application.config.rails_semantic_logger.add_file_appender = false
  Rails.application.config.semantic_logger.add_appender(io: $stdout, formatter: :json)
  Rails.application.config.rails_semantic_logger.started    = true
  Rails.application.config.rails_semantic_logger.processing = true
  Rails.application.config.rails_semantic_logger.rendered   = true
end
Error Trace ```ruby ber s => Booting Puma => Rails 7.1.3.4 application starting in development => Run `bin/rails server --help` for more startup options Exiting /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:133:in `block in ': uninitialized constant RailsSemanticLogger::Sidekiq (NameError) ::Sidekiq::Job.singleton_class.prepend(RailsSemanticLogger::Sidekiq::Loggable) ^^^^^^^^^ from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:94:in `block in execute_hook' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:87:in `with_execution_control' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:92:in `execute_hook' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:62:in `block in on_load' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:61:in `each' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/lazy_load_hooks.rb:61:in `on_load' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/railties-7.1.3.4/lib/rails/railtie/configuration.rb:66:in `before_initialize' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:94:in `' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:5:in `' from /Users/hatsu/development/works/sheinc/she_webapp/vendor/bundle/ruby/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:4:in `' ```

Note:

Expected Behavior

I expected that when I include the rails_semantic_logger gem with require: false in my Gemfile and then manually require it in the initializer, the logging would work without errors.

Here's the relevant code from my Gemfile:

gem "rails_semantic_logger", require: false

config/initialize/logger.rb

require "rails_semantic_logger"

Rails.application.config.after_initialize do
  Rails.application.config.rails_semantic_logger.add_file_appender = false
  Rails.application.config.semantic_logger.add_appender(io: $stdout, formatter: :json)
  Rails.application.config.rails_semantic_logger.started    = true
  Rails.application.config.rails_semantic_logger.processing = true
  Rails.application.config.rails_semantic_logger.rendered   = true
end
bin/rails s
=> Puma starting in single mode...
* Puma version: 6.4.2 (ruby 3.3.4-p94) ("The Eagle of Durango")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 48496
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish

Actual Behavior

When I run the application with rails_semantic_logger included with require: false, I encounter the following error:

uninitialized constant RailsSemanticLogger::Sidekiq (NameError)

The error seems to indicate that the RailsSemanticLogger::Sidekiq constant is not initialized when the gem is required manually in the initializer.

However, if I remove require: false from the Gemfile, the error does not occur, and the logger works as expected.

Pull Request

reidmorrison commented 1 month ago

Can you post the full stack trace, or at least the parts showing the Semantic Logger portions so that we can see where the error is coming from? Or, if you track it down, would be happy to accept a pull request.

hatsu38 commented 1 month ago

@reidmorrison Thank you. I will send this trace. Let me know if you need any other information.

bin/rails s
=> Booting Puma
=> Rails 7.1.4 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:133:in `block in <class:Engine>': uninitialized constant RailsSemanticLogger::Sidekiq (NameError)

          ::Sidekiq::Job.singleton_class.prepend(RailsSemanticLogger::Sidekiq::Loggable)
                                                                    ^^^^^^^^^
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:94:in `block in execute_hook'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:87:in `with_execution_control'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:92:in `execute_hook'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:62:in `block in on_load'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:61:in `each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/lazy_load_hooks.rb:61:in `on_load'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/railtie/configuration.rb:66:in `before_initialize'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:94:in `<class:Engine>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:5:in `<module:RailsSemanticLogger>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/engine.rb:4:in `<main>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.18/lib/zeitwerk/kernel.rb:34:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger.rb:3:in `<main>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.18/lib/zeitwerk/kernel.rb:34:in `require'
        from /Users/hatsu/development/practice/rails-7.1/config/initializers/semantic_logger.rb:1:in `<main>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:690:in `load'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:690:in `block in load_config_initializer'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4/lib/active_support/notifications.rb:208:in `instrument'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:689:in `load_config_initializer'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:643:in `block (2 levels) in <class:Engine>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:642:in `each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/engine.rb:642:in `block in <class:Engine>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:32:in `instance_exec'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:32:in `run'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:61:in `block in run_initializers'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:231:in `block in tsort_each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:353:in `block (2 levels) in each_strongly_connected_component'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:425:in `block (2 levels) in each_strongly_connected_component_from'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:434:in `each_strongly_connected_component_from'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:424:in `block in each_strongly_connected_component_from'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:50:in `each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:50:in `tsort_each_child'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:418:in `call'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:418:in `each_strongly_connected_component_from'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:352:in `block in each_strongly_connected_component'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:350:in `each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:350:in `call'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:350:in `each_strongly_connected_component'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:229:in `tsort_each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/tsort.rb:208:in `tsort_each'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/initializable.rb:60:in `run_initializers'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/application.rb:426:in `initialize!'
        from /Users/hatsu/development/practice/rails-7.1/config/environment.rb:5:in `<main>'
        from config.ru:3:in `require_relative'
        from config.ru:3:in `block in <main>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/builder.rb:116:in `eval'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/builder.rb:116:in `new_from_string'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/builder.rb:105:in `load_file'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/builder.rb:66:in `parse_file'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/server.rb:349:in `build_app_and_options_from_config'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/server.rb:249:in `app'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/rack-2.2.9/lib/rack/server.rb:422:in `wrapped_app'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/commands/server/server_command.rb:76:in `log_to_stdout'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/commands/server/server_command.rb:36:in `start'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/commands/server/server_command.rb:145:in `block in perform'
        from <internal:kernel>:90:in `tap'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/commands/server/server_command.rb:136:in `perform'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/command/base.rb:178:in `invoke_command'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor.rb:538:in `dispatch'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/command/base.rb:73:in `perform'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/command.rb:71:in `block in invoke'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/command.rb:149:in `with_argv'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/command.rb:69:in `invoke'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.1.4/lib/rails/commands.rb:18:in `<main>'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/hatsu/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from bin/rails:4:in `<main>'