Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[DO NOT MERGE] Compatibility with stdlib logger when using block arguments. #3

Closed
wants to merge 1 commit into from

1 participant

@chancancode

The change concerning this PR is in 1938f23, the rest are just the lost commits (see #2). Once #2 is resolved, I will rebase this and possibly rework it w.r.t. any logger API changes in Ruby 2.0.

@chancancode chancancode closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 3, 2013
  1. @chancancode
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 4 deletions.
  1. +7 −1 lib/syslog/logger.rb
  2. +15 −3 test/test_syslog_logger.rb
View
8 lib/syslog/logger.rb
@@ -186,8 +186,14 @@ def initialize program_name = 'ruby'
def add severity, message = nil, progname = nil, &block
severity ||= ::Logger::UNKNOWN
+
+ if progname.nil? && block_given?
+ progname = message
+ message = block.call
+ end
+
@level <= severity and
- @@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, (message || block.call))
+ @@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, message)
true
end
end
View
18 test/test_syslog_logger.rb
@@ -65,12 +65,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
@@ -468,6 +468,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 if defined?(Syslog)
class TestSyslogLogger < TestSyslogRootLogger
@@ -495,7 +507,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
Something went wrong with that request. Please try again.