lib/syslogger.rb in syslogger-1.2.3 vs lib/syslogger.rb in syslogger-1.2.4
- old
+ new
@@ -1,11 +1,11 @@
require 'syslog'
require 'logger'
class Syslogger
- VERSION = "1.2.3"
+ VERSION = "1.2.4"
attr_reader :level, :ident, :options, :facility
MAPPING = {
Logger::DEBUG => Syslog::LOG_DEBUG,
@@ -63,17 +63,22 @@
add(Logger::INFO, msg)
end
# 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+:: optionally, a overwrite the program name that appears in the log message.
+ # +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.
def add(severity, message = nil, progname = nil, &block)
progname ||= @ident
Syslog.open(progname, @options, @facility) { |s|
s.mask = Syslog::LOG_UPTO(MAPPING[@level])
- s.log(MAPPING[severity], clean(message || block.call))
+ s.log(
+ MAPPING[severity],
+ clean(message || (block && block.call) || progname)
+ )
}
end
# Sets the minimum level for messages to be written in the log.
# +level+:: one of <tt>Logger::DEBUG</tt>, <tt>Logger::INFO</tt>, <tt>Logger::WARN</tt>, <tt>Logger::ERROR</tt>, <tt>Logger::FATAL</tt>, <tt>Logger::UNKNOWN</tt>
@@ -83,9 +88,12 @@
protected
# Borrowed from SyslogLogger.
def clean(message)
- # syslog(3) freaks on % (printf)
- message.strip.gsub(/%/, '%%')
+ message = message.to_s.dup
+ message.strip!
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
+ message
end
end