Class: Lumberjack::SyslogDevice

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

Overview

This Lumberjack device logs output to syslog. There can only be one connection to syslog open at a time. If you use syslog elsewhere in your application, you’ll need to pass :close_connection => true to the constructor. Otherwise, the connection will be kept open between write calls.

Constant Summary collapse

SEVERITY_MAP =
{
  Severity::DEBUG => Syslog::LOG_DEBUG,
  Severity::INFO => Syslog::LOG_INFO,
  Severity::WARN => Syslog::LOG_WARNING,
  Severity::ERROR => Syslog::LOG_ERR,
  Severity::FATAL => Syslog::LOG_CRIT,
  Severity::UNKNOWN => Syslog::LOG_ALERT
}
PERCENT =
"%"
ESCAPED_PERCENT =
"%%"
@@lock =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SyslogDevice

Create a new SyslogDevice. The options control how messages are written to syslog.

The template can be specified using the :template option. This can either be a Proc or a string that will compile into a Template object. If the template is a Proc, it should accept an LogEntry as its only argument and output a string. If the template is a template string, it will be used to create a Template. The default template is ‘:message (#:unit_of_work_id) :tags`.

The :close_connection option can be used to specify that the connection to syslog should be closed after every write call. This will slow down performance, but will allow you to use syslog elsewhere in your application.

The :options option will pass through options to syslog. The default is Syslog::LOG_PID | Syslog::LOG_CONS. Available values for the bit map are:

  • Syslog::LOG_CONS - Write directly to system console if there is an error while sending to system logger.

  • Syslog::LOG_NDELAY - Open the connection immediately (normally, the connection is opened when the first message is logged).

  • Syslog::LOG_NOWAIT - Don’t wait for child processes that may have been created while logging the message.

  • Syslog::LOG_ODELAY - The converse of LOG_NDELAY; opening of the connection is delayed.

  • Syslog::LOG_PERROR - Print to stderr as well.

  • Syslog::LOG_PID - Include PID with each message.

The :facility option will pass through a facility to syslog. Available values are

  • Syslog::LOG_AUTH

  • Syslog::LOG_AUTHPRIV

  • Syslog::LOG_CRON

  • Syslog::LOG_DAEMON

  • Syslog::LOG_FTP

  • Syslog::LOG_KERN

  • Syslog::LOG_LOCAL0 through Syslog::LOG_LOCAL7

  • Syslog::LOG_LPR

  • Syslog::LOG_MAIL

  • Syslog::LOG_NEWS

  • Syslog::LOG_SYSLOG

  • Syslog::LOG_USER (default)

  • Syslog::LOG_UUCP



61
62
63
64
65
66
67
68
# File 'lib/lumberjack_syslog_device.rb', line 61

def initialize(options = {})
  @template = options[:template] || default_template
  @template = Template.new(@template) if @template.is_a?(String)
  @syslog_options = options[:options] || (Syslog::LOG_PID | Syslog::LOG_CONS)
  @syslog_facility = options[:facility]
  @close_connection = options[:close_connection]
  @syslog_identity = nil
end

Instance Method Details

#closeObject



82
83
84
85
86
87
# File 'lib/lumberjack_syslog_device.rb', line 82

def close
  flush
  @lock.synchronize do
    @syslog.close if @syslog&.opened?
  end
end

#write(entry) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lumberjack_syslog_device.rb', line 70

def write(entry)
  message = @template.call(entry).to_s.gsub(PERCENT, ESCAPED_PERCENT)
  @@lock.synchronize do
    syslog = open_syslog(entry.progname)
    begin
      syslog.log(SEVERITY_MAP[entry.severity], message)
    ensure
      syslog.close if @close_connection
    end
  end
end