Class: Lumberjack::SyslogDevice
- Inherits:
-
Device
- Object
- Device
- Lumberjack::SyslogDevice
- 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
- VERSION =
::File.read(::File.join(__dir__, "..", "..", "VERSION")).strip.freeze
- SEVERITY_MAP =
Mapping of Lumberjack severity levels to syslog priority levels
{ Severity::TRACE => Syslog::LOG_DEBUG, 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 =
Literal percent character
"%"- ESCAPED_PERCENT =
Escaped percent character for syslog format strings
"%%"- DEFAULT_TEMPLATE =
Default template for formatting log messages
"{{message}} {{attributes}}"- DEFAULT_ATTRIBUTE_FORMAT =
Default format for rendering log entry attributes
"[%s:%s]"- @@lock =
Mutex.new
Instance Method Summary collapse
-
#close ⇒ void
Close the syslog connection.
-
#initialize(options = {}) ⇒ SyslogDevice
constructor
Create a new SyslogDevice.
-
#write(entry) ⇒ void
Write a log entry to syslog.
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}} {{attributes}}.
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
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lumberjack/syslog_device.rb', line 76 def initialize( = {}) @template = [:template] || DEFAULT_TEMPLATE attribute_format = [:attribute_format] || DEFAULT_ATTRIBUTE_FORMAT @template = Template.new(@template, attribute_format: attribute_format) if @template.is_a?(String) @syslog_options = [:options] || (Syslog::LOG_PID | Syslog::LOG_CONS) # Syslog substitutes LOG_USER for a nil facility; normalize here so the # connection reuse check in open_syslog matches what syslog reports back. @syslog_facility = [:facility] || Syslog::LOG_USER @close_connection = [:close_connection] end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the syslog connection.
108 109 110 111 112 113 |
# File 'lib/lumberjack/syslog_device.rb', line 108 def close flush @@lock.synchronize do syslog_implementation.close if syslog_implementation.opened? end end |
#write(entry) ⇒ void
This method returns an undefined value.
Write a log entry to syslog.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lumberjack/syslog_device.rb', line 92 def write(entry) = @template.call(entry).to_s.chomp.gsub(PERCENT, ESCAPED_PERCENT) severity = SEVERITY_MAP[entry.severity] || Syslog::LOG_ALERT @@lock.synchronize do syslog = open_syslog(entry.progname) begin syslog.log(severity, ) ensure syslog.close if @close_connection end end end |