Class: Ougai::Formatters::Base

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/ougai/formatters/base.rb

Overview

Base formatter Custom formatter must override ‘_call`.

Direct Known Subclasses

Bunyan, Pino, Readable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Intialize a formatter

Parameters:

  • app_name (String) (defaults to: nil)

    application name

  • hostname (String) (defaults to: nil)

    hostname

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

    the initial values of attributes

Options Hash (opts):

  • :trace_indent (String) — default: 2

    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



25
26
27
28
29
30
31
32
# File 'lib/ougai/formatters/base.rb', line 25

def initialize(app_name = nil, hostname = nil, opts = {})
  @app_name = app_name || File.basename($0, ".rb")
  @hostname = hostname || Socket.gethostname.force_encoding('UTF-8')
  @trace_indent = opts.fetch(:trace_indent, 2)
  @trace_max_lines = opts.fetch(:trace_max_lines, 100)
  @serialize_backtrace = opts.fetch(:serialize_backtrace, true)
  self.datetime_format = nil
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



16
17
18
# File 'lib/ougai/formatters/base.rb', line 16

def app_name
  @app_name
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



16
17
18
# File 'lib/ougai/formatters/base.rb', line 16

def hostname
  @hostname
end

#serialize_backtraceBoolean

Whether exception should converts String (by default this is on).

Returns:

  • (Boolean)

    the current value of serialize_backtrace



13
14
15
# File 'lib/ougai/formatters/base.rb', line 13

def serialize_backtrace
  @serialize_backtrace
end

#trace_indentFixnum

Specify exception backtrace indent (by default this is 2).

Returns:

  • (Fixnum)

    the current value of trace_indent



13
14
15
# File 'lib/ougai/formatters/base.rb', line 13

def trace_indent
  @trace_indent
end

#trace_max_linesFixnum

Keep exception backtrace lines (by default this is 100).

Returns:

  • (Fixnum)

    the current value of trace_max_lines



13
14
15
# File 'lib/ougai/formatters/base.rb', line 13

def trace_max_lines
  @trace_max_lines
end

Instance Method Details

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

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/ougai/formatters/base.rb', line 38

def _call(severity, time, progname, data)
  raise NotImplementedError, "_call must be implemented"
end

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



34
35
36
# File 'lib/ougai/formatters/base.rb', line 34

def call(severity, time, progname, data)
  _call(severity, time, progname, data.is_a?(Hash) ? data : { msg: data.to_s })
end

#datetime_format=(value) ⇒ Object



42
43
44
# File 'lib/ougai/formatters/base.rb', line 42

def datetime_format=(value)
  @datetime_format = value || default_datetime_format
end

#serialize_exc(ex) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ougai/formatters/base.rb', line 46

def serialize_exc(ex)
  err = {
    name: ex.class.name,
    message: ex.to_s
  }
  if ex.backtrace
    bt = ex.backtrace.slice(0, @trace_max_lines)
    err[:stack] = @serialize_backtrace ? serialize_trace(bt) : bt
  end
  err
end

#serialize_trace(trace) ⇒ Object



58
59
60
61
# File 'lib/ougai/formatters/base.rb', line 58

def serialize_trace(trace)
  sp = "\n" + ' ' * @trace_indent
  trace.join(sp)
end