Class: Dry::Logger::Formatters::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/logger/formatters/template.rb

Overview

Basic string formatter.

This formatter returns log entries in key=value format.

Since:

  • 1.0.0

API:

  • public

Direct Known Subclasses

Colorized

Defined Under Namespace

Classes: Colorized

Constant Summary collapse

TOKEN_REGEXP =

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

API:

  • private

/%<(\w*)>s/
MESSAGE_TOKEN =

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

API:

  • private

"%<message>s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Template

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 Template.

Since:

  • 1.0.0

API:

  • private



64
65
66
67
# File 'lib/dry/logger/formatters/template.rb', line 64

def initialize(value)
  @value = value
  @tokens = value.scan(TOKEN_REGEXP).flatten(1).map(&:to_sym).to_set
end

Instance Attribute Details

#tokensObject (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

API:

  • private



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

def tokens
  @tokens
end

#valueObject (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

API:

  • private



26
27
28
# File 'lib/dry/logger/formatters/template.rb', line 26

def value
  @value
end

Class Method Details

.[](value) ⇒ 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

API:

  • private



34
35
36
37
38
# File 'lib/dry/logger/formatters/template.rb', line 34

def self.[](value)
  cache.fetch(value) {
    cache[value] = (colorized?(value) ? Template::Colorized : Template).new(value)
  }
end

Instance Method Details

#%(other) ⇒ 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

API:

  • private



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dry/logger/formatters/template.rb', line 71

def %(other)
  begin
    output = value % other
  rescue Encoding::CompatibilityError
    sanitized_tokens = other.transform_values { |v|
      if v.respond_to?(:encode)
        v.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?")
      else
        v
      end
    }
    output = value % sanitized_tokens
  end
  output.strip!
  output.split(NEW_LINE).map(&:rstrip).join(NEW_LINE)
end

#colorize(color, input) ⇒ 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

API:

  • private



90
91
92
# File 'lib/dry/logger/formatters/template.rb', line 90

def colorize(color, input)
  "\e[#{Colors[color.to_sym]}m#{input}\e[0m"
end

#include?(token) ⇒ Boolean

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:

Since:

  • 1.0.0

API:

  • private



96
97
98
# File 'lib/dry/logger/formatters/template.rb', line 96

def include?(token)
  tokens.include?(token)
end