Skip to content

Instantly share code, notes, and snippets.

@cpetschnig
Created June 6, 2012 11:29
Show Gist options
  • Save cpetschnig/2881364 to your computer and use it in GitHub Desktop.
Save cpetschnig/2881364 to your computer and use it in GitHub Desktop.
Show the source of your Rails log messages. Put this file in your config/initializers directory.
require 'active_support/buffered_logger'
class ActiveSupport::BufferedLogger
def add(severity, message = nil, progname = nil, &block)
add_debugging_details(severity)
@log.add(severity, message, progname, &block)
end
private
EXCLUDE_CALLERS = Gem.paths.path.clone << 'script/rails' << RbConfig::CONFIG['rubylibdir'] << __FILE__
def add_debugging_details(severity)
caller_in_app = caller.select do |line|
EXCLUDE_CALLERS.detect { |gem_path| line.starts_with?(gem_path) }.nil?
end
return if caller_in_app.empty?
@log.add(severity, "Your code in \e[1;33m#{caller_in_app.first.sub(%r(^#{Rails.root}/), '')}\e[0;0m triggered:")
end
end if Rails.env.development?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment