Class: Ougai::Formatters::Customizable
- Inherits:
-
Base
- Object
- Base
- Ougai::Formatters::Customizable
- Defined in:
- lib/ougai/formatters/customizable.rb
Overview
Ougai log printing can be split in three components:
-
Main log message: usually with timestamp, log severity and a single line message
-
Log data: the structured logging component. Can be represented by a Hash
-
Errors: errors require specific log formatting
Customizable offers a flexible way to handle each component independently by assigning a proc to the following keys:
-
:format_msg
Format message. Proc arguments are |level, datetime, progname, data|. This block must remove the key:msg
fromdata
-
:format_data
Format data. Proc argument is |data|. -
:format_err
Format err. Proc argument is |data|. The proc must remove the key:err
Class Method Summary collapse
-
.default_data_format(excluded_fields, plain) ⇒ Proc
Define the default error formatting to use which handles field exclusion and plain mode for amazing-print.
-
.default_err_format(trace_indent = 4) ⇒ Proc
Define the default error formatting to use.
-
.default_msg_format(color_config) ⇒ Proc
Define the default main log message formatting to use.
Instance Method Summary collapse
-
#_call(severity, time, progname, data) ⇒ String
Format a log entry.
-
#initialize(app_name = nil, hostname = nil, opts = {}) ⇒ Customizable
constructor
Intialize a formatter.
Constructor Details
#initialize(app_name = nil, hostname = nil, opts = {}) ⇒ Customizable
Intialize a 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
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.
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
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
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 |