Class: SyslogLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/production_log/syslog_logger.rb

Overview

This Logger logs messages to syslogd. Add it to your production environment so you can run pl_analyze on your log files.

SyslogLogger logs everything. Tweak your syslog.conf to throw away messages you don’t want, SyslogLogger ignores any calls to #level=.

SyslogLogger logs messages using the ‘rails’ facility.

Sample usage

config/environment/production.rb

Add the following lines:

require 'production_log/syslog_logger'
RAILS_DEFAULT_LOGGER = SyslogLogger.new

config/environment.rb

In 0.10.0, change this line:

RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")

to:

RAILS_DEFAULT_LOGGER ||= Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")

Other versions of Rails should have a similar change.

/etc/syslog.conf

Add the following lines:

!rails
*.*                                             /var/log/production.log

Then touch /var/log/production.log and signal syslogd with a HUP (killall -HUP syslogd, on FreeBSD).

/etc/newsyslog.conf

Add the following line:

/var/log/production.log                 640  7     *    @T00  Z

This creates a log file that is rotated every day at midnight, gzip’d, then kept for 7 days. Consult newsyslog.conf(5) for more details.

Now restart your Rails app. Your production logs should now be showing up in /var/log/production.log. If you have mulitple machines, you can log them all to a central machine with remote syslog logging for analysis.

Constant Summary collapse

SYSLOG =

Syslog is a module, so this constant holds our open logger.

Syslog.open "rails"
LOGGER_MAP =

Maps Logger warning types to syslog(3) warning types.

{
    :fatal => :err,
    :error => :warning,
    :warn  => :notice,
    :info  => :info,
    :debug => :debug,
}
LOGGER_LEVEL_MAP =

Maps Logger log levels to their values so we can silence.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SyslogLogger

Fill in variables for Logger compatibility.



107
108
109
# File 'lib/production_log/syslog_logger.rb', line 107

def initialize(*args)
    @level = Logger::DEBUG
end

Instance Attribute Details

#levelObject

Log level for Logger compatibility.



62
63
64
# File 'lib/production_log/syslog_logger.rb', line 62

def level
  @level
end

Class Method Details

.log_method(meth) ⇒ Object

Builds a logging method for level meth.



92
93
94
95
96
97
98
# File 'lib/production_log/syslog_logger.rb', line 92

def self.log_method(meth)
    eval <<-EOF
        def #{meth}(message)
            SYSLOG.#{LOGGER_MAP[meth]} clean(message) if #{LOGGER_LEVEL_MAP[meth]} >= @level
        end
    EOF
end

Instance Method Details

#silence(temporary_level = Logger::ERROR) ⇒ Object

Allows messages of a particular log level to be ignored temporarily.

Can you say “Broken Windows”?



116
117
118
119
120
121
122
# File 'lib/production_log/syslog_logger.rb', line 116

def silence(temporary_level = Logger::ERROR)
    old_logger_level = @level
    @level = temporary_level 
    yield
ensure
    @level = old_logger_level
end