在生产模式下登录 Ruby on Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16824355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging in Ruby on Rails in Production Mode
提问by user1135541
I would like to view some variables in controller, it tried the following:
我想查看控制器中的一些变量,它尝试了以下操作:
Rails.logger.debug "Year: #{Time.now.year}"
Rails.logger.debug "Year: #{Time.now.year}"
puts "Year: #{Time.now.year}, Month: #{@month}"
puts "Year: #{Time.now.year}, Month: #{@month}"
where can I see the output for Logger or Puts in production mode? Do I need so set something up to view these somewhere?
我在哪里可以看到生产模式下 Logger 或 Puts 的输出?我需要设置一些东西才能在某处查看这些吗?
回答by Martin M
The normal log level in production is info, so debuglogs are not shown.
Change your logging to
生产中的正常日志级别是info,因此debug不显示日志。
将您的日志记录更改为
Rails.logger.info "Year: #{Time.now.year}"
to show it in production.log.
在production.log.
Alternatively (but not a good idea) you can raise the logging level in /config/environments/production.rb:
或者(但不是一个好主意)您可以在/config/environments/production.rb以下位置提高日志记录级别:
config.log_level = :debug
UpdateRails 4.2:
更新Rails 4.2:
Now the default debug level in all environments is :debug(as @nabilh mentioned).
If you want you production environment less chattery, you can reset your log level in /config/environments/production.rbto the former :info:
现在所有环境中的默认调试级别是:debug(如@nabilh 所述)。
如果你想让你的生产环境少喋喋不休,你可以将你的日志级别重置/config/environments/production.rb为前者:info:
config.log_level = :info
回答by nabilh
While I believe @martin-m is right that you probably don't want to clutter your logs by using config.log_level = :debugin /config/environments/production.rb, I believe the default logging level in allenvironments is debugas of Rails 4.2. So debuglogs (and all levels up) will be shown in production unless you specify otherwise.
虽然我相信 @martin-m 是正确的,您可能不想使用config.log_level = :debugin弄乱您的日志/config/environments/production.rb,但我相信所有环境中的默认日志记录级别都是debug从 Rails 4.2 开始的。因此debug,除非您另行指定,否则日志(以及所有级别)将在生产中显示。
So in response to your question, you can now write:
所以为了回答你的问题,你现在可以写:
Rails.logger.debug "Year: #{Time.now.year}"and see the results in /log/production.log.
Rails.logger.debug "Year: #{Time.now.year}"并查看结果/log/production.log。
See herefor more detailed documentation. Hereis the documentation for Rails 4.1 for comparison.

