我正在使用RoR 4.1.12和log4r gem来格式化日志。我认为一切正常,但如果我想改变日志级别,它根本就没有影响力。
我有以下config/log4r_init.rb
:
require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/fileoutputter'
require 'log4r/outputter/datefileoutputter'
# The problem is that Rails 4.2 (not sure about other versions at the moment) expects a method
# on the logger called formatter(). Log4r, however, defines formatter() on each outputter of
# the logger (i.e. so that each outputter can have a different format).
# We could monkey-patch it like this
Log4r::Logger.class_eval do
def formatter
# Give it the first formatter we have?
outputters[0].formatter if outputters.any?
end
end
# log4r.yml configuration is expected to be in the same directory as this file.
log4r_config = YAML.load(ERB.new(File.read(File.expand_path(File.dirname(__FILE__) + "/log4r.yml"))).result)
Log4r::YamlConfigurator.decode_yaml(log4r_config['log4r_config'])
V2p0::Application.config.logger = Log4r::Logger[Rails.env]
以及以下config/log4r.yml
:
log4r_config:
loggers:
- name : development
level : WARN
additive : 'false'
trace : 'true'
outputters:
- standard_plus_trace_log_outputter
- stderr_outputter
- name : test
level : DEBUG
additive : 'false'
trace : 'true'
outputters:
- standard_plus_trace_log_outputter
- name : staging
level : INFO
additive : 'false'
trace : 'true'
outputters:
- standard_plus_trace_log_outputter
- stderr_outputter
- name : production
level : INFO
additive : 'false'
trace : 'true'
outputters:
- standard_plus_trace_log_outputter
- stderr_outputter
outputters:
- type : StderrOutputter
name : stderr_outputter
level : INFO
formatter:
date_pattern: '%Y-%m-%d %H:%M:%S'
pattern : "[SXS] [%d] PID:%p %t %l %m"
type : PatternFormatter
- type : FileOutputter
name : standard_log_outputter
trunc : 'false'
filename : <%= "#{Rails.root}/log/#{Rails.env}.log" %>
formatter :
date_pattern: '%Y-%m-%d %H:%M:%S.%3N'
pattern : "[SXS] [%d] %l %m"
type : PatternFormatter
- type : FileOutputter
name : standard_plus_trace_log_outputter
trunc : 'false'
filename : <%= "#{Rails.root}/log/#{Rails.env}.log" %>
formatter :
date_pattern: '%Y-%m-%d %H:%M:%S.%3N'
pattern : "[SXS] [%d] %t %l %m"
type : PatternFormatter
即使在开发模式下该级别为WARN,我也会在错误输出上看到很多类似的日志消息:
[SXS] [2017-12-19 14:11:32] PID:947 /home/ntibor/.bundle/ruby/2.2.0/rails-9bfb9f2c3b8b/activesupport/lib/active_support/log_subscriber.rb:93:in `info' INFO Rendered shared/_common_scripts.html.erb (5.6ms)
有什么问题?