如何使用ougai logger将日志级别从数字更改为日志名称

时间:2019-07-25 09:50:38

标签: ruby-on-rails logging

我正在使用ougai记录器在Ruby on Rails应用程序中启用自定义记录。当我尝试打印以下日志时,它将以数字打印“级别”。相反,我希望它在日志级别本身中。

logger.info('Information!')

{"name":"main","hostname":"mint","pid":14607,"level":30,"time":"2016-10-16T22:26:48.835+09:00","v":0,"msg":"Information!"}

预期结果

logger.info('Information!')

{"name":"main","hostname":"mint","pid":14607,"level":"Info","time":"2016-10-16T22:26:48.835+09:00","v":0,"msg":"Information!"}

1 个答案:

答案 0 :(得分:1)

我知道这个问题是在不久前问到的,但是对于任何在这里遇到同样问题的人……

我解决这个问题的方法是制作客户格式化程序:

class LogLevelLoggerFormatter < Ougai::Formatters::Bunyan
  # override the to_level method to just return the string
  # instead of converting it to an integer
  # see .../gems/ougai-1.5.8/lib/ougai/formatters/bunyan.rb
  def to_level(severity)
    severity
  end
end

然后,在配置/环境文件中:

  require 'log_level_logger_formatter'
  require 'ougai'

  Cube::Application.configure do
    config.logger = Ougai::Logger.new(STDOUT)
    config.logger.formatter = LogLevelLoggerFormatter.new
    # ... other config
  end

此问题是此方法的“灵感” https://github.com/tilfin/ougai/issues/99