Created
November 22, 2012 09:45
-
-
Save dmitriy-kiriyenko/4130272 to your computer and use it in GitHub Desktop.
Cool logging snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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