Take the 2-minute tour ×

I'm having a problem with a boolean expression and when I did a logger.debug I had strange results, so I simplified my logging code to the following and was surprised not to see any 'false' being printed.

Logging code in my controller:

logger.debug 'true'
logger.debug true
logger.debug 'false'
logger.debug false
logger.debug '1 == 1'
logger.debug 1 == 1
logger.debug '1 == 0'
logger.debug 1 == 0

Which prints out the following



1 == 1

1 == 0

... ? I would expect to see false. When I run '1 == 0' or 'puts false' in the console I get false. Am I missing something?

Any ideas why isn't it printing the 'false'?

ruby version: 1.8.7-p352

rails version: 2.3.2

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Rails Logger uses || in the code before running .to_s, which fails for nil and false.


def add(severity, message = nil, progname = nil, &block)
  return if @level > severity
  message = (message || (block && block.call) || progname).to_s ## <- this line
  # If a newline is necessary then create a new message ending with a newline.
  # Ensures that the original message is not mutated.
  message = "#{message}\n" unless message[-1] == ?\n
  buffer << message

You can do this instead:

logger.debug( false.to_s)
share|improve this answer
'logger.debug (1 == 0).to_s' doesn't print anything. –  John MacIntyre Aug 15 '11 at 14:52
How about logger.debug((1 == 0).to_s)? –  Dogbert Aug 15 '11 at 14:52
add comment

Logger has got a condition somewhere (if value), which fails for nil and false. You want logger.debug value.inspect.

share|improve this answer
So it should be 'logger.debug (false).inspect' ? Do I understand you correctly? Because that also prints nothing. –  John MacIntyre Aug 15 '11 at 14:46
Never mind. 'logger.debug false.inspect' works fine. The parentheses were there for expressions. Thanks +1 –  John MacIntyre Aug 15 '11 at 14:48
no .. wait. '(1 == 0).inspect' doesn't print anything. –  John MacIntyre Aug 15 '11 at 14:50
For future reference 'logger.debug( (1 == 0).inspect)' works. –  John MacIntyre Aug 15 '11 at 14:55
add comment

Your Answer


