Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save billhorsman/1095069 to your computer and use it in GitHub Desktop.
Save billhorsman/1095069 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.0
def log_with_trace(sql, name = "SQL", &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, ''
name = "#{name} #{c}"
end
log_without_trace sql, name, &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