Class: Dry::Logger::Formatters::Template
- Inherits:
-
Object
- Object
- Dry::Logger::Formatters::Template
- Defined in:
- lib/dry/logger/formatters/template.rb
Overview
Basic string formatter.
This formatter returns log entries in key=value format.
Direct Known Subclasses
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.
/%<(\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.
"%<message>s"
Instance Attribute Summary collapse
- #tokens ⇒ Object readonly private
- #value ⇒ Object readonly private
Class Method Summary collapse
- .[](value) ⇒ Object private
Instance Method Summary collapse
- #%(other) ⇒ Object private
- #colorize(color, input) ⇒ Object private
- #include?(token) ⇒ Boolean private
-
#initialize(value) ⇒ Template
constructor
private
A new instance of Template.
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.
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
#tokens ⇒ 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/template.rb', line 30 def tokens @tokens end |
#value ⇒ 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.
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.
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.
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.
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.
96 97 98 |
# File 'lib/dry/logger/formatters/template.rb', line 96 def include?(token) tokens.include?(token) end |