Class: VCAP::Logging::Sink::SyslogSink
- Defined in:
- lib/vcap/logging/sink/syslog_sink.rb
Overview
A sink for logging messages to the local syslog server. NB: The ruby syslog module is a thin wrapper around glibc syslog(). It will first
attempt to open a unix stream socket to '/dev/log', and upon failure will attempt
to open a unix datagram socket there. Make sure you configure your syslog server
to use the appropriate type (probably dgram in our case).
Beware that all messages will be silently lost if the syslog server goes away.
Constant Summary collapse
- DEFAULT_LOG_LEVEL_MAP =
{ :fatal => Syslog::LOG_CRIT, :error => Syslog::LOG_ERR, :warn => Syslog::LOG_WARNING, :info => Syslog::LOG_INFO, :debug => Syslog::LOG_DEBUG, :debug1 => Syslog::LOG_DEBUG, :debug2 => Syslog::LOG_DEBUG, }
Instance Attribute Summary
Attributes inherited from BaseSink
#autoflush, #formatter, #opened
Instance Method Summary collapse
- #add_record(log_record) ⇒ Object
- #close ⇒ Object
-
#initialize(prog_name, opts = {}) ⇒ SyslogSink
constructor
A new instance of SyslogSink.
- #open ⇒ Object
Methods inherited from BaseSink
Constructor Details
#initialize(prog_name, opts = {}) ⇒ SyslogSink
Returns a new instance of SyslogSink.
29 30 31 32 33 34 35 36 |
# File 'lib/vcap/logging/sink/syslog_sink.rb', line 29 def initialize(prog_name, opts={}) super(opts[:formatter]) @prog_name = prog_name @log_level_map = opts[:log_level_map] || DEFAULT_LOG_LEVEL_MAP @syslog = nil open end |
Instance Method Details
#add_record(log_record) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/vcap/logging/sink/syslog_sink.rb', line 57 def add_record(log_record) raise UsageError, "You cannot add a record until the sink has been opened" unless @opened raise UsageError, "You must supply a formatter" unless @formatter = @formatter.format_record(log_record) pri = @log_level_map[log_record.log_level] @mutex.synchronize { @syslog.log(pri, '%s', ) } end |
#close ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/vcap/logging/sink/syslog_sink.rb', line 47 def close @mutex.synchronize do if @opened @syslog.close @syslog = nil @opened = false end end end |
#open ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/vcap/logging/sink/syslog_sink.rb', line 38 def open @mutex.synchronize do unless @opened @syslog = Syslog.open(@prog_name, Syslog::LOG_PID, Syslog::LOG_USER) @opened = true end end end |