Skip to content

undefined method `to_h' for an instance of String on logging eval (Rails 7.2.2) #250

@aleandro-mifsud-axini

Description

@aleandro-mifsud-axini

Hi,

My team and I are currently migrating from Rails 7.1 to 7.2

After upgrading a rails app to Rails 7.2.2.2 and running the server locally the following error is raised:

[2025-09-19T13:41:35] ERROR X : <NoMethodError> undefined method `to_h' for an instance of String
        (eval at * logging-2.4.0/lib/logging.rb:198):1:in `logger'
        app/controllers/application_controller.rb:215:in `process_action'
[2025-09-19T13:41:35] ERROR Object : <NoMethodError> undefined method `to_h' for an instance of String
        (eval at * logging-2.4.0/lib/logging.rb:198):1:in `logger'
        app/controllers/concerns/http_error_handler.rb:50:in `internal_server_error'
        app/controllers/application_controller.rb:221:in `rescue in process_action'
        app/controllers/application_controller.rb:214:in `process_action'
        --- Caused by ---
        <NoMethodError> undefined method `to_h' for an instance of String
        (eval at * logging-2.4.0/lib/logging.rb:198):1:in `logger'
        app/controllers/application_controller.rb:215:in `process_action'
Full backtrace


["/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/ordered_options.rb:108:in `=='",
 "/home/.rvm/gems/ruby-3.3.6/gems/logging-2.4.0/lib/logging/repository.rb:169:in `==='",
 "/home/.rvm/gems/ruby-3.3.6/gems/logging-2.4.0/lib/logging/repository.rb:169:in `to_key'",
 "/home/.rvm/gems/ruby-3.3.6/gems/logging-2.4.0/lib/logging/logger.rb:45:in `[]'",
 "(eval at /home/.rvm/gems/ruby-3.3.6/gems/logging-2.4.0/lib/logging.rb:198):1:in `logger'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/configurable.rb:115:in `logger'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_controller/log_subscriber.rb:91:in `logger'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/log_subscriber.rb:143:in `silenced?'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/notifications/fanout.rb:406:in `silenced?'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/notifications/fanout.rb:312:in `block in listening?'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/notifications/fanout.rb:312:in `any?'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/notifications/fanout.rb:312:in `listening?'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/notifications.rb:209:in `instrument'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_controller/metal/instrumentation.rb:74:in `process_action'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activerecord-7.2.2.2/lib/active_record/railties/controller_runtime.rb:39:in `process_action'",
 "/home/myapp/app/controllers/application_controller.rb:215:in `process_action'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/abstract_controller/base.rb:163:in `process'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionview-7.2.2.2/lib/action_view/rendering.rb:40:in `process'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_controller/metal.rb:252:in `dispatch'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_controller/metal.rb:335:in `dispatch'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/routing/route_set.rb:67:in `dispatch'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/routing/route_set.rb:50:in `serve'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/routing/mapper.rb:32:in `block in <class:Constraints>'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/routing/mapper.rb:62:in `serve'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/journey/router.rb:53:in `block in serve'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/journey/router.rb:133:in `block in find_routes'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/journey/router.rb:126:in `each'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/journey/router.rb:126:in `find_routes'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/journey/router.rb:34:in `serve'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/routing/route_set.rb:896:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/degzipper-0.0.6/lib/degzipper/middleware.rb:16:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/warden-1.2.9/lib/warden/manager.rb:36:in `block in call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/warden-1.2.9/lib/warden/manager.rb:34:in `catch'",
 "/home/.rvm/gems/ruby-3.3.6/gems/warden-1.2.9/lib/warden/manager.rb:34:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/tempfile_reaper.rb:15:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/etag.rb:27:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/conditional_get.rb:27:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/head.rb:12:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/http/permissions_policy.rb:38:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/http/content_security_policy.rb:38:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/session/abstract/id.rb:266:in `context'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/session/abstract/id.rb:260:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/cookies.rb:704:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activerecord-7.2.2.2/lib/active_record/migration.rb:674:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/callbacks.rb:31:in `block in call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/callbacks.rb:101:in `run_callbacks'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/callbacks.rb:30:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/executor.rb:16:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:31:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/show_exceptions.rb:32:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/railties-7.2.2.2/lib/rails/rack/logger.rb:41:in `call_app'",
 "/home/.rvm/gems/ruby-3.3.6/gems/railties-7.2.2.2/lib/rails/rack/logger.rb:29:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/sprockets-rails-3.5.2/lib/sprockets/rails/quiet_assets.rb:17:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/remote_ip.rb:96:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/request_id.rb:33:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/method_override.rb:24:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/runtime.rb:22:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/activesupport-7.2.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/server_timing.rb:61:in `block in call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/server_timing.rb:26:in `collect_events'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/server_timing.rb:60:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/executor.rb:16:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/static.rb:27:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/rack-2.2.14/lib/rack/sendfile.rb:110:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/actionpack-7.2.2.2/lib/action_dispatch/middleware/host_authorization.rb:143:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/railties-7.2.2.2/lib/rails/engine.rb:535:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/configuration.rb:272:in `call'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/request.rb:100:in `block in handle_request'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/thread_pool.rb:378:in `with_force_shutdown'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/request.rb:99:in `handle_request'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/server.rb:464:in `process_client'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/server.rb:245:in `block in run'",
 "/home/.rvm/gems/ruby-3.3.6/gems/puma-6.4.3/lib/puma/thread_pool.rb:155:in `block in spawn_thread'",
 "/home/.rvm/gems/ruby-3.3.6/gems/logging-2.4.0/lib/logging/diagnostic_context.rb:474:in `block in create_with_logging_context'"]

After some investigation we realized the error relates to the Logging::Repository#to_key method. It seems there is some conflict with the updates to the Rails logger (and perhaps our configuration). In particular the error seemed to not be able to parse the root logger, which had the class of OrderedOption.

We managed to work around this error with the following monkey patch:

module Logging
  class Repository
    def to_key( key )
      case key
      when :root, 'root'; :root
      when String; key
      when Symbol; key.to_s
      when Module; key.logger_name
      when Object; key.class.logger_name
      end
    rescue NoMethodError => _
      key.class.logger_name
    end
  end
end

We have configured the logging gem quite extensively, with custom escape patterns and filtered exceptions, but even when I remove the patches the same error occurs.

Has this bug been encountered previously? If so, do you have any recommendations for configuration that could fix this issue?

If you require any further information let me know and I will provide what I can.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions