Class: BufferedSyslogger
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- BufferedSyslogger
- Defined in:
- lib/buffered_syslogger.rb
Constant Summary collapse
- MAPPING =
{ Logger::DEBUG => Syslog::LOG_DEBUG, Logger::INFO => Syslog::LOG_INFO, Logger::WARN => Syslog::LOG_NOTICE, Logger::ERROR => Syslog::LOG_WARNING, Logger::FATAL => Syslog::LOG_ERR, Logger::UNKNOWN => Syslog::LOG_ALERT }
Instance Attribute Summary collapse
-
#facility ⇒ Object
readonly
Returns the value of attribute facility.
-
#ident ⇒ Object
readonly
Returns the value of attribute ident.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Low level method to add a message.
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ BufferedSyslogger
constructor
- Initializes default options for the logger
ident
-
the name of your program [default=$0].
- Initializes default options for the logger
Constructor Details
#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ BufferedSyslogger
Initializes default options for the logger
ident
-
the name of your program [default=$0].
options
-
syslog options [default=
Syslog::LOG_PID | Syslog::LOG_CONS
]. Correct values are:LOG_CONS : writes the message on the console if an error occurs when sending the message; LOG_NDELAY : no delay before sending the message; LOG_PERROR : messages will also be written on STDERR; LOG_PID : adds the process number to the message (just after the program name)
facility
-
the syslog facility [default=nil] Correct values include:
Syslog::LOG_DAEMON Syslog::LOG_USER Syslog::LOG_SYSLOG Syslog::LOG_LOCAL2 Syslog::LOG_NEWS etc.
Usage:
logger = BufferedSyslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
logger.level = Logger::INFO # use Logger levels
logger.warn "warning message"
logger.debug "debug message"
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/buffered_syslogger.rb', line 40 def initialize(ident = $0, = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) @ident = ident @options = || (Syslog::LOG_PID | Syslog::LOG_CONS) @facility = facility @level = Logger::INFO @buffer = {} @auto_flushing = 1 @guard = Mutex.new end |
Instance Attribute Details
#facility ⇒ Object (readonly)
Returns the value of attribute facility.
6 7 8 |
# File 'lib/buffered_syslogger.rb', line 6 def facility @facility end |
#ident ⇒ Object (readonly)
Returns the value of attribute ident.
6 7 8 |
# File 'lib/buffered_syslogger.rb', line 6 def ident @ident end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/buffered_syslogger.rb', line 6 def @options end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Low level method to add a message.
severity
-
the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
message
-
the message string. If nil, the method will call the block and use the result as the message string.
progname
-
unsupported, kept for compatibility
55 56 57 58 59 60 61 |
# File 'lib/buffered_syslogger.rb', line 55 def add(severity, = nil, progname = nil, &block) return if @level > severity ( || (block && block.call) || progname).to_s.chomp.tap do |m| buffer << [severity, m] auto_flush end end |
#close ⇒ Object
81 82 83 |
# File 'lib/buffered_syslogger.rb', line 81 def close flush end |
#flush ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/buffered_syslogger.rb', line 63 def flush @guard.synchronize do unless buffer.empty? old_buffer = buffer Syslog.open(@ident, @options, @facility) do |s| s.mask = Syslog::LOG_UPTO(MAPPING[@level]) old_buffer.each do |severity, | .split(/[\r\f\n]/).each { |m| s.log(MAPPING[severity], m.gsub('%', '%%')) } end end end # Important to do this even if buffer was empty or else @buffer will # accumulate empty arrays for each request where nothing was logged. clear_buffer end end |