class Syslogger
Constants
- LEVELS
- MAPPING
- MUTEX
Attributes
Public Class Methods
# File lib/syslogger/version.rb, line 5 def self.gem_version Gem::Version.new VERSION::STRING end
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 = Syslogger.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" logger.info "my_subapp" { "Some lazily computed message" }
# File lib/syslogger.rb, line 53 def initialize(ident = $PROGRAM_NAME, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) @ident = ident @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS) @facility = facility @level = Logger::INFO @formatter = SimpleFormatter.new end
Public Instance Methods
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. If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.
progname
-
optionally, overwrite the program name that appears in the log message.
# File lib/syslogger.rb, line 91 def add(severity, message = nil, progname = nil, &block) if message.nil? && block.nil? && !progname.nil? message, progname = progname, nil end progname ||= @ident mask = Syslog::LOG_UPTO(MAPPING[level]) communication = message || block && block.call formatted_communication = clean(formatter.call(severity, Time.now, progname, communication)) # Call Syslog syslog_add(progname, severity, mask, formatted_communication) end
Sets the minimum level for messages to be written in the log.
level
-
one of
Logger::DEBUG
,Logger::INFO
,Logger::WARN
,Logger::ERROR
,Logger::FATAL
,Logger::UNKNOWN
# File lib/syslogger.rb, line 106 def level=(level) @level = sanitize_level(level) end
Tagging code borrowed from ActiveSupport gem
# File lib/syslogger.rb, line 111 def tagged(*tags) formatter.tagged(*tags) { yield self } end
Protected Instance Methods
Borrowed from SyslogLogger.
# File lib/syslogger.rb, line 134 def clean(message) message = message.to_s.dup message.strip! # remove whitespace message.gsub!(/\n/, '\\n'.freeze) # escape newlines message.gsub!(/%/, '%%'.freeze) # syslog(3) freaks on % (printf) message.gsub!(/\e\[[^m]*m/, ''.freeze) # remove useless ansi color codes message end
# File lib/syslogger.rb, line 119 def sanitize_level(new_level) begin new_level = Logger.const_get(new_level.to_s.upcase) rescue => _ raise ArgumentError.new("Invalid logger level `#{new_level.inspect}`") end if new_level.is_a?(Symbol) unless new_level.is_a?(Integer) raise ArgumentError.new("Invalid logger level `#{new_level.inspect}`") end new_level end
Private Instance Methods
# File lib/syslogger.rb, line 145 def syslog_add(progname, severity, mask, formatted_communication) MUTEX.synchronize do Syslog.open(progname, @options, @facility) do |s| s.mask = mask if max_octets buffer = '' formatted_communication.bytes do |byte| buffer.concat(byte) # if the last byte we added is potentially part of an escape, we'll go ahead and add another byte if buffer.bytesize >= max_octets && !['%'.ord, '\\'.ord].include?(byte) s.log(MAPPING[severity], buffer) buffer = '' end end s.log(MAPPING[severity], buffer) unless buffer.empty? else s.log(MAPPING[severity], formatted_communication) end end end end