Rails.logger神秘行为

时间:2017-04-04 18:54:16

标签: ruby-on-rails logging activerecord

我正在玩Rails登录,我发现了一件奇怪的事 我最初试图理解的一个问题是ActiveRecord如何将SQL查询记录到控制台。

irb(main):001:0> Message.last
  Message Load (7.4ms)  SELECT  "messages".* FROM "messages"  ORDER BY "messages"."id" DESC LIMIT 1

我发现输出是here。 但我无法理解ActiveRecord::Base.logger#debug如何同时打印STDOUTlog/development.log

我创建了新的Rails 5.0.2应用程序并运行控制台。

然后我做

irb(main):001:0> Rails.logger.debug 'foo bar'
foo bar
=> true

我在控制台和log/development.log文件中都有 foo bar 文字。

Rails.logger看起来像这样

#<ActiveSupport::Logger:0x007f9545860150 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f9545860088 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007f95430b7d10 @datetime_format=nil, @thread_key="activesupport_tagged_logging_tags:70139525840520">, @logdev=#<Logger::LogDevice:0x007f9545861820 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/Users/aleksey/projects/test-logger/log/development.log>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f9545863080>>, @local_levels=#<Concurrent::Map:0x007f9545893de8 entries=0 default_proc=nil>>

但是如果我创建自定义记录器,我只能在日志文件中获取文本

irb(main):005:0* logger = ActiveSupport::Logger.new('log/development.log')
=> #<ActiveSupport::Logger:0x007f9543411588 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f9543411510 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007f9543411330 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007f95434114c0 @shift_size=1048576, @shift_age=0, @filename="log/development.log", @dev=#<File:log/development.log>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f9543411448>>, @local_levels=#<Concurrent::Map:0x007f9543411290 entries=0 default_proc=nil>>
irb(main):006:0> logger.debug 'foo bar'
=> true

我在Rails.logger和自定义记录器之间没有看到太多差异,格式化程序除了@thread_key(因为我可以看到它来自ActiveSupport::TaggedLogging,我认为它没有什么处理问题)。

有谁知道问题出在哪里?我缺少什么?

同样的事情也发生在Rails 4.2.8上 我在MongoDB上运行Rails 4.2.8应用程序(带有mongoid gem),Rails.logger.debug('foo bar')在日志文件中打印
这让我觉得整件事与ActiveRecord有某种联系 是这样吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

似乎与ActiveRecord有关 我找到了this铁路代码,它将日志广播添加到STDOUT

相关问题