1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/syslog/lib/syslog/logger.rb: add a formatter to the

Syslog::Logger object. [Bug #7065]
* test/syslog/test_syslog_logger.rb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2012-09-27 09:20:58 +00:00
parent 015af597e0
commit e0bff65092
3 changed files with 50 additions and 14 deletions

View file

@ -1,3 +1,9 @@
Thu Sep 27 18:12:20 2012 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/syslog/lib/syslog/logger.rb: add a formatter to the
Syslog::Logger object. [Bug #7065]
* test/syslog/test_syslog_logger.rb: ditto.
Wed Sep 26 16:39:57 2012 Koichi Sasada <ko1@atdot.net>
* insns.def: add new instruction `opt_empty_p' for optimize `empty?'

View file

@ -37,6 +37,23 @@ require 'logger'
# newsyslog.conf(5) and newsyslog(8) man pages.
class Syslog::Logger
# Default formatter for log messages.
class Formatter
def call severity, time, progname, msg
clean msg
end
private
##
# Clean up messages so they're nice and pretty.
def clean message
message = message.to_s.strip
message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
return message
end
end
##
# The version of Syslog::Logger you are using.
@ -136,6 +153,20 @@ class Syslog::Logger
attr_accessor :level
# Logging formatter, as a +Proc+ that will take four arguments and
# return the formatted message. The arguments are:
#
# +severity+:: The Severity of the log message.
# +time+:: A Time instance representing when the message was logged.
# +progname+:: The #progname configured, or passed to the logger method.
# +msg+:: The _Object_ the user passed to the log message; not necessarily a
# String.
#
# The block should return an Object that can be written to the logging
# device via +write+. The default formatter is used when no formatter is
# set.
attr_accessor :formatter
##
# Fills in variables for Logger compatibility. If this is the first
# instance of Syslog::Logger, +program_name+ may be set to change the logged
@ -145,6 +176,7 @@ class Syslog::Logger
def initialize program_name = 'ruby'
@level = ::Logger::DEBUG
@formatter = Formatter.new
@@syslog ||= Syslog.open(program_name)
end
@ -155,20 +187,8 @@ class Syslog::Logger
def add severity, message = nil, progname = nil, &block
severity ||= ::Logger::UNKNOWN
@level <= severity and
@@syslog.log LEVEL_MAP[severity], '%s', clean(message || block.call)
@@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, (message || block.call))
true
end
private
##
# Clean up messages so they're nice and pretty.
def clean message
message = message.to_s.strip
message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
return message
end
end

View file

@ -29,7 +29,7 @@ class TestSyslogRootLogger < Test::Unit::TestCase
end
def log(level, format, *args)
@line = "#{LEVEL_LABEL_MAP[level]} - \#{format % args}"
@line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
end
attr_reader :line
@ -92,6 +92,16 @@ class TestSyslogRootLogger < Test::Unit::TestCase
assert_equal Logger::DEBUG, @logger.level
end
def test_custom_formatter
@logger.formatter = Class.new {
def call severity, time, progname, msg
"hi mom!"
end
}.new
assert_match(/hi mom!/, log_raw(:fatal, 'fatal level message'))
end
def test_add
msg = log_add nil, 'unknown level message' # nil == unknown
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity