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

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

/%<(\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

"%<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



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

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



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

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



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

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



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

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

Instance Method Details

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



72
73
74
75
76
# File 'lib/dry/logger/formatters/template.rb', line 72

def %(tokens)
  output = value % tokens
  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



80
81
82
# File 'lib/dry/logger/formatters/template.rb', line 80

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:

  • (Boolean)

Since:

  • 1.0.0



86
87
88
# File 'lib/dry/logger/formatters/template.rb', line 86

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