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



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

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.



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

def app_name
  @app_name
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#serialize_backtraceBoolean

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

Returns:

  • (Boolean)

    the current value of serialize_backtrace



11
12
13
# File 'lib/ougai/formatters/base.rb', line 11

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



11
12
13
# File 'lib/ougai/formatters/base.rb', line 11

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



11
12
13
# File 'lib/ougai/formatters/base.rb', line 11

def trace_max_lines
  @trace_max_lines
end

Instance Method Details

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

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/ougai/formatters/base.rb', line 36

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

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



32
33
34
# File 'lib/ougai/formatters/base.rb', line 32

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

#datetime_format=(value) ⇒ Object



40
41
42
# File 'lib/ougai/formatters/base.rb', line 40

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

#serialize_exc(ex) ⇒ Object



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

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



56
57
58
59
# File 'lib/ougai/formatters/base.rb', line 56

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