Class: Ougai::Formatters::Base
- Inherits:
-
Logger::Formatter
- Object
- Logger::Formatter
- Ougai::Formatters::Base
- Defined in:
- lib/ougai/formatters/base.rb
Overview
Base formatter Custom formatter must override ‘_call`.
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#serialize_backtrace ⇒ Boolean
Whether exception should converts String (by default this is on).
-
#trace_indent ⇒ Fixnum
Specify exception backtrace indent (by default this is 2).
-
#trace_max_lines ⇒ Fixnum
Keep exception backtrace lines (by default this is 100).
Instance Method Summary collapse
- #_call(severity, time, progname, data) ⇒ Object
- #call(severity, time, progname, data) ⇒ Object
- #datetime_format=(value) ⇒ Object
-
#initialize(app_name = nil, hostname = nil, opts = {}) ⇒ Base
constructor
Intialize a formatter.
- #serialize_exc(ex) ⇒ Object
- #serialize_trace(trace) ⇒ Object
Constructor Details
#initialize(app_name = nil, hostname = nil, opts = {}) ⇒ Base
Intialize a formatter
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_name ⇒ Object (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 |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
16 17 18 |
# File 'lib/ougai/formatters/base.rb', line 16 def hostname @hostname end |
#serialize_backtrace ⇒ Boolean
Whether exception should converts String (by default this is on).
13 14 15 |
# File 'lib/ougai/formatters/base.rb', line 13 def serialize_backtrace @serialize_backtrace end |
#trace_indent ⇒ Fixnum
Specify exception backtrace indent (by default this is 2).
13 14 15 |
# File 'lib/ougai/formatters/base.rb', line 13 def trace_indent @trace_indent end |
#trace_max_lines ⇒ Fixnum
Keep exception backtrace lines (by default this is 100).
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
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 |