Rails 4 弹性搜索日志记录的生产问题

Rails 4 production issue with elasticsearch logging

我正在 rails 4.2.11.3 应用程序和 ruby 2.5.0 应用程序中实现 Heroku 上的 Elasticsearch,用于用户模型的基本搜索。在heroku staging环境下,完美运行,但是在生产环境下,我在导入模型的时候遇到了这个错误。

irb(main):001:0> User.import
Traceback (most recent call last):
       16: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/visitor.rb:16:in `visit'
       15: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:208:in `visit_Psych_Nodes_Mapping'
       14: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:374:in `revive'
       13: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:336:in `revive_hash'
       12: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:336:in `each_slice'
       11: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:336:in `each'
       10: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:338:in `block in revive_hash'
        9: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:32:in `accept'
        8: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/visitor.rb:6:in `accept'
        7: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/visitor.rb:16:in `visit'
        6: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:208:in `visit_Psych_Nodes_Mapping'
        5: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/visitors/to_ruby.rb:391:in `resolve_class'
        4: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/class_loader.rb:28:in `load'
        3: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/class_loader.rb:46:in `find'
        2: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/class_loader.rb:54:in `resolve'
        1: from /app/vendor/ruby-2.5.0/lib/ruby/2.5.0/psych/class_loader.rb:54:in `path2class'
ArgumentError (undefined class/module Logger::SimpleFormatter)

Rails.logger 是:

irb(main):002:0> Rails.logger
=> #<ActiveSupport::Logger:0x0000563a356d7070 @level=2, @progname=nil, @default_formatter=#<Logger::Formatter:0x0000563a356d6e40 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x0000563a356d6be8 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x0000563a356d6d78 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x0000563a356d6c88>>, @local_levels=#<ThreadSafe::Cache:0x0000563a356d6ad0 @backend={}, @default_proc=nil>>

我知道 Logger::SimpleFormatter 已从 rails 4.1 中删除,我应该使用 ActiveSupport::Logger::SimpleFormatter。我知道生产 log_formatter 默认为 Logger::Formatter。我在 config/environments/production.rb 中尝试了多种配置变体,但无济于事,并且还添加了 rails_12factor gem,它将 Rails.logger 切换为:

<RailsStdoutLogging::StdoutLogger:0x000055fbb41bca28 @level=2, @progname=nil, @default_formatter=#<Logger::Formatter:0x000055fbb41bc988 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x000055fbb41bc8c0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x000055fbb41bc938 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000055fbb41bc8e8>>, @local_levels=#<ThreadSafe::Cache:0x000055fbb41bc898 @backend={}, @default_proc=nil>>

如果我引入我自己的 Logger::SimpleFormatter class,继承自 ActiveSupport::Logger::SimpleFormatter,它解决了问题,但错误移动到:

undefined class/module Logger::LogDevice::LogDeviceMutex

问题是我们在数据库中有遗留的 Braintree 对象,当我们执行 ES 导入时,这些对象正在调用 rails 3 记录器