加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为什么’logger.debug false’没有打印任何东

发布时间:2020-12-17 01:28:31 所属栏目:百科 来源:网络整理
导读:我有一个布尔表达式的问题,当我做了一个logger.debug我得到了奇怪的结果,所以我将我的日志代码简化为以下内容,并且惊讶地看不到任何’false’被打印. 在我的控制器中记录代码: logger.debug 'true'logger.debug truelogger.debuglogger.debug 'false'logger
我有一个布尔表达式的问题,当我做了一个logger.debug我得到了奇怪的结果,所以我将我的日志代码简化为以下内容,并且惊讶地看不到任何’false’被打印.

在我的控制器中记录代码:

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

其中打印出以下内容

true
true

false


1 == 1
true

1 == 0

……?我希望看到错误.当我在控制台中运行’1 == 0’或’put false’时,我得到了错误.我错过了什么吗?

任何想法为什么不打印’假’?

ruby版本:1.8.7-p352

rails版本:2.3.2

解决方法

Rails Logger使用||在运行.to_s之前的代码中,对于nil和false失败.

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/buffered_logger.rb#L65

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
  auto_flush
  message
end

你可以这样做:

logger.debug( false.to_s)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读