繁体   English   中英

Rails Log Formatter在开发中可用,但不能在生产中使用

[英]Rails Log Formatter works in Development, but not Production

我通过在每个日志语句中添加时间,用户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”。 我已经在这个问题上工作了大约一个星期,非常感谢您的帮助!

让我知道您是否有任何澄清问题,非常感谢!

看起来您只是有一个命名冲突-Ruby 2.0+中包含Logger :: Formatter类,由于生产中的类加载策略不同,因此已加载了Logger :: Formatter类,而不是您的自定义类。 只需将您的类重命名为例如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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM