简体   繁体   中英

How to change log level from number to log name using ougai logger

I am using ougai logger to enable custom logging in Ruby on Rails application. When I try to print the below log, it prints "level" in numbers. Instead, I want it in the log level itself.

logger.info('Information!')

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

Expected result

logger.info('Information!')

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

I know this was asked a while ago, but for anyone else who lands here with the same question...

The way I've addressed this is to make a customer formatter:

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

Then, in the config/environment file(s):

  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

This issue was the "inspiration" for this approach: https://github.com/tilfin/ougai/issues/99

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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