Module: ActiveRecord::QueryLogs
- Defined in:
- lib/active_record/query_logs.rb
Overview
Active Record Query Logs
Automatically tag SQL queries with runtime information.
Default tags available for use:
-
application
-
pid
-
socket
-
db_host
-
database
_Action Controller and Active Job tags are also defined when used in Rails:_
-
controller
-
action
-
job
The tags used in a query can be configured directly:
ActiveRecord::QueryLogs. = [ :application, :controller, :action, :job ]
or via Rails configuration:
config.active_record. = [ :application, :controller, :action, :job ]
To add new comment tags, add a hash to the tags array containing the keys and values you want to add to the comment. Dynamic content can be created by setting a proc or lambda value in a hash, and can reference any value stored in the context
object.
Example:
= [
:application,
{
custom_tag: ->(context) { context[:controller]&.controller_name },
custom_value: -> { Custom.value },
}
]
ActiveRecord::QueryLogs. =
The QueryLogs context
can be manipulated via the ActiveSupport::ExecutionContext.set
method.
Temporary updates limited to the execution of a block:
ActiveSupport::ExecutionContext.set(foo: Bar.new) do
posts = Post.all
end
Direct updates to a context value:
ActiveSupport::ExecutionContext[:foo] = Bar.new
Tag comments can be prepended to the query:
ActiveRecord::QueryLogs.prepend_comment = true
For applications where the content will not change during the lifetime of the request or job execution, the tags can be cached for reuse in every query:
ActiveRecord::QueryLogs. = true
This option can be set during application configuration or in a Rails initializer:
config.active_record. = true
Class Method Summary collapse
-
.call(sql) ⇒ Object
:nodoc:.
-
.clear_cache ⇒ Object
:nodoc:.
Class Method Details
.call(sql) ⇒ Object
:nodoc:
79 80 81 82 83 84 85 |
# File 'lib/active_record/query_logs.rb', line 79 def call(sql) # :nodoc: if prepend_comment "#{self.comment} #{sql}" else "#{sql} #{self.comment}" end.strip end |
.clear_cache ⇒ Object
:nodoc:
87 88 89 |
# File 'lib/active_record/query_logs.rb', line 87 def clear_cache # :nodoc: self.cached_comment = nil end |