class SyslogLogger

SyslogLogger is a Logger work-alike that logs via syslog instead of to a file. You can add SyslogLogger to your Rails production environment to aggregate logs between multiple machines.

By default, SyslogLogger uses the program name ‘rails’, but this can be changed via the first argument to ::new.

NOTE! You can only set the SyslogLogger program name when you initialize SyslogLogger for the first time. This is a limitation of the way SyslogLogger uses syslog (and in some ways, a limitation of the way syslog(3) works). Attempts to change SyslogLogger’s program name after the first initialization will be ignored.

Sample usage with Rails

config/environment/production.rb

Add the following lines:

require '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.

BSD syslog setup

/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.

syslog-ng setup

syslog-ng.conf

destination rails_log { file("/var/log/production.log"); };
filter f_rails { program("rails.*"); };
log { source(src); filter(f_rails); destination(rails_log); };

Starting

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. Consult your syslogd(8) manpage for further details.

Constants

LEVEL_LOGGER_MAP

Maps Logger log level values to syslog log levels.

LOGGER_LEVEL_MAP

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

LOGGER_MAP

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

Messages from ruby applications are not considered as critical as messages from other processes using syslog(3), so most messages are reduced by one level. For example, a fatal message for ruby’s Logger is considered an error for syslog(3).

VERSION

The version of SyslogLogger you are using.

Attributes

level[RW]

Log level for Logger compatibility.

Public Class Methods

make_methods(meth) click to toggle source

Builds a methods for level meth.

# File lib/syslog_logger.rb, line 120
  def self.make_methods(meth)
    eval "      def #{meth}(message = nil)
        return true if #{LOGGER_LEVEL_MAP[meth]} < @level
        SYSLOG.#{LOGGER_MAP[meth]} clean(message || yield)
        return true
      end

      def #{meth}?
        @level <= Logger::#{meth.to_s.upcase}
      end
", nil, __FILE__, __LINE__ + 1
  end
new(program_name = 'rails') click to toggle source

Fills in variables for Logger compatibility. If this is the first instance of SyslogLogger, program_name may be set to change the logged program name.

Due to the way syslog works, only one program name may be chosen.

# File lib/syslog_logger.rb, line 186
def initialize(program_name = 'rails')
  @level = Logger::DEBUG

  return if defined? SYSLOG
  self.class.const_set :SYSLOG, Syslog.open(program_name)
end

Public Instance Methods

add(severity, message = nil, progname = nil, &block) click to toggle source

Almost duplicates Logger#add. progname is ignored.

# File lib/syslog_logger.rb, line 196
def add(severity, message = nil, progname = nil, &block)
  severity ||= Logger::UNKNOWN
  return true if severity < @level
  SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message || block.call)
  return true
end
debug() click to toggle source

Logs a message at the debug (syslog debug) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 170
  
error() click to toggle source

Logs a message at the error (syslog warning) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 152
  
fatal() click to toggle source

Logs a message at the fatal (syslog err) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 146
  
info() click to toggle source

Logs a message at the info (syslog info) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 164
  
silence(temporary_level = Logger::ERROR) { || ... } click to toggle source

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

Can you say “Broken Windows”?

# File lib/syslog_logger.rb, line 208
def silence(temporary_level = Logger::ERROR)
  old_logger_level = @level
  @level = temporary_level
  yield
ensure
  @level = old_logger_level
end
unknown() click to toggle source

Logs a message at the unknown (syslog alert) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 140
  
warn() click to toggle source

Logs a message at the warn (syslog notice) log level, or logs the message returned from the block.

# File lib/syslog_logger.rb, line 158