在 Ruby on Rails 中设置日志记录级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12180690/
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
Set logging levels in Ruby on Rails
提问by Lieven Cardoen
I'm using following code to configure logging in my Ruby on Rails application:
我正在使用以下代码在我的 Ruby on Rails 应用程序中配置日志记录:
environment.rb:
环境.rb:
Rails.logger = Logger.new(STDOUT)
class Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
I'm trying to set the logging level to warn now using
我正在尝试将日志记录级别设置为现在使用
config.log_level = :warn
in my production.rb, but it doens't seem to work. Am I missing something here?
在我的 production.rb 中,但它似乎不起作用。我在这里错过了什么吗?
If I put Rails.logger.level = 4 in my environment.rb, it does seem to work. But I would like to configure things in my environment initializers.
如果我将 Rails.logger.level = 4 放在我的 environment.rb 中,它似乎确实有效。但我想在我的环境初始值设定项中配置一些东西。
回答by Brad Werth
According to the official documentation, you should be using:
根据官方文档,您应该使用:
config.log_level = :warn # In any environment initializer, or
Rails.logger.level = 0 # at any time
If neither of those work for you, try:
如果这些都不适合您,请尝试:
config.log_level = Logger::WARN
And if that doesn't work, try:
如果这不起作用,请尝试:
config.logger.level = Logger::WARN
Note:The final method appears to be conflating the two official strategies, but works in some situations
注意:最后一种方法似乎将两种官方策略混为一谈,但在某些情况下有效
回答by Besi
For Rails 4.0 you can set the value in production.rb(or your desired environment) like so:
对于 Rails 4.0,您可以在production.rb(或您想要的环境)中设置值,如下所示:
# Set to :debug to see everything in the log.
config.log_level = :warn
UpdateThe docsstate it like so:
更新文档如下所示:
2.2 Log Levels
When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the
Rails.logger.levelmethod.The available log levels are:
:debug,:info,:warn,:error,:fatal, and:unknown, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, useconfig.log_level = :warn # In any environment initializer, or Rails.logger.level = 0 # at any timeThis is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
The default Rails log level is
infoin production mode and debug in development and test mode.
2.2 日志级别
记录某些内容时,如果消息的日志级别等于或高于配置的日志级别,则会将其打印到相应的日志中。如果您想知道当前的日志级别,可以调用该
Rails.logger.level方法。可用的日志级别有:
:debug、:info、:warn、:error、:fatal和:unknown,分别对应从 0 到 5 的日志级别编号。要更改默认日志级别,请使用config.log_level = :warn # In any environment initializer, or Rails.logger.level = 0 # at any time当您想在开发或暂存时记录日志,但又不想用不必要的信息淹没生产日志时,这很有用。
默认的 Rails 日志级别
info处于生产模式,调试处于开发和测试模式。

