Class: Makit::Logging::Formatters::ConsoleFormatter
- Defined in:
- lib/makit/logging/formatters/console_formatter.rb
Overview
Console formatter for colored log output
Formats log requests for console display with colors, symbols, and optional timestamps/levels. This is specifically designed for human-readable console output.
Instance Attribute Summary collapse
-
#show_level ⇒ Boolean
readonly
Whether to show log levels.
-
#show_timestamp ⇒ Boolean
readonly
Whether to show timestamps.
Instance Method Summary collapse
-
#config ⇒ Hash
Get formatter configuration.
-
#format(log_request) ⇒ String
Format log request for console display.
-
#initialize(show_timestamp: false, show_level: false) ⇒ ConsoleFormatter
constructor
Initialize console formatter.
Constructor Details
#initialize(show_timestamp: false, show_level: false) ⇒ ConsoleFormatter
Initialize console formatter
37 38 39 40 |
# File 'lib/makit/logging/formatters/console_formatter.rb', line 37 def initialize(show_timestamp: false, show_level: false) @show_timestamp = @show_level = show_level end |
Instance Attribute Details
#show_level ⇒ Boolean (readonly)
Returns whether to show log levels.
31 32 33 |
# File 'lib/makit/logging/formatters/console_formatter.rb', line 31 def show_level @show_level end |
#show_timestamp ⇒ Boolean (readonly)
Returns whether to show timestamps.
29 30 31 |
# File 'lib/makit/logging/formatters/console_formatter.rb', line 29 def @show_timestamp end |
Instance Method Details
#config ⇒ Hash
Get formatter configuration
85 86 87 88 89 90 |
# File 'lib/makit/logging/formatters/console_formatter.rb', line 85 def config super.merge( show_timestamp: @show_timestamp, show_level: @show_level, ) end |
#format(log_request) ⇒ String
Format log request for console display
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/makit/logging/formatters/console_formatter.rb', line 46 def format(log_request) parts = [] # Add timestamp if enabled if @show_timestamp = log_request..strftime("%H:%M:%S") parts << "[#{}]".colorize(:light_black) end # Add log level if enabled if @show_level level_str = log_request.level.to_s.upcase level_color = get_level_color(log_request.level) parts << level_str.colorize(level_color) end # Add symbol and message symbol = get_level_symbol(log_request.level) symbol_color = get_level_color(log_request.level) = log_request. # Special handling for task messages - no symbol, no context, just the message if log_request.context[:task_message] parts << else # Add context if present (only for non-task messages) unless log_request.context.empty? context_str = log_request.context.map { |k, v| "#{k}=#{v}" }.join(" ") += " #{context_str}".colorize(:light_black) end parts << "#{symbol} #{}".colorize(symbol_color) end parts.join(" ") end |