Skip to content

Instantly share code, notes, and snippets.

@frullah
Created March 21, 2022 17:15
Show Gist options
  • Save frullah/9890831e39bece7d1ebec712717a9cee to your computer and use it in GitHub Desktop.
Save frullah/9890831e39bece7d1ebec712717a9cee to your computer and use it in GitHub Desktop.
read query from activerecord log
Rails.application.config.colorize_logging = false
io = StringIO.new
logger = Logger.new(io)
logger.formatter = proc do |_severity, _time, _progname, message|
sql = message.sub(/^\s*[A-Za-z0-9]+ Load \(\d+\.\d+ms\)\s+/, "").chomp
sql = sql.sub(/^\s*\(\d+\.\d+ms\)\s+/, "").chomp
IO.popen("sql-formatter", "r+") do |pipe|
pipe.puts sql
pipe.close_write
pipe.read
end
end
ActiveRecord::Base.logger = logger
# do something triggers SQL here
Clipboard.copy(io.string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment