Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed incompatibility with stock logger when using blocks

  • Loading branch information...
commit 1938f236feb583697c3ffcab98327c1a1b50dd5a 1 parent 8a68c88
@chancancode chancancode authored
Showing with 27 additions and 8 deletions.
  1. +12 −5 lib/syslog_logger.rb
  2. +15 −3 test/test_syslog_logger.rb
View
17 lib/syslog_logger.rb
@@ -119,10 +119,8 @@ class SyslogLogger
def self.make_methods(meth)
eval <<-EOM, nil, __FILE__, __LINE__ + 1
- def #{meth}(message = nil)
- return true if #{LOGGER_LEVEL_MAP[meth]} < @level
- SYSLOG.#{LOGGER_MAP[meth]} clean(message || yield)
- return true
+ def #{meth}(progname = nil, &block)
+ add(Logger::#{meth.upcase}, nil, progname, &block)
end
def #{meth}?
@@ -196,7 +194,16 @@ def initialize(program_name = 'rails')
def add(severity, message = nil, progname = nil, &block)
severity ||= Logger::UNKNOWN
return true if severity < @level
- SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message || block.call)
+
+ if message.nil?
+ if block_given?
+ message = yield
+ else
+ message = progname
+ end
+ end
+
+ SYSLOG.send LEVEL_LOGGER_MAP[severity], clean(message)
return true
end
View
18 test/test_syslog_logger.rb
@@ -50,12 +50,12 @@ class Log
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
def initialize(line)
@line = line
- /\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
+ /\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)\n\z/ =~ @line
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
end
end
- def log_add(severity, msg, progname = nil, &block)
+ def log_add(severity, msg = nil, progname = nil, &block)
log(:add, severity, msg, progname, &block)
end
@@ -443,6 +443,18 @@ def test_debug_eh
assert_equal false, @logger.debug?
end
+ def test_blocks
+ message = 'A message logged from within a block'
+ msg = log(:debug) { message }
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
+ assert_equal message, msg.msg
+
+ # Test compatibility with logger.debug('progname') { 'a message' }
+ msg = log(:debug, 'progname') { message }
+ assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
+ assert_equal message, msg.msg
+ end
+
end
class TestSyslogLogger < TestLogger
@@ -464,7 +476,7 @@ def initialize(line)
end
end
- def log_add(severity, msg, progname = nil, &block)
+ def log_add(severity, msg = nil, progname = nil, &block)
log(:add, severity, msg, progname, &block)
end
Please sign in to comment.
Something went wrong with that request. Please try again.