Parent

Included Modules

Logging::Appenders::Syslog

This class provides an Appender that can write to the UNIX syslog daemon.

Public Class Methods

new( name, opts = {} ) click to toggle source

Create an appender that will log messages to the system message logger. The message is then written to the system console, log files, logged-in users, or forwarded to other machines as appropriate. The options that can be used to configure the appender are as follows:

   :ident     => identifier string (name is used by default)
   :logopt    => options used when opening the connection
   :facility  => the syslog facility to use

The parameter :ident is a string that will be prepended to every message. The :logopt argument is a bit field specifying logging options, which is formed by OR’ing one or more of the following values:

   LOG_CONS      If syslog() cannot pass the message to syslogd(8) it
                 wil attempt to write the message to the console
                 ('/dev/console').

   LOG_NDELAY    Open the connection to syslogd(8) immediately. Normally
                 the open is delayed until the first message is logged.
                 Useful for programs that need to manage the order in
                 which file descriptors are allocated.

   LOG_PERROR    Write the message to standard error output as well to
                 the system log.  Not available on Solaris.

   LOG_PID       Log the process id with each message: useful for
                 identifying instantiations of daemons.

The :facility parameter encodes a default facility to be assigned to all messages that do not have an explicit facility encoded:

   LOG_AUTH      The authorization system: login(1), su(1), getty(8),
                 etc.

   LOG_AUTHPRIV  The same as LOG_AUTH, but logged to a file readable
                 only by selected individuals.

   LOG_CONSOLE   Messages written to /dev/console by the kernel console
                 output driver.

   LOG_CRON      The cron daemon: cron(8).

   LOG_DAEMON    System daemons, such as routed(8), that are not
                 provided for explicitly by other facilities.

   LOG_FTP       The file transfer protocol daemons: ftpd(8), tftpd(8).

   LOG_KERN      Messages generated by the kernel. These cannot be
                 generated by any user processes.

   LOG_LPR       The line printer spooling system: lpr(1), lpc(8),
                 lpd(8), etc.

   LOG_MAIL      The mail system.

   LOG_NEWS      The network news system.

   LOG_SECURITY  Security subsystems, such as ipfw(4).

   LOG_SYSLOG    Messages generated internally by syslogd(8).

   LOG_USER      Messages generated by random user processes. This is
                 the default facility identifier if none is specified.

   LOG_UUCP      The uucp system.

   LOG_LOCAL0    Reserved for local use. Similarly for LOG_LOCAL1
                 through LOG_LOCAL7.
     # File lib/logging/appenders/syslog.rb, line 95
 95:     def initialize( name, opts = {} )
 96:       @ident = opts.getopt(:ident, name)
 97:       @logopt = opts.getopt(:logopt, (LOG_PID | LOG_CONS), :as => Integer)
 98:       @facility = opts.getopt(:facility, LOG_USER, :as => Integer)
 99:       @syslog = ::Syslog.open(@ident, @logopt, @facility)
100: 
101:       # provides a mapping from the default Logging levels
102:       # to the syslog levels
103:       @map = [LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR, LOG_CRIT]
104: 
105:       map = opts.getopt(:map)
106:       self.map = map unless map.nil?
107: 
108:       super
109:     end

Public Instance Methods

close click to toggle source

Closes the connection to the syslog facility.

     # File lib/logging/appenders/syslog.rb, line 139
139:     def close( footer = true )
140:       super
141:       @syslog.close if @syslog.opened?
142:       self
143:     end
closed? => true or false click to toggle source

Queries the connection to the syslog facility and returns true if the connection is closed.

     # File lib/logging/appenders/syslog.rb, line 151
151:     def closed?
152:       !@syslog.opened?
153:     end
map = { logging_levels => syslog_levels } click to toggle source

Configure the mapping from the Logging levels to the syslog levels. This is needed in order to log events at the proper syslog level.

Without any configuration, the following mapping will be used:

   :debug  =>  LOG_DEBUG
   :info   =>  LOG_INFO
   :warn   =>  LOG_WARNING
   :error  =>  LOG_ERR
   :fatal  =>  LOG_CRIT
     # File lib/logging/appenders/syslog.rb, line 125
125:     def map=( levels )
126:       map = []
127:       levels.keys.each do |lvl|
128:         num = ::Logging.level_num(lvl)
129:         map[num] = syslog_level_num(levels[lvl])
130:       end
131:       @map = map
132:     end
reopen() click to toggle source

Reopen the connection to the underlying logging destination. If the connection is currently closed then it will be opened. If the connection is currently open then it will be closed and immediately opened.

     # File lib/logging/appenders/syslog.rb, line 159
159:     def reopen
160:       @mutex.synchronize {
161:         if @syslog.opened?
162:           flush
163:           @syslog.close
164:         end
165:         @syslog = ::Syslog.open(@ident, @logopt, @facility)
166:       }
167:       super
168:       self
169:     end

Private Instance Methods

syslog_level_num( level ) => integer click to toggle source

Takes the given level as a string, symbol, or integer and returns the corresponding syslog level number.

     # File lib/logging/appenders/syslog.rb, line 201
201:     def syslog_level_num( level )
202:       case level
203:       when Integer; level
204:       when String, Symbol
205:         level = level.to_s.upcase
206:         self.class.const_get level
207:       else
208:         raise ArgumentError, "unkonwn level '#{level}'"
209:       end
210:     end
write( event ) click to toggle source

Write the given event to the syslog facility. The log event will be processed through the Layout associated with this appender. The message will be logged at the level specified by the event.

     # File lib/logging/appenders/syslog.rb, line 181
181:     def write( event )
182:       pri = LOG_DEBUG
183:       message = if event.instance_of?(::Logging::LogEvent)
184:           pri = @map[event.level]
185:           @layout.format(event)
186:         else
187:           event.to_s
188:         end
189:       return if message.empty?
190: 
191:       @syslog.log(pri, '%s', message)
192:       self
193:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.