diff --git a/lib/syslog-logger.rb b/lib/syslog-logger.rb index 041a2ad..ebadfa0 100644 --- a/lib/syslog-logger.rb +++ b/lib/syslog-logger.rb @@ -52,13 +52,13 @@ class Logger::Syslog # Builds a methods for level +meth+. for severity in Logger::Severity.constants class_eval <<-EOT, __FILE__, __LINE__ - def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block) - add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block) - end # end - # - def #{severity.downcase}? # def debug? - @level <= #{severity} # @level <= DEBUG - end # end + def #{severity.downcase}(progname = nil, &block) # def debug(progname = nil, &block) + add(#{severity}, nil, progname, &block) # add(DEBUG, nil, progname, &block) + end # end + # + def #{severity.downcase}? # def debug? + @level <= #{severity} # @level <= DEBUG + end # end EOT end @@ -158,4 +158,4 @@ def clean(message) return message end -end \ No newline at end of file +end diff --git a/test/test_syslog_logger.rb b/test/test_syslog_logger.rb index d768f76..05c27a5 100644 --- a/test/test_syslog_logger.rb +++ b/test/test_syslog_logger.rb @@ -233,6 +233,10 @@ def test_unknown msg = log :unknown, 'unknown level message' assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity + assert_raise(ArgumentError) do + log :unknown, 'unknown level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :unknown, 'unknown level message' assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity @@ -270,6 +274,10 @@ def test_fatal msg = log :fatal, 'fatal level message' assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity + assert_raise(ArgumentError) do + log :fatal, 'fatal level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :fatal, 'fatal level message' assert_equal '', msg.line @@ -307,6 +315,10 @@ def test_error msg = log :error, 'error level message' assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity + assert_raise(ArgumentError) do + log :error, 'error level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :error, 'error level message' assert_equal '', msg.line @@ -344,6 +356,10 @@ def test_warn msg = log :warn, 'warn level message' assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity + assert_raise(ArgumentError) do + log :warn, 'warn level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :warn, 'warn level message' assert_equal '', msg.line @@ -381,6 +397,10 @@ def test_info msg = log :info, 'info level message' assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity + assert_raise(ArgumentError) do + log :info, 'info level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :info, 'info level message' assert_equal '', msg.line @@ -418,6 +438,10 @@ def test_debug msg = log :debug, 'debug level message' assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity + assert_raise(ArgumentError) do + log :debug, 'debug level message', :dummy_progname + end + @logger.level = Logger::UNKNOWN msg = log :debug, 'debug level message' assert_equal '', msg.line @@ -451,6 +475,22 @@ def test_debug_eh assert_equal false, @logger.debug? end + [:unknown, :fatal, :error, :warn, :info, :debug].each do |severity| + class_eval <<-EOS, __FILE__, __LINE__ + def test_#{severity}_block + msg = log :#{severity}, '#{severity} level message' + msg_blk = log(:#{severity}){ '#{severity} level message' } + [:severity, :msg].each do |attr| + assert_equal msg.send(attr), msg_blk.send(attr) + end + + msg_blk = log(:#{severity}, :dummy_progname){ '#{severity} level message' } + [:severity, :msg].each do |attr| + assert_equal msg.send(attr), msg_blk.send(attr) + end + end + EOS + end end class TestSyslogLogger < TestLogger @@ -464,8 +504,8 @@ class Log attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg def initialize(line) @line = line - return unless /\A(\w+) - (.*)\Z/ =~ @line - severity, @msg = $1, $2 + return unless /\A(\w+) - \[([^\]]*)\]\s+\[[^\]]+\]: ([\x0-\xff]*)/ =~ @line + severity, @datetime, @msg = $1, $2, $3 severity = Logger::Syslog::LOGGER_MAP.invert[severity.downcase.intern] @severity = severity.to_s.upcase @severity = 'ANY' if @severity == 'UNKNOWN'