Skip to content

Instantly share code, notes, and snippets.

@davidrenne
Created January 7, 2013 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrenne/4475557 to your computer and use it in GitHub Desktop.
Save davidrenne/4475557 to your computer and use it in GitHub Desktop.
log all queries to file in ruby/rails
connection = ActiveRecord::Base.connection
class << connection
alias :original_exec :execute
def execute(sql, *name)
# try to log sql command but ignore any errors that occur in this block
# we log before executing, in case the execution raises an error
begin
file = File.open(Rails.root + "/log/database.log",'a'){|f| f.puts Time.now.to_s+": "+sql}
rescue Exception => e
;
end
# execute original statement
original_exec(sql, *name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment