Class: Dry::Logger::Formatters::Structured

Inherits:
Logger::Formatter
  • Object
show all
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.

Direct Known Subclasses

JSON, String

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.

Since:

  • 1.0.0

[].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.

Since:

  • 1.0.0

-> message { message }

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 1.0.0



38
39
40
41
42
# File 'lib/dry/logger/formatters/structured.rb', line 38

def initialize(filters: DEFAULT_FILTERS, **options)
  super()
  @filter = filters.equal?(DEFAULT_FILTERS) ? NOOP_FILTER : Filter.new(filters)
  @options = options
end

Instance Attribute Details

#filterObject (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.

Since:

  • 1.0.0



30
31
32
# File 'lib/dry/logger/formatters/structured.rb', line 30

def filter
  @filter
end

#optionsObject (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.

Since:

  • 1.0.0



34
35
36
# File 'lib/dry/logger/formatters/structured.rb', line 34

def options
  @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

Returns:

Since:

  • 1.0.0



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.

Since:

  • 1.0.0



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