我为 Rails 编写了一个自定义格式化程序:
module Logging
class GeneralFormatter < Logger::Formatter
def call(severity, time, program_name, message)
...
end
end
end
并根据导轨导轨 http://guides.rubyonrails.org/configuring.html and 这个答案 https://stackoverflow.com/a/22382634/1103543,我所要做的就是使用它
config.log_formatter = Logging::GeneralFormatter.new
不幸的是,这似乎不起作用——我的自定义格式没有生效。但是,如果我也定义它:
module Logging
class GeneralLogger < ActiveSupport::Logger
def initialize(*args)
super(*args)
@formatter = GeneralFormatter.new
end
end
end
那我可以做config.logger = Logging::GeneralLogger.new
我的日志已根据需要格式化。
我的设置做错了什么log_formatter
?当我想要的只是自定义格式时,我宁愿不定义自己的记录器。
EDIT(回应评论,并通过更多的挖掘添加更多细节):
我正在设置config.log_formatter
in application.rb
它正在开发中进行测试,并且似乎正在发挥作用在一定程度上,在那个召唤中Rails.logger.formatter
给我我的定制课程。但这是我在打电话时看到的行为Rails.logger.warn("test")
:
-
test
被打印到控制台(后跟换行符)。
-
Then the
call
输入我的格式化程序的方法。
- 返回值
call
方法永远不会打印到控制台。
我认为会发生的事情(以及我想要发生的事情是):
- The
call
输入我的格式化程序的方法。
- 返回值
call
方法打印到控制台。
- 没有打印任何其他内容。
我是否误解了格式化程序的工作原理?
再次编辑(提供更清晰的例子):
当我定义了这个:
def call(severity, time, program_name, message)
puts "Checkpoint 1"
"Checkpoint 2"
end
并打电话Rails.logger.warn("Checkpoint 0")
,我期望看到:
Checkpoint 1
Checkpiont 2
但我看到的是:
Checkpoint 0
Checkpoint 1