Class: Syslogger

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

Constant Summary collapse

VERSION =
"1.2.8"
MAPPING =
{
  Logger::DEBUG => Syslog::LOG_DEBUG,
  Logger::INFO => Syslog::LOG_INFO,
  Logger::WARN => Syslog::LOG_NOTICE,
  Logger::ERROR => Syslog::LOG_WARNING,
  Logger::FATAL => Syslog::LOG_ERR,
  Logger::UNKNOWN => Syslog::LOG_ALERT
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ Syslogger

Initializes default options for the logger

ident

the name of your program [default=$0].

options

syslog options [default=Syslog::LOG_PID | Syslog::LOG_CONS]. Correct values are:

LOG_CONS    : writes the message on the console if an error occurs when sending the message;
LOG_NDELAY  : no delay before sending the message;
LOG_PERROR  : messages will also be written on STDERR;
LOG_PID     : adds the process number to the message (just after the program name)
facility

the syslog facility [default=nil] Correct values include:

Syslog::LOG_DAEMON
Syslog::LOG_USER
Syslog::LOG_SYSLOG
Syslog::LOG_LOCAL2
Syslog::LOG_NEWS
etc.

Usage:

logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
logger.level = Logger::INFO # use Logger levels
logger.warn "warning message"
logger.debug "debug message"


42
43
44
45
46
47
# File 'lib/syslogger.rb', line 42

def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
  @ident = ident
  @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
  @facility = facility
  @level = Logger::INFO
end

Instance Attribute Details

#facilityObject (readonly)

Returns the value of attribute facility.



8
9
10
# File 'lib/syslogger.rb', line 8

def facility
  @facility
end

#identObject (readonly)

Returns the value of attribute ident.



8
9
10
# File 'lib/syslogger.rb', line 8

def ident
  @ident
end

#levelObject

Returns the value of attribute level.



8
9
10
# File 'lib/syslogger.rb', line 8

def level
  @level
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/syslogger.rb', line 8

def options
  @options
end

Instance Method Details

#<<(msg) ⇒ Object

Logs a message at the Logger::INFO level.



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

def <<(msg)
  add(Logger::INFO, msg)
end

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

Low level method to add a message.

severity

the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN

message

the message string. If nil, the method will call the block and use the result as the message string. If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.

progname

optionally, overwrite the program name that appears in the log message.



72
73
74
75
76
77
78
79
80
81
# File 'lib/syslogger.rb', line 72

def add(severity, message = nil, progname = nil, &block)
  progname ||= @ident
  Syslog.open(progname, @options, @facility) { |s|
    s.mask = Syslog::LOG_UPTO(MAPPING[@level])
    s.log(
      MAPPING[severity], 
      clean(message || (block && block.call) || progname)
    )
  }
end