-
-
Save edzhelyov/3303414 to your computer and use it in GitHub Desktop.
Rails 3 logs with severity and PIDs
This file contains hidden or 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
# You must require this file in application.rb, above the Application | |
# definition, for this to work. For example: | |
# | |
# # Syslog-like Rails logs | |
# if Rails.env.production? | |
# require File.expand_path('../../lib/better_logger', __FILE__) | |
# end | |
# | |
# module MyApp | |
# class Application < Rails::Application | |
require 'active_support/buffered_logger' | |
module BetterLogger | |
SEVERITIES = ActiveSupport::BufferedLogger::Severity.constants.sort_by{|c| ActiveSupport::BufferedLogger::Severity.const_get(c) } | |
def add(severity, message = nil, progname = nil, &block) | |
return if @level > severity | |
message = (message || (block && block.call) || progname).to_s | |
# Prepend pid and severity to the written message | |
message = "[%s] %-5.5s %s" % [$$, SEVERITIES[severity], message.gsub(/^\n+/, '')] | |
super | |
end | |
class Railtie < ::Rails::Railtie | |
# Extend the actual Rails logger with our module | |
initializer :initialize_better_logger, :after => :initialize_logger do |app| | |
Rails.logger.extend(BetterLogger) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment