You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sql.active_record events should be logged properly.
Actual Behavior
Some combinations of binds in the query lead to logging errors like this:
2024-11-14 12:24:35.453837 E [39239:5920 log_subscriber.rb:188] ActiveRecord -- Could not log "sql.active_record" event. NoMethodError: undefined method `type' for an instance of Integer ["/Users/ruby/.asdf/installs/ruby/3.3.6/lib/ruby/gems/3.3.0/gems/rails_semantic_logger-4.17.0/lib/rails_semantic_logger/active_record/log_subscriber.rb:162:in `render_bind_v5_0_3'"
We can see from the stack trace it's calling render_bind_v5_0_3 in RailsSemanticLogger::ActiveRecord::LogSubscriber, and from looking at the source code there we can see this is due to the logic used to setup event handlers by Rails version defaulting to treating it as Rails 5/5.1/6:
To stop this from happening when the Rails major version increases again (assuming there's no other incompatibility), the check for Rails 7 should probably be changed to >= from == in RailsSemanticLogger::ActiveRecord::LogSubscriber.
I haven't looked at any of the other subscriber classes other than ActiveRecord, so there likely needs to be updates made there too.
Temporary fix (for those creating a new project with/upgrading to Rails 8)
As a temporary workaround until the gem is actually properly tested with Rails 8 and a new version is released, here's a monkeypatch that got things working for me on Rails 8.0.0 and version 4.17.0 of this gem. To use it, make a new file in your initializers folder (i.e. config/initializers/rails_semantic_logger_8_monkeypatch.rb) with the following contents:
# frozen_string_literal: trueLAST_TESTED_VERSION="4.17.0"require"rails_semantic_logger/version"unlessRailsSemanticLogger::VERSION == LAST_TESTED_VERSIONraise"rails_semantic_logger is version #{RailsSemanticLogger::VERSION} but the monkey patch was last tested on #{LAST_TESTED_VERSION} - manually check if it supports Rails 8 now and this can be removed, or that this still works as intended"endmoduleLogSubscriberMonkeyPatchdefself.included(base)base.alias_method(:bind_values,:bind_values_v6_1)base.alias_method(:render_bind,:render_bind_v6_1)base.alias_method(:type_casted_binds,:type_casted_binds_v5_1_5)endendRailsSemanticLogger::ActiveRecord::LogSubscriber.include(LogSubscriberMonkeyPatch)
This also will check when your Rails application starts that you are still using 4.17.0 and will raise if the gem version does not match. My project is using Renovatebot so this is helpful to ensure CI will fail when it creates the change to upgrade to a newer version of this gem, but it's not necessary to just get things working in Rails 8.
The text was updated successfully, but these errors were encountered:
Just a short note to say that I have encountered this bug and came to the same conclusion about the cause. Pleased to find such a thorough issue already reported. Nice work, thanks @rubyroobs!
(If you are trying to create a new project with or upgrade to Rails 8 and having this issue, I have a temporary workaround at the bottom!)
Environment
Expected Behavior
sql.active_record
events should be logged properly.Actual Behavior
Some combinations of binds in the query lead to logging errors like this:
We can see from the stack trace it's calling
render_bind_v5_0_3
inRailsSemanticLogger::ActiveRecord::LogSubscriber
, and from looking at the source code there we can see this is due to the logic used to setup event handlers by Rails version defaulting to treating it as Rails 5/5.1/6:Proposed permanent fix
To stop this from happening when the Rails major version increases again (assuming there's no other incompatibility), the check for Rails 7 should probably be changed to
>=
from==
inRailsSemanticLogger::ActiveRecord::LogSubscriber
.I haven't looked at any of the other subscriber classes other than ActiveRecord, so there likely needs to be updates made there too.
Temporary fix (for those creating a new project with/upgrading to Rails 8)
As a temporary workaround until the gem is actually properly tested with Rails 8 and a new version is released, here's a monkeypatch that got things working for me on Rails 8.0.0 and version 4.17.0 of this gem. To use it, make a new file in your initializers folder (i.e.
config/initializers/rails_semantic_logger_8_monkeypatch.rb
) with the following contents:This also will check when your Rails application starts that you are still using
4.17.0
and will raise if the gem version does not match. My project is using Renovatebot so this is helpful to ensure CI will fail when it creates the change to upgrade to a newer version of this gem, but it's not necessary to just get things working in Rails 8.The text was updated successfully, but these errors were encountered: