Class: Ougai::Formatters::Customizable

Inherits:
Base
  • Object
show all
Defined in:
lib/ougai/formatters/customizable.rb

Overview

Ougai log printing can be split in three components:

  1. Main log message: usually with timestamp, log severity and a single line message

  2. Log data: the structured logging component. Can be represented by a Hash

  3. Errors: errors require specific log formatting

Customizable offers a flexible way to handle each component independently by assigning a proc to the following keys:

  1. :format_msg Format message. Proc arguments are |level, datetime, progname, data|. This block must remove the key :msg from data

  2. :format_data Format data. Proc argument is |data|.

  3. :format_err Format err. Proc argument is |data|. The proc must remove the key :err

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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_max_lines (String) — default: 100

    the value of trace_max_lines attribute

  • :plain (String) — default: false

    the value of plain attribute

  • :excluded_fields (String) — default: []

    the value of excluded_fields attribute

  • :color_config (Ougai::Formatters::Colors::Configuration)

    assign a color configuration.

  • :format_msg (Proc)

    main message formatter

  • :format_data (Proc)

    data formatter

  • :format_err (Proc)

    error formatter



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ougai/formatters/customizable.rb', line 93

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

  # Message logging
  color_config = opts.fetch(:color_config) {
    color_config = Ougai::Formatters::Colors::Configuration.new({})
  }
  @format_msg = opts.fetch(:format_msg) {
    Customizable.default_msg_format(color_config)
  }

  # Data logging
  plain = opts.fetch(:plain) { false }
  excluded_fields = opts[:excluded_fields] || []
  @format_data = opts.fetch(:format_data) {
    Customizable.default_data_format(excluded_fields, plain)
  }

  # Error logging
  trace_indent = opts.fetch(:trace_indent) { 4 }
  @format_err = opts.fetch(:format_err) {
    Customizable.default_err_format(trace_indent)
  }

  # Ensure dependency are present
  load_dependent
end

Class Method Details

.default_data_format(excluded_fields, plain) ⇒ Proc

Define the default error formatting to use which handles field exclusion and plain mode for amazing-print

Parameters:

  • excluded_fields (Array<Symbol>)

    list of key to exclude from data before printing logs

  • plain (Boolean)

    parameter to define if Amazing-Print renders in plain mode or not

Returns:

  • (Proc)

    data formatter



51
52
53
54
55
56
57
58
# File 'lib/ougai/formatters/customizable.rb', line 51

def default_data_format(excluded_fields, plain)
  proc do |data|
    excluded_fields.each { |field| data.delete(field) }
    next nil if data.empty?

    data.ai(plain: plain)
  end
end

.default_err_format(trace_indent = 4) ⇒ Proc

Define the default error formatting to use.

Parameters:

  • trace_indent (Integer) (defaults to: 4)

    space indentation to prepend before trace content

Returns:

  • (Proc)

    error formatter



66
67
68
69
70
71
72
73
74
75
# File 'lib/ougai/formatters/customizable.rb', line 66

def default_err_format(trace_indent = 4)
  proc do |data|
    next nil unless data.key?(:err)

    err = data.delete(:err)
    err_str = "  #{err[:name]} (#{err[:message]}):"
    err_str += "\n" + (' ' * trace_indent) + err[:stack] if err.key?(:stack)
    err_str
  end
end

.default_msg_format(color_config) ⇒ Proc

Define the default main log message formatting to use. A non-null color configuration has to be provided. The configuration can however be empty

Parameters:

Returns:

  • (Proc)

    main message formatter



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

def default_msg_format(color_config)
  proc do |severity, datetime, _progname, data|
    msg = data.delete(:msg)
    severity  = color_config.color(:severity, severity, severity)
    datetime  = color_config.color(:datetime, datetime, severity)
    msg       = color_config.color(:msg, msg, severity)

    "[#{datetime}] #{severity}: #{msg}"
  end
end

Instance Method Details

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

Format a log entry

Parameters:

  • severity (String)

    log severity, in capital letters

  • time (Time)

    timestamp of the log. Is formatted by strftime

  • progname (String)

    optional program name

  • data (Hash)

    log data. Main message is stored under the key :msg while errors are logged under the key :err.

Returns:

  • (String)

    log text, ready to be printed out



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ougai/formatters/customizable.rb', line 131

def _call(severity, time, progname, data)
  strs = ''.dup
  # Main message
  dt =  format_datetime(time)
  msg_str = @format_msg.call(severity, dt, progname, data)
  strs.concat(msg_str)

  # Error: displayed before additional data
  err_str = @format_err.call(data)
  strs.concat("\n").concat(err_str) unless err_str.nil?

  # Additional data
  data_str = @format_data.call(data)
  strs.concat("\n").concat(data_str) unless data_str.nil?

  strs.concat("\n")
end