Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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