123456789_123456789_123456789_123456789_123456789_

Class: Syslog::Logger

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: ext/syslog/lib/syslog/logger.rb

Overview

Logger is a Logger work-alike that logs via syslog instead of to a file. You can use Logger to aggregate logs between multiple machines.

By default, Logger uses the program name 'ruby', but this can be changed via the first argument to .new.

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

Example

The following will log to syslogd on your local machine:

require 'syslog/logger'

log = Syslog::Logger.new 'my_program'
log.info 'this line will be logged via syslog(3)'

Also the facility may be set to specify the facility level which will be used:

log.info 'this line will be logged using Syslog default facility level'

log_local1 = Syslog::Logger.new 'my_program', Syslog::LOG_LOCAL1
log_local1.info 'this line will be logged using local1 facility level'

You may need to perform some syslog.conf setup first. For a BSD machine add the following lines to /etc/syslog.conf:

!my_program
*.*                                             /var/log/my_program.log

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

If you wish to have logs automatically roll over and archive, see the newsyslog.conf(5) and newsyslog(8) man pages.

Constant Summary

  • LEVEL_MAP =

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

    Messages from Ruby applications are not considered as critical as messages from other system daemons 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).

    # File 'ext/syslog/lib/syslog/logger.rb', line 80
    {
      ::Logger::UNKNOWN => Syslog::LOG_ALERT,
      ::Logger::FATAL   => Syslog::LOG_ERR,
      ::Logger::ERROR   => Syslog::LOG_WARNING,
      ::Logger::WARN    => Syslog::LOG_NOTICE,
      ::Logger::INFO    => Syslog::LOG_INFO,
      ::Logger::DEBUG   => Syslog::LOG_DEBUG,
    }
  • VERSION =

    The version of Logger you are using.

    # File 'ext/syslog/lib/syslog/logger.rb', line 70
    '2.1.0'

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

  • #facility rw

    The facility argument is used to specify what type of program is logging the message.

  • #formatter rw

    Logging formatter, as a Proc that will take four arguments and return the formatted message.

  • #level rw

    Log level for Logger compatibility.

Instance Method Summary

  • #add(severity, message = nil, progname = nil, &block)

    Almost duplicates #add.

  • #debug

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

  • #error

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

  • #fatal

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

  • #info

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

  • #unknown

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

  • #warn

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

Constructor Details

.new(program_name = 'ruby', facility = nil) ⇒ Logger

Fills in variables for Logger compatibility. If this is the first instance of Logger, program_name may be set to change the logged program name. The #facility may be set to specify the facility level which will be used.

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 191

def initialize program_name = 'ruby', facility = nil
  @level = ::Logger::DEBUG
  @formatter = Formatter.new

  @@syslog ||= Syslog.open(program_name)

  @facility = (facility || @@syslog.facility)
end

Class Attribute Details

.syslog (rw)

Returns the internal ::Syslog object that is initialized when the first instance is created.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 93

def self.syslog
  @@syslog
end

.syslog=(syslog) (rw)

Specifies the internal ::Syslog object to be used.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 100

def self.syslog= syslog
  @@syslog = syslog
end

Class Method Details

.make_methods(meth)

Builds a methods for level meth.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 107

def self.make_methods meth
  level = ::Logger.const_get(meth.upcase)
  eval <<-EOM, nil, __FILE__, __LINE__ + 1
    def #{meth}(message = nil, &block)
      add(#{level}, message, &block)
    end

    def #{meth}?
      @level <= #{level}
    end
  EOM
end

Instance Attribute Details

#facility (rw)

The facility argument is used to specify what type of program is logging the message.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 182

attr_accessor :facility

#formatter (rw)

Logging formatter, as a Proc that will take four arguments and return the formatted message. The arguments are:

severity

The Severity of the log message.

time

A Time instance representing when the message was logged.

progname

The #progname configured, or passed to the logger method.

msg

The Object the user passed to the log message; not necessarily a String.

The block should return an Object that can be written to the logging device via write. The default formatter is used when no formatter is set.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 177

attr_accessor :formatter

#level (rw)

Log level for Logger compatibility.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 163

attr_accessor :level

Instance Method Details

#add(severity, message = nil, progname = nil, &block)

Almost duplicates #add. progname is ignored.

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 203

def add severity, message = nil, progname = nil, &block
  severity ||= ::Logger::UNKNOWN
  @level <= severity and
    @@syslog.log( (LEVEL_MAP[severity] | @facility), '%s', formatter.call(severity, Time.now, progname, (message || block.call)) )
  true
end

#debug

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 157

rdoc_method :method: debug

#error

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 133

rdoc_method :method: error

#fatal

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 127

rdoc_method :method: fatal

#info

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 145

rdoc_method :method: info

#unknown

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 121

rdoc_method :method: unknown

#warn

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

[ GitHub ]

  
# File 'ext/syslog/lib/syslog/logger.rb', line 139

rdoc_method :method: warn