Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change log level from number to log name #83

Closed
PoombavaiS opened this issue Jul 30, 2019 · 5 comments
Closed

Change log level from number to log name #83

PoombavaiS opened this issue Jul 30, 2019 · 5 comments

Comments

@PoombavaiS
Copy link

When I try to print the below log, it prints "level" in numbers. Instead, I want it in the log level name 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!"}

Am expecting,

logger.info('Information!') {"name":"main","hostname":"mint","pid":14607,"level":"Info","time":"2016-10-16T22:26:48.835+09:00","v":0,"msg":"Information!"}
How to do this?

@tilfin
Copy link
Owner

tilfin commented Jul 30, 2019

Please make suitable formatter for your project referring to https://github.com/tilfin/ougai/blob/master/lib/ougai/formatters/bunyan.rb.

For example,

require 'rubygems'
require 'ougai'

class YourFormatter < Ougai::Formatters::Base
  include Ougai::Formatters::ForJson

  def initialize(app_name = nil, hostname = nil, opts = {})
    aname, hname, opts = Ougai::Formatters::Base.parse_new_params([app_name, hostname, opts])
    super(aname, hname, opts)
    init_opts_for_json(opts)
  end

  def _call(severity, time, progname, data)
    dump({
      name: progname || @app_name,
      hostname: @hostname,
      pid: $$,
      level: severity.downcase,
      time: time,
      v: 0
    }.merge(data))
  end

  def convert_time(data)
    data[:time] = format_datetime(data[:time])
  end
end

logger = Ougai::Logger.new(STDOUT)
logger.formatter = YourFormatter.new
logger.info('a message', foo: 100)
{"name":"yours","hostname":"mint3","pid":25620,"level":"info","time":"2019-07-30T18:15:12.972+09:00","v":0,"msg":"a message","foo":100}

@zw963
Copy link

zw963 commented Aug 10, 2019

i am wired too why we use number instead level name, thank you.

@tilfin
Copy link
Owner

tilfin commented Aug 11, 2019

If the log level is numeric, the log service can filter the logs easily like { $.level> 30 } .

@tilfin tilfin closed this as completed Aug 11, 2019
@zw963
Copy link

zw963 commented Aug 11, 2019

If the log level is numeric, the log service can filter the logs easily like { $.level> 30 }

Can we use { $.level> 30 } filter in kibana? it is not worked for me.

image

But filter by string is more rememberable.

@zw963
Copy link

zw963 commented Aug 11, 2019

thank you, kibanba can use: 'level > 30' directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants