gpt4 book ai didi

ruby-on-rails - Rails Log Formatter 适用于开发,但不适用于生产

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:22 25 4
gpt4 key购买 nike

我通过向每个日志语句添加时间、用户 ID 和严重性来使我的 Rails 记录器更加详细。

我在 config/initializers 中创建了一个名为 log_formatter.rb 的新文件来格式化日志消息。这是文件:

class Logger::Formatter
def call(severity, time, progname, msg)
"#{Time.now.strftime("%F %T").to_s} #{severity} #{msg}\n"
end
end

Rails.configuration.log_tags = [
proc do |req|
if req.session["warden.user.user.key"].nil?
"Anonym"
else
"uid:#{req.session["warden.user.user.key"][0][0]}"
end
end
]

我的 config/environments/development.rb 包含:

Rails.application.configure do
config.log_level = :info
config.log_formatter = Logger::Formatter.new
end

这在我进行开发时非常有效,并且我将输出输出到以下日志文​​件:

2015-12-02 00:33:30 INFO [uid:1] <Message>

我的 config/environments/production.rb 包含:

require_relative './../initializers/log_formatter'
Rails.application.configure do
config.logger = ActiveSupport::Logger.new(STDOUT)
config.log_level = :info
config.log_formatter = Logger::Formatter.new
end

但是,在生产过程中,我得到的唯一消息是来自 log_tags

[uid:1] <Message>

我不确定这里发生了什么变化,但看起来在格式化记录器时没有调用函数“call”。我已经在这个问题上工作了大约一个星期,非常感谢您的帮助!

如果您有任何疑问,请告诉我,非常感谢!

最佳答案

看起来你刚刚遇到了命名冲突 - Logger::Formatter 类包含在 Ruby 2.0+ 中,由于生产中的类加载策略不同,它被加载而不是你的自定义类。只需将您的类(class)重命名为例如MyLoggerFormatter,它应该可以工作:

class MyLoggerFormatter
def call(severity, time, progname, msg)
"#{Time.now.strftime("%F %T").to_s} #{severity} #{msg}\n"
end
end

Rails.application.configure do
config.logger = ActiveSupport::Logger.new(STDOUT)
config.log_level = :info
config.log_formatter = MyLoggerFormatter.new
end

关于ruby-on-rails - Rails Log Formatter 适用于开发,但不适用于生产,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com