Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jlindley/1093930 to your computer and use it in GitHub Desktop.
Save jlindley/1093930 to your computer and use it in GitHub Desktop.
Let Rails display file names and line numbers for log activity.
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
protected
# Turn:
# User Load (6.3ms) SELECT * FROM "users"
# Into:
# User Load /app/views/_partial.erb:27 (6.3ms) in `_app_views_partial_erb` SELECT * FROM "users"
# Rails 3.1
def log_with_trace(sql, name = "SQL", binds=[], &block)
@stringified_rails_root ||= Rails.root.to_s
if @logger && @logger.debug?
c = caller.detect{|line| line !~ /(activerecord|active_support|__DELEGATION__|\/lib\/|\/vendor\/plugins|\/vendor\/gems)/i}
c ||= caller.first
c.gsub! @stringified_rails_root, ''
sql = "#{c}\n\t#{sql}"
end
log_without_trace sql, name, binds, &block
end
alias_method_chain :log, :trace
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment