在生产模式下登录Ruby on Rails

use*_*541 41 logging production ruby-on-rails

我想在控制器中查看一些变量,它尝试了以下内容:

Rails.logger.debug "Year: #{Time.now.year}"

puts "Year: #{Time.now.year}, Month: #{@month}"

在哪里可以看到Logger或Puts在生产模式下的输出?我需要这样设置一些东西来查看这些吗?

Mar*_*n M 105

生产中的正常日志级别是info,因此debug不显示日志.
将日志记录更改为

Rails.logger.info "Year: #{Time.now.year}"
Run Code Online (Sandbox Code Playgroud)

展示它production.log.

或者(但不是一个好主意)您可以提高日志记录级别/config/environments/production.rb:

config.log_level = :debug
Run Code Online (Sandbox Code Playgroud)

更新 Rails 4.2:

现在所有环境中的默认调试级别都是:debug(如@nabilh所述).
如果您希望生产环境减少聊天,可以将日志级别重置/config/environments/production.rb为前者:info:

config.log_level = :info
Run Code Online (Sandbox Code Playgroud)


nab*_*ilh 8

虽然我认为@ martin-m是正确的,你可能不想通过使用config.log_level = :debugin 来混乱你的日志/config/environments/production.rb,但我相信所有环境中的默认日志记录级别都是debug从Rails 4.2开始.因此debug,除非另行指定,否则日志(以及所有级别)将在生产中显示.

所以在回答你的问题时,你现在可以写:

Rails.logger.debug "Year: #{Time.now.year}"并查看结果/log/production.log.

有关详细文档,请参见此处.以下是Rails 4.1的文档以供比较.