Class: Makit::Logging::Formatters::TextFormatter
- Defined in:
- lib/makit/logging/formatters/text_formatter.rb
Overview
Text formatter with timestamp and log level
Formats log requests as human-readable text with timestamp, log level, and message. This is the most common format for traditional log files.
Instance Attribute Summary collapse
-
#include_context ⇒ Boolean
readonly
Whether to include context in output.
-
#timestamp_format ⇒ String
readonly
Timestamp format string.
Instance Method Summary collapse
-
#config ⇒ Hash
Get formatter configuration.
-
#format(log_request) ⇒ String
Format log request as text.
-
#initialize(timestamp_format: "%Y-%m-%d %H:%M:%S", include_context: false) ⇒ TextFormatter
constructor
Initialize text formatter.
Constructor Details
#initialize(timestamp_format: "%Y-%m-%d %H:%M:%S", include_context: false) ⇒ TextFormatter
Initialize text formatter
27 28 29 30 |
# File 'lib/makit/logging/formatters/text_formatter.rb', line 27 def initialize(timestamp_format: "%Y-%m-%d %H:%M:%S", include_context: false) @timestamp_format = @include_context = include_context end |
Instance Attribute Details
#include_context ⇒ Boolean (readonly)
Returns whether to include context in output.
21 22 23 |
# File 'lib/makit/logging/formatters/text_formatter.rb', line 21 def include_context @include_context end |
#timestamp_format ⇒ String (readonly)
Returns timestamp format string.
19 20 21 |
# File 'lib/makit/logging/formatters/text_formatter.rb', line 19 def @timestamp_format end |
Instance Method Details
#config ⇒ Hash
Get formatter configuration
55 56 57 58 59 60 |
# File 'lib/makit/logging/formatters/text_formatter.rb', line 55 def config super.merge( timestamp_format: @timestamp_format, include_context: @include_context, ) end |
#format(log_request) ⇒ String
Format log request as text
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/makit/logging/formatters/text_formatter.rb', line 36 def format(log_request) = log_request..strftime(@timestamp_format) level = log_request.level.to_s.upcase = log_request. formatted = "[#{}] #{level} #{}" # Add context if enabled and present if @include_context && !log_request.context.empty? context_str = log_request.context.map { |k, v| "#{k}=#{v}" }.join(" ") formatted += " #{context_str}" end formatted end |