在 Ruby on Rails 中设置日志记录级别 [英] Set logging levels in Ruby on Rails
问题描述
我正在使用以下代码在我的 Ruby on Rails 应用程序中配置日志记录:
I'm using following code to configure logging in my Ruby on Rails application:
环境.rb:
Rails.logger = Logger.new(STDOUT)
class Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}
"
end
end
我正在尝试将日志记录级别设置为现在使用
I'm trying to set the logging level to warn now using
config.log_level = :warn
在我的 production.rb 中,但它似乎不起作用.我在这里遗漏了什么吗?
in my production.rb, but it doens't seem to work. Am I missing something here?
如果我将 Rails.logger.level = 4 放在我的 environment.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.
推荐答案
根据官方文档,你应该使用:
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
这篇关于在 Ruby on Rails 中设置日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!