diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-09-27 09:20:58 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-09-27 09:20:58 +0000 |
commit | e0bff65092ddd92058994feb33e69195fb216488 (patch) | |
tree | 2cc2468305fed447c57cc3853889020e14c8f8fc /ext/syslog | |
parent | 015af597e09a8fc938ed9562e29946da7fb0aa38 (diff) | |
download | ruby-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
Diffstat (limited to 'ext/syslog')
-rw-r--r-- | ext/syslog/lib/syslog/logger.rb | 46 |
1 files changed, 33 insertions, 13 deletions
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 |