我遇到了同样的问题。尝试了该网站上的所有内容。
最后我在控制台中检查了Rails.logger 的值。它给出了以下输出。
#<RailsStdoutLogging::StdoutLogger:0x00007fe3b5bc3540
@default_formatter=#<Logger::Formatter:0x00007fe3b5bc3658 @datetime_format=nil>,
@formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00007fe3b5bc34c8 @datetime_format=nil>,
@level=0,
@logdev=
#<Logger::LogDevice:0x00007fe3b5bc3608
@dev=#<IO:<STDOUT>>,
@filename=nil,
@mon_count=0,
@mon_mutex=#<Thread::Mutex:0x00007fe3b5bc35b8>,
@mon_mutex_owner_object_id=70307991657220,
@mon_owner=nil,
@shift_age=nil,
@shift_period_suffix=nil,
@shift_size=nil>,
@progname=nil>
然后我与其他一些 Rails 项目进行了交叉检查,并给出了以下输出
#<ActiveSupport::Logger:0x00005566367b77c0 @level=0,
@progname=nil,
@default_formatter=#<Logger::Formatter:0x00005566367b7950 @datetime_format=nil>, @formatter=#<Logger::Formatter:0x00005566367b7770 @datetime_format=nil>,
@logdev=#<Logger::LogDevice:0x00005566367b7900 @shift_period_suffix=nil, @shift_size=nil,
@shift_age=nil, @filename=nil,
@dev=#<File:/home/rajanverma/workspace/aarvy/log/production.log>,
@mon_mutex=#<Thread::Mutex:0x00005566367b7888>, @mon_mutex_owner_object_id=46948744543360,
@mon_owner=nil, @mon_count=0>>
您可以清楚地看到两个项目都使用不同的记录器来显示输出。我不想在 STDOUT 上输出日志,所以我通过创建一个初始化文件将我的记录器更改为 ActiveSupport。
config/initializer/logger.rb
Rails.logger = ActiveSupport::Logger.new('log/production.log')
现在它开始登录 production.log。
编辑:我尝试在production.rb 中做同样的事情,但不知道为什么它总是被覆盖。所以我决定放入初始化程序。我不知道天气它有任何性能问题。此外,我不知道它是如何自动更改为 #<RailsStdoutLogging::StdoutLogger:0x00007fe3b5bc3540 的,因为在 production.log 上写入应该是默认行为。如果您知道更好的理由,请告诉我。