Class: Logging::Appenders::RemoteSyslog

Inherits:
Logging::Appender
  • Object
show all
Defined in:
lib/logging/appenders/remote-syslog.rb

Overview

This class provides an Appender that can write to remote syslog.

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ RemoteSyslog

call-seq:

RemoteSyslog.new( name, opts = {} )

Create an appender that will log messages to remote syslog. The options that can be used to configure the appender are as follows:

:ident         => identifier string (name is used by default)
:syslog_server => address of the remote syslog server
:port          => port of the remote syslog server
:facility      => the syslog facility to use
:modifier      => an optional callback method for altering original message; takes original message and returns updated one

The parameter :ident is a string that will be prepended to every message. The :facility parameter encodes a default facility to be assigned to all messages that do not have an explicit facility encoded.

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

'authpriv'   The same as 'auth', but logged to a file readable
             only by selected individuals.

'cron'       The cron daemon: cron(8).

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

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

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

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

'mail'       The mail system.

'news'       The network news system.

'syslog'     Messages generated internally by syslogd(8).

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

'uucp'       The uucp system.

'local0'     Reserved for local use. Similarly for 'local1'
             through 'local7'.

See SyslogProtocol::FACILITIES for the complete list of valid values.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logging/appenders/remote-syslog.rb', line 78

def initialize( name, opts = {} )
  @ident = opts.getopt(:ident, name)
  @syslog_server =  opts.getopt(:syslog_server, '127.0.0.1')
  @port = opts.getopt(:port, 514, :as => Integer)
  @modifier = opts.getopt(:modifier)

  @strip_colors =  opts.getopt(:strip_colors, true)

  facility_name = opts.getopt(:facility, 'user')

  @facility = ::SyslogProtocol::FACILITIES[facility_name]

  # provides a mapping from the default Logging levels
  # to the syslog levels
  @map = ['debug', 'info', 'warn', 'err', 'crit']

  map = opts.getopt(:map)
  self.map = map unless map.nil?

  super
end

Instance Method Details

#map=(levels) ⇒ Object

call-seq:

map = { logging_levels => syslog_levels }

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  =>  'debug'
:info   =>  'info'
:warn   =>  'warn'
:error  =>  'err'
:fatal  =>  'crit'


114
115
116
117
118
119
120
121
# File 'lib/logging/appenders/remote-syslog.rb', line 114

def map=( levels )
  map = []
  levels.keys.each do |lvl|
    num = ::Logging.level_num(lvl)
    map[num] = syslog_level_num(levels[lvl])
  end
  @map = map
end

#prepare_message(message) ⇒ Object



127
128
129
130
131
# File 'lib/logging/appenders/remote-syslog.rb', line 127

def prepare_message(message)
  message = @strip_colors ? strip_ansi_colors(message) : message
  message = @modifier.call(message) if @modifier
  message
end

#strip_ansi_colors(message) ⇒ Object



123
124
125
# File 'lib/logging/appenders/remote-syslog.rb', line 123

def strip_ansi_colors(message)
  message.gsub /\e\[?.*?[\@-~]/, ''
end