Skip to content

Instantly share code, notes, and snippets.

@dmitriy-kiriyenko
Created November 22, 2012 09:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriy-kiriyenko/4130272 to your computer and use it in GitHub Desktop.
Save dmitriy-kiriyenko/4130272 to your computer and use it in GitHub Desktop.
Cool logging snippets
# This silences assets logger.
# Till https://github.com/rails/rails/issues/2639 is resolved
# this is the only way to have a reasonable logs.
# Someone, please, study
# - https://github.com/rails/rails/pull/3741
# - https://github.com/rails/rails/pull/3795
# and submit a correct pull request as suggested.
#
# Solution taken from http://stackoverflow.com/questions/7471606/dont-log-asset-requests-in-rails-3-1-in-development-mode
Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if silence?(env)
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
end
alias_method_chain :call, :quiet_assets
def silence?(env)
env['PATH_INFO'].index("/assets/") == 0
end
end
# Log time and log level.
# Monkey patch. Sorry.
class Logger::SimpleFormatter
# This method is invoked when a log event occurs
def call(severity, timestamp, progname, msg)
msg = "#{String === msg ? msg : msg.inspect}"
"[#{timestamp.to_s(:log)}] #{"%5s"% severity.upcase} #{msg}\n"
end
end
# This brings full urls to logs
# Monkey-patch. Sorry.
Rails::Rack::Logger.class_eval do
def call_app(env)
request = ActionDispatch::Request.new(env)
Rails.logger.info ""
Rails.logger.info ""
Rails.logger.info "Started #{request.request_method} \"#{request.url}\" for #{request.ip} at #{Time.now.to_default_s}"
@app.call(env)
ensure
ActiveSupport::LogSubscriber.flush_all!
end
end
class ActionView::LogSubscriber
def render_template(event)
log_rendering_event("info", event)
end
def render_partial(event)
log_rendering_event("debug", event)
end
alias :render_collection :render_partial
def log_rendering_event(severity, event)
message = " Rendered #{from_rails_root(event.payload[:identifier])}"
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << (" (%.1fms)" % event.duration)
send(severity, message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment