Created
March 21, 2022 17:15
-
-
Save frullah/9890831e39bece7d1ebec712717a9cee to your computer and use it in GitHub Desktop.
read query from activerecord log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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