Class: Lumberjack::EntryFormatter
- Inherits:
-
Object
- Object
- Lumberjack::EntryFormatter
- Defined in:
- lib/lumberjack/entry_formatter.rb
Overview
EntryFormatter provides a unified interface for formatting complete log entries by combining message formatting and attribute formatting into a single, coordinated system.
This class serves as the central formatting coordinator in the Lumberjack logging pipeline, bringing together two specialized formatters:
-
Message Formatter (Formatter) - Formats the main log message content
-
Attribute Formatter (AttributeFormatter) - Formats key-value attribute pairs
Instance Attribute Summary collapse
-
#attribute_formatter ⇒ Lumberjack::AttributeFormatter
The attribute formatter used to format log entry attributes.
-
#message_formatter ⇒ Lumberjack::Formatter
The message formatter used to format log message content.
Class Method Summary collapse
-
.build(message_formatter: nil, attribute_formatter: nil) {|formatter| ... } ⇒ Lumberjack::EntryFormatter
Build a new entry formatter using a configuration block.
Instance Method Summary collapse
-
#call(severity, timestamp, progname, msg) ⇒ String?
Compatibility method for Ruby’s standard Logger::Formatter interface.
-
#default_attribute_format(formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Set the default attribute formatter to apply to any attributes that do not have a specific formatter defined.
-
#format(message, attributes) ⇒ Array<Object, Hash>
Format a complete log entry by applying both message and attribute formatting.
-
#format_attribute_name(names, formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for the named attribute.
-
#format_attributes(classes_or_names, formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for the specified class or module when it appears as an attribute value.
-
#format_class(classes_or_names, formatter = nil, *args) {|obj| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for specific classes or modules.
-
#format_message(classes_or_names, formatter = nil, *args) {|obj| ... } ⇒ Lumberjack::EntryFormatter
Add a message formatter for specific classes or modules.
-
#include(formatter) ⇒ self
Extend this formatter by adding the formats defined in the provided formatter into this one.
-
#initialize(message_formatter: nil, attribute_formatter: nil) ⇒ EntryFormatter
constructor
Create a new entry formatter with the specified message and attribute formatters.
-
#prepend(formatter) ⇒ self
Extend this formatter by adding the formats defined in the provided formatter into this one.
-
#remove_attribute_class(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove an attribute formatter for the specified classes or modules.
-
#remove_attribute_name(names) ⇒ Lumberjack::EntryFormatter
Remove an attribute formatter for the specified attribute names.
-
#remove_class(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove a formatter for specific classes or modules.
-
#remove_message_formatter(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove a message formatter for specific classes or modules.
Constructor Details
#initialize(message_formatter: nil, attribute_formatter: nil) ⇒ EntryFormatter
Create a new entry formatter with the specified message and attribute formatters.
78 79 80 81 |
# File 'lib/lumberjack/entry_formatter.rb', line 78 def initialize(message_formatter: nil, attribute_formatter: nil) self. = self.attribute_formatter = attribute_formatter end |
Instance Attribute Details
#attribute_formatter ⇒ Lumberjack::AttributeFormatter
The attribute formatter used to format log entry attributes.
43 44 45 |
# File 'lib/lumberjack/entry_formatter.rb', line 43 def attribute_formatter @attribute_formatter end |
#message_formatter ⇒ Lumberjack::Formatter
The message formatter used to format log message content.
39 40 41 |
# File 'lib/lumberjack/entry_formatter.rb', line 39 def @message_formatter end |
Class Method Details
.build(message_formatter: nil, attribute_formatter: nil) {|formatter| ... } ⇒ Lumberjack::EntryFormatter
Build a new entry formatter using a configuration block. The block receives the new formatter as a parameter, allowing you to configure it with various configuration methods.
63 64 65 66 67 |
# File 'lib/lumberjack/entry_formatter.rb', line 63 def build(message_formatter: nil, attribute_formatter: nil, &block) formatter = new(message_formatter: , attribute_formatter: attribute_formatter) block&.call(formatter) formatter end |
Instance Method Details
#call(severity, timestamp, progname, msg) ⇒ String?
Compatibility method for Ruby’s standard Logger::Formatter interface. This delegates to the message formatter’s call method for basic Logger compatibility.
312 313 314 |
# File 'lib/lumberjack/entry_formatter.rb', line 312 def call(severity, , progname, msg) &.call(severity, , progname, msg) end |
#default_attribute_format(formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Set the default attribute formatter to apply to any attributes that do not have a specific formatter defined.
213 214 215 216 |
# File 'lib/lumberjack/entry_formatter.rb', line 213 def default_attribute_format(formatter = nil, *args, &block) @attribute_formatter.default(formatter, *args, &block) self end |
#format(message, attributes) ⇒ Array<Object, Hash>
Format a complete log entry by applying both message and attribute formatting. This is the main method that coordinates the formatting of both the message content and any associated attributes.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/lumberjack/entry_formatter.rb', line 284 def format(, attributes) = .call if .is_a?(Proc) = .format() if .respond_to?(:format) = nil if .is_a?(MessageAttributes) = .attributes = . end = Utils.flatten_attributes() if attributes = merge_attributes(attributes, ) if attributes = AttributesHelper.(attributes) attributes = attribute_formatter.format(attributes) if attributes && attribute_formatter [, attributes] end |
#format_attribute_name(names, formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for the named attribute.
184 185 186 187 |
# File 'lib/lumberjack/entry_formatter.rb', line 184 def format_attribute_name(names, formatter = nil, *args, &block) @attribute_formatter.add_attribute(names, formatter, *args, &block) self end |
#format_attributes(classes_or_names, formatter = nil, *args) {|value| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for the specified class or module when it appears as an attribute value.
198 199 200 201 |
# File 'lib/lumberjack/entry_formatter.rb', line 198 def format_attributes(classes_or_names, formatter = nil, *args, &block) @attribute_formatter.add_class(classes_or_names, formatter, *args, &block) self end |
#format_class(classes_or_names, formatter = nil, *args) {|obj| ... } ⇒ Lumberjack::EntryFormatter
Add a formatter for specific classes or modules. This method adds the formatter for both log messages and attributes.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lumberjack/entry_formatter.rb', line 123 def format_class(classes_or_names, formatter = nil, *args, &block) Array(classes_or_names).each do |class_or_name| unless @message_formatter.include?(class_or_name) @message_formatter.add(class_or_name, formatter, *args, &block) end unless @attribute_formatter.include_class?(class_or_name) @attribute_formatter.add_class(class_or_name, formatter, *args, &block) end end self end |
#format_message(classes_or_names, formatter = nil, *args) {|obj| ... } ⇒ Lumberjack::EntryFormatter
Add a message formatter for specific classes or modules.
159 160 161 162 |
# File 'lib/lumberjack/entry_formatter.rb', line 159 def (classes_or_names, formatter = nil, *args, &block) @message_formatter.add(classes_or_names, formatter, *args, &block) self end |
#include(formatter) ⇒ self
Extend this formatter by adding the formats defined in the provided formatter into this one.
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/lumberjack/entry_formatter.rb', line 244 def include(formatter) unless formatter.is_a?(Lumberjack::EntryFormatter) raise ArgumentError.new("formatter must be a Lumberjack::EntryFormatter") end @message_formatter ||= Lumberjack::Formatter.new @message_formatter.include(formatter.) @attribute_formatter ||= Lumberjack::AttributeFormatter.new @attribute_formatter.include(formatter.attribute_formatter) self end |
#prepend(formatter) ⇒ self
Extend this formatter by adding the formats defined in the provided formatter into this one. Formats defined in this formatter will take precedence and not be overridden.
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/lumberjack/entry_formatter.rb', line 263 def prepend(formatter) unless formatter.is_a?(Lumberjack::EntryFormatter) raise ArgumentError.new("formatter must be a Lumberjack::EntryFormatter") end @message_formatter ||= Lumberjack::Formatter.new @message_formatter.prepend(formatter.) @attribute_formatter ||= Lumberjack::AttributeFormatter.new @attribute_formatter.prepend(formatter.attribute_formatter) self end |
#remove_attribute_class(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove an attribute formatter for the specified classes or modules.
235 236 237 238 |
# File 'lib/lumberjack/entry_formatter.rb', line 235 def remove_attribute_class(classes_or_names) @attribute_formatter.remove_class(classes_or_names) self end |
#remove_attribute_name(names) ⇒ Lumberjack::EntryFormatter
Remove an attribute formatter for the specified attribute names.
224 225 226 227 |
# File 'lib/lumberjack/entry_formatter.rb', line 224 def remove_attribute_name(names) @attribute_formatter.remove_attribute(names) self end |
#remove_class(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove a formatter for specific classes or modules. This method removes the formatter for both log messages and attributes.
144 145 146 147 148 |
# File 'lib/lumberjack/entry_formatter.rb', line 144 def remove_class(classes_or_names) @message_formatter.remove(classes_or_names) @attribute_formatter.remove_class(classes_or_names) self end |
#remove_message_formatter(classes_or_names) ⇒ Lumberjack::EntryFormatter
Remove a message formatter for specific classes or modules.
170 171 172 173 |
# File 'lib/lumberjack/entry_formatter.rb', line 170 def (classes_or_names) @message_formatter.remove(classes_or_names) self end |