Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified syntax of logging methods according to those of Logger class #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/syslog-logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -158,4 +158,4 @@ def clean(message)
return message
end

end
end
44 changes: 42 additions & 2 deletions test/test_syslog_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down