在 Rails 中启用自定义格式化程序 [英] Enabling a custom formatter in Rails

查看:48
本文介绍了在 Rails 中启用自定义格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Rails 编写了一个自定义格式化程序:

I've written a custom formatter for Rails:

module Logging
  class GeneralFormatter < Logger::Formatter
    def call(severity, time, program_name, message)
      ...
    end
  end
end

并根据 Rails 指南这个答案,我所要做的就是使用它

And according to the Rails guide and this answer, all I have to do to use it is set

config.log_formatter = Logging::GeneralFormatter.new

不幸的是,这似乎不起作用——我的自定义格式没有生效.然而,如果我也定义它:

Unfortunately, this doesn't seem to work—my custom formatting isn't kicking in. And yet, if I instead define this as well:

module Logging
  class GeneralLogger < ActiveSupport::Logger
    def initialize(*args)
      super(*args)
      @formatter = GeneralFormatter.new
    end
  end
end

然后我可以执行 config.logger = Logging::GeneralLogger.new 并且我的日志按需要格式化.

Then I can do config.logger = Logging::GeneralLogger.new and my logs are formatted as desired.

我在设置 log_formatter 时做错了什么?当我想要的只是自定义格式时,我宁愿不定义自己的 Logger.

What am I doing wrong with setting log_formatter? I'd rather not define my own Logger when all I want is custom formatting.

编辑(回复评论,并通过更多挖掘添加更多细节):

EDIT (responding to comments, and adding more detail from a little more digging):

我正在 application.rb 中设置 config.log_formatter 并在开发中进行测试,它似乎在一定程度上,在那个调用 Rails.logger.formatter 给了我我的自定义类.但这是我在调用 Rails.logger.warn("test") 时看到的行为:

I'm setting config.log_formatter in application.rb and it testing in development, and it appears to be working to an extent, in that calling Rails.logger.formatter gives me my custom class. But here's the behavior I'm seeing on calling Rails.logger.warn("test"):

  1. test 打印到控制台(后跟换行符).
  2. 然后进入我的格式化程序的call方法.
  3. call 方法的返回值永远不会打印到控制台.
  1. test is printed out to the console (followed by a newline).
  2. Then the call method of my formatter is entered.
  3. The return value of the call method is never printed out to the console.

我认为会发生的事情(以及我想要发生的事情是):

Where what I thought would happen (and what I want to happen is):

  1. 输入了我的格式化程序的 call 方法.
  2. call 方法的返回值打印到控制台.
  3. 没有打印任何其他内容.
  1. The call method of my formatter is entered.
  2. The return value of the call method is printed out to the console.
  3. Nothing else is printed.

我是否误解了格式化程序的工作原理?

Am I misunderstanding how formatters work?

再次编辑(提供更清晰的示例):

EDIT Again (providing clearer example):

当我定义了这个:

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

推荐答案

好的,我们再试一次.

我实际上没有答案——相反,我无法重现您的问题.我会给你我采取的步骤和我所看到的,也许你将能够与你所做的进行比较,看看它有什么不同.

I don't actually have an answer -- instead, I am unable to reproduce your problem. I will give you the steps I took and what I saw, perhaps you will be able to compare to what you've done to see how it's different.

  • 你没有说你有什么版本的 Rails,我从最新的 4.1.1 开始.构建了一个全新的应用程序 rails new logtest.

./lib/custom_formatter.rb 添加一个文件,如下所示:

Added a file at ./lib/custom_formatter.rb that looks like this:

class CustomFormatter < Logger::Formatter
  def call(severity, time, program_name, message)
    # I think it makes no sense to have a raw `puts`
    # in a log formatter, but I'll leave it in to
    # copy you. Almost certainly a bad idea though. 
    # commented out. 
    puts "Checkpoint 1"
    return "Checkpoint 2"
  end
end

将此添加到我现有的config/application.rb:

require 'custom_formatter'
config.log_formatter = CustomFormatter.new

现在我使用 rails server 启动应用程序,并访问 http://localhost:3000/test.我希望收到一条错误消息,因为我还没有定义任何路由或控制器或任何东西.但是由于我奇怪的自定义记录器,我希望所有日志行基本上都被检查点 1"和检查点 2"替换.确实是这么回事.我的控制台看起来像这样:

Now I start up the application with rails server, and access http://localhost:3000/test. I expect to get an error message, because I haven't defined any routes or controllers or anything. But because of my weird custom logger, I expect all the log lines to be essentially replaced by "Checkpoint 1" and "Checkpoint 2". Indeed that's what's happened. My console looks like this:

  [2014-06-03 18:22:49] INFO  WEBrick 1.3.1
  [2014-06-03 18:22:49] INFO  ruby 1.9.3 (2013-02-22) [x86_64-darwin12.2.1]
  [2014-06-03 18:22:49] INFO  WEBrick::HTTPServer#start: pid=57113 port=3000
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1
  Checkpoint 1
  Checkpoint 2Checkpoint 1

我的 development.log 如下所示:

My development.log looks like this:

  Checkpoint 2Checkpoint 2Checkpoint 2Checkpoint 2Checkpoint 2Checkpoint 2Checkpoint 2

很明显,自定义格式化程序"没有做任何有用的事情——我强烈建议不要在真正的格式化程序中放置直的放置"行.

So clearly that custom 'formatter' isn't doing anything useful -- and I strongly recommend against putting a straight 'puts' line in a real formatter.

但我的自定义格式化程序确实正在使用,所有日志行都被格式化"为固定字符串检查点 2"(末尾没有换行符!),并且额外的检查点 1"也被放入标准输出.

But my custom formatter is indeed being used, all log lines are 'formatted' as the fixed string 'Checkpoint 2' (with no newline at the end!), and the additional "Checkpoint 1" is also puts to stdout.

因此,按照您提供的步骤无法重现您的问题,如果我尝试按照您所说的去做,我就没有您的问题.所以,这确实是调试的工作方式,令人沮丧!我建议你按照我上面的步骤创建一个测试应用程序,看看它是否也适合你.然后你必须将你的实际应用程序与那个小测试应用程序进行比较,找出它的不同之处,以在你的实际应用程序中体现问题.一种常见的方法是复制您的实际应用程序,并开始剥离所有可能的内容,以获得仍然存在问题的最简单的应用程序.很可能,在这样做时,您会自己找出问题的原因——但如果不这样做,您可以将您的基本演示应用程序提供给其他试图帮助您的人,他们可能会弄清楚是什么在它导致问题.

So your problem is not reproducible by the steps you've given, if I try to do what you say you've done, I dont' have your problem. So, this is indeed how debugging works, it is frustrating! I suggest you follow my steps to create a test app above and see if it works for you too. Then you've got to compare your actual app to that little test app and figure out how it's different, to manifest the problem in your actual app. One common way to do this is make a copy of your actual app, and begin stripping out everything you can, to get the barest possible app that still manifests the problem. Odds are, in doing this, you'll figure out yourself the cause of the problem -- but if you don't, you can make your bare bones demonstration app available to others trying to help you, and they can maybe figure out what in it is causing the problem.

这篇关于在 Rails 中启用自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆