summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-09-27 09:20:58 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-09-27 09:20:58 +0000
commite0bff65092ddd92058994feb33e69195fb216488 (patch)
tree2cc2468305fed447c57cc3853889020e14c8f8fc
parent015af597e09a8fc938ed9562e29946da7fb0aa38 (diff)
downloadruby-e0bff65092ddd92058994feb33e69195fb216488.tar.gz
* 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
-rw-r--r--ChangeLog6
-rw-r--r--ext/syslog/lib/syslog/logger.rb46
-rw-r--r--test/syslog/test_syslog_logger.rb12
3 files changed, 50 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index 05038f5a43..da7cf89a52 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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?'
diff --git a/ext/syslog/lib/syslog/logger.rb b/ext/syslog/lib/syslog/logger.rb
index 10c6b590c6..a8d089a094 100644
--- a/ext/syslog/lib/syslog/logger.rb
+++ b/ext/syslog/lib/syslog/logger.rb
@@ -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
diff --git a/test/syslog/test_syslog_logger.rb b/test/syslog/test_syslog_logger.rb
index 4c5241f639..3a659cfb84 100644
--- a/test/syslog/test_syslog_logger.rb
+++ b/test/syslog/test_syslog_logger.rb
@@ -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