Skip to content

Commit

Permalink
Logger: don't yield to the block on nil logger. Related to #3065
Browse files Browse the repository at this point in the history
Ary Borenszweig committed Jul 29, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent e99cee2 commit ea19ae2
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions spec/std/logger_spec.cr
Original file line number Diff line number Diff line change
@@ -79,4 +79,11 @@ describe "Logger" do
logger = Logger.new(nil)
logger.error("ouch")
end

it "doesn't yield to the block with nil" do
a = 0
logger = Logger.new(nil)
logger.info { a = 1 }
a.should eq(0)
end
end
4 changes: 2 additions & 2 deletions src/logger.cr
Original file line number Diff line number Diff line change
@@ -115,15 +115,15 @@ class Logger
# Logs *message* if *severity* is higher or equal with the logger's current
# severity. *progname* overrides a default progname set in this logger.
def log(severity, message, progname = nil)
return if severity < level
return if severity < level || !@io
write(severity, Time.now, progname || @progname, message)
end

# Logs the message as returned from the given block if *severity*
# is higher or equal with the loggers current severity. The block is not run
# if *severity* is lower. *progname* overrides a default progname set in this logger.
def log(severity, progname = nil)
return if severity < level
return if severity < level || !@io
write(severity, Time.now, progname || @progname, yield)
end

0 comments on commit ea19ae2

Please sign in to comment.