Rails 4.0.2:Rails.logger!= Rails.application.config.logger

时间:2014-01-29 15:02:15

标签: ruby-on-rails-4

我正在尝试使用我的Rails 4.0.2(ruby 2.1.0)应用程序来使用Syslog。

这是我的配置:

config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new('myApp'))

我注意到Rails.application.config.logger对应于我在production.rb文件中设置的记录器。如果我直接使用它(例如在rails c中),我在syslog中成功获取了消息。

令我困惑的是Rails.logger(如果我理解的话,是rails本身使用的那个)不是同一个对象。它是一个简单的Logger,就像默认的开发者一样。因此,我在stdout中从rails获取所有日志消息。

输出表单栏c:

2.1.0 :004 > Rails.application.config.logger
 => #<Syslog::Logger:0x00000003529fd8 @level=1, @formatter=#<Syslog::Logger::Formatter:0x00000003529fb0>, @facility=8>
2.1.0 :005 > Rails.logger
 => #<Logger:0x000000032b21d8 @progname=nil, @level=1, @default_formatter=#<Logger::Formatter:0x000000032b21b0 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x000000032b1fd0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x000000032b20c0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x000000032b2070 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x000000032b2020>>>>

有人可以告诉我这是否是预期的行为?我错过了什么吗?

**编辑**

我在Rails的logger =方法中添加了一些跟踪(值和调用者),这是我得到的:

App 18055 stdout: SET LOGGER #<Syslog::Logger:0x000000044b08a8>
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application/bootstrap.rb:32:in `block in <module:Bootstrap>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
App 18055 stdout: /home/myUser/myApp/releases/20140129114805/config/environment.rb:5:in `<top (required)>'
App 18055 stdout: config.ru:3:in `require'
App 18055 stdout: config.ru:3:in `block in <main>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
App 18055 stdout: config.ru:1:in `new'
App 18055 stdout: config.ru:1:in `<main>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'
App 18055 stdout: SET LOGGER #<Logger:0x000000042ea8c0>
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rails_stdout_logging-0.0.3/lib/rails_stdout_logging/rails3.rb:7:in `set_logger'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rails_stdout_logging-0.0.3/lib/rails_stdout_logging/railtie.rb:6:in `block in <class:Railtie>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `call'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application/bootstrap.rb:76:in `block in <module:Bootstrap>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
App 18055 stdout: /home/myUser/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
App 18055 stdout: /home/myUser/myApp/releases/20140129114805/config/environment.rb:5:in `<top (required)>'
App 18055 stdout: config.ru:3:in `require'
App 18055 stdout: config.ru:3:in `block in <main>'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
App 18055 stdout: /home/myUser/myApp/shared/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
App 18055 stdout: config.ru:1:in `new'
App 18055 stdout: config.ru:1:in `<main>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
App 18055 stdout: /usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'

第一个调用设置了正确的记录器,但由于我不理解的原因而被覆盖。

提前致谢。

1 个答案:

答案 0 :(得分:4)

知道了!

答案:如果您没有使用12factor主机,请不要使用“rails_12factor”gem ...

它的一个功能是使用“before_initialize”中的“rails_stdout_logging”覆盖Rails的记录器。