Class: Dry::Logger::Formatters::Structured
- Inherits:
-
Logger::Formatter
- Object
- Logger::Formatter
- Dry::Logger::Formatters::Structured
- Defined in:
- lib/dry/logger/formatters/structured.rb
Overview
Default structured formatter which receives Entry from the backends.
This class can be used as the base class for your custom formatters.
Constant Summary collapse
- DEFAULT_FILTERS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[].freeze
- NOOP_FILTER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
-> { }
Instance Attribute Summary collapse
- #filter ⇒ Object readonly private
- #options ⇒ Object readonly private
Instance Method Summary collapse
-
#call(_severity, _time, _progname, entry) ⇒ String
Filter and then format the log entry into a string.
-
#format(entry) ⇒ Entry
Format entry into a loggable object.
- #format_values(entry) ⇒ Object private
-
#initialize(filters: DEFAULT_FILTERS, **options) ⇒ Structured
constructor
private
A new instance of Structured.
Constructor Details
#initialize(filters: DEFAULT_FILTERS, **options) ⇒ Structured
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Structured.
38 39 40 41 42 |
# File 'lib/dry/logger/formatters/structured.rb', line 38 def initialize(filters: DEFAULT_FILTERS, **) super() @filter = filters.equal?(DEFAULT_FILTERS) ? NOOP_FILTER : Filter.new(filters) @options = end |
Instance Attribute Details
#filter ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
30 31 32 |
# File 'lib/dry/logger/formatters/structured.rb', line 30 def filter @filter end |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
34 35 36 |
# File 'lib/dry/logger/formatters/structured.rb', line 34 def @options end |
Instance Method Details
#call(_severity, _time, _progname, entry) ⇒ String
Filter and then format the log entry into a string
Custom formatters typically won’t have to override this method because the actual formatting logic is implemented as Structured#format
54 55 56 |
# File 'lib/dry/logger/formatters/structured.rb', line 54 def call(_severity, _time, _progname, entry) format(entry.filter(filter)) + NEW_LINE end |
#format(entry) ⇒ Entry
Format entry into a loggable object
Custom formatters should override this method
65 66 67 |
# File 'lib/dry/logger/formatters/structured.rb', line 65 def format(entry) format_values(entry) end |
#format_values(entry) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 75 76 77 78 |
# File 'lib/dry/logger/formatters/structured.rb', line 71 def format_values(entry) entry .to_h .map { |key, value| [key, respond_to?(meth = "format_#{key}", true) ? __send__(meth, value) : value] } .to_h end |