Class: Ougai::Formatters::Pino

Inherits:
Base
  • Object
show all
Includes:
ForJson
Defined in:
lib/ougai/formatters/pino.rb

Overview

A JSON formatter compatible with pino

Instance Attribute Summary

Attributes included from ForJson

#jsonize, #with_newline

Attributes inherited from Base

#app_name, #hostname, #serialize_backtrace, #trace_indent, #trace_max_lines

Instance Method Summary collapse

Methods inherited from Base

#call, #serialize_exc, #serialize_trace

Constructor Details

#initialize(app_name = nil, hostname = nil, opts = {}) ⇒ Pino

Intialize a formatter

Parameters:

  • app_name (String) (defaults to: nil)

    application name (execution program name if nil)

  • hostname (String) (defaults to: nil)

    hostname (hostname if nil)

  • opts (Hash) (defaults to: {})

    the initial values of attributes

Options Hash (opts):

  • :trace_indent (String) — default: 4

    the value of trace_indent attribute

  • :trace_max_lines (String) — default: 100

    the value of trace_max_lines attribute

  • :serialize_backtrace (String) — default: true

    the value of serialize_backtrace attribute

  • :jsonize (String) — default: true

    the value of jsonize attribute

  • :with_newline (String) — default: true

    the value of with_newline attribute



20
21
22
23
24
25
26
# File 'lib/ougai/formatters/pino.rb', line 20

def initialize(app_name = nil, hostname = nil, opts = {})
  aname, hname, opts = Base.parse_new_params([app_name, hostname, opts])
  super(aname, hname, opts)
  init_opts_for_json(opts)
  @trace_indent = opts.fetch(:trace_indent, 4)
  @serialize_backtrace = true
end

Instance Method Details

#_call(severity, time, progname, data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ougai/formatters/pino.rb', line 32

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

#convert_time(data) ⇒ Object



55
56
57
# File 'lib/ougai/formatters/pino.rb', line 55

def convert_time(data)
  data[:time] = (data[:time].to_f * 1000).to_i
end

#datetime_format=(val) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/ougai/formatters/pino.rb', line 28

def datetime_format=(val)
  raise NotImplementedError, 'Not support datetime_format attribute' unless val.nil?
end

#flat_err(data) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/ougai/formatters/pino.rb', line 44

def flat_err(data)
  return unless data.key?(:err)
  err = data.delete(:err)
  msg = err[:message]
  data[:type] ||= 'Error'
  data[:msg] ||= msg
  stack = "#{err[:name]}: #{msg}"
  stack += "\n" + (" " * @trace_indent) + err[:stack] if err.key?(:stack)
  data[:stack] ||= stack
end