Skip to content

Instantly share code, notes, and snippets.

@JackDanger
Created July 19, 2011 20:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save JackDanger/1093616 to your computer and use it in GitHub Desktop.
Save JackDanger/1093616 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