Lograge::Sql is an extension to the famous Lograge gem, which adds SQL queries to the Lograge Event and disable default ActiveRecord logging. This is extremely useful if you're using Lograge together with the ELK stack.
Add this line to your application's Gemfile:
gem 'lograge-sql'
In order to enable SQL logging in your application, you'll simply need to add this on top of your lograge initializer:
# config/initializers/lograge
require 'lograge/sql/extension'
By default, Lograge::Sql disables default logging on ActiveRecord. To preserve default logging, add this to your lograge initializer:
config.lograge_sql.keep_default_active_record_log = true
By default, lograge-sql
stores all queries, but you can set a min_duration_ms
config.
When you do so, only queries that run for AT LEAST min_duration_ms
milliseconds will be logged, and all others will be ignored.
This can be really helpful if you want to detect Slow SQL queries
.
# config/initializers/lograge.rb
Rails.application.configure do
# Defaults is zero
config.lograge_sql.min_duration_ms = 5000 # milliseconds
end
You can filter out queries using the query_name_denylist
configuration.
This takes an array of regular expressions to match against the query name. If the query name matches any of the regular expressions, it will be ignored. By default, lograge-sql
ignores queries named SCHEMA
and queries from the SolidCable
namespace.
If you are using Solid Cable in your project, be careful when removing this default value as it will cause a memory leak.
# config/initializers/lograge.rb
Rails.application.configure do
# Defaults is [/\ASCHEMA\z/, /\ASolidCable::/]
config.lograge_sql.query_name_denylist << /\AEXACT NAME TO IGNORE\z/
end
By default, the format is a string concatenation of the query name, the query duration and the query itself joined by \n
newline:
method=GET path=/mypath format=html ...
Object Load (0.42) SELECT "objects.*" FROM "objects"
Associations Load (0.42) SELECT "associations.*" FROM "associations" WHERE "associations"."object_id" = "$1"
However, having Lograge::Formatters::Json.new
, the relevant output is
{
"sql_queries": "name1 ({duration1}) {query1}\nname2 ({duration2}) query2 ...",
"sql_queries_count": 3
}
To customize the output:
# config/initializers/lograge.rb
Rails.application.configure do
# Instead of extracting event as Strings, extract as Hash. You can also extract
# additional fields to add to the formatter
config.lograge_sql.extract_event = Proc.new do |event|
{ name: event.payload[:name], duration: event.duration.to_f.round(2), sql: event.payload[:sql] }
end
# Format the array of extracted events
config.lograge_sql.formatter = Proc.new do |sql_queries|
sql_queries
end
end
By default, lograge-sql
will log full query but if you have sensitive data that need to be filtered out, you can set query_filter
config:
Rails.application.configure do
config.lograge_sql.query_filter = ->(query) { query.gsub(/custom_regexp/, "[FILTERED]".freeze) }
end
Depending on the web server in your project you might benefit from improved thread-safety by adding request_store
to your Gemfile. It will be automatically picked up by lograge-sql
.
Bug reports and pull requests are welcome on GitHub at https://github.com/iMacTia/lograge-sql.