Class: LogStruct::SemanticLogger::ColorFormatter
- Inherits:
-
SemanticLogger::Formatters::Color
- Object
- SemanticLogger::Formatters::Color
- LogStruct::SemanticLogger::ColorFormatter
- Extended by:
- T::Sig
- Defined in:
- lib/log_struct/semantic_logger/color_formatter.rb
Overview
Development-Optimized Colorized JSON Formatter
This formatter extends SemanticLogger's Color formatter to provide beautiful, readable JSON output in development environments. It significantly improves the developer experience when working with structured logs.
Benefits of Colorized Output:
Readability
- Syntax highlighting: JSON keys, values, and data types are color-coded
- Visual hierarchy: Different colors help identify structure at a glance
- Error spotting: Quickly identify malformed data or unexpected values
- Context separation: Log entries are visually distinct from each other
Performance in Development
- Faster debugging: Quickly scan logs without reading every character
- Pattern recognition: Colors help identify common log patterns
- Reduced cognitive load: Less mental effort required to parse log output
- Improved workflow: Spend less time reading logs, more time coding
Customization
- Configurable colors: Customize colors for keys, strings, numbers, etc.
- Environment-aware: Automatically disabled in production/CI environments
- Fallback support: Gracefully falls back to standard formatting if needed
Color Mapping:
- Keys: Yellow - Easy to spot field names
- Strings: Green - Clear indication of text values
- Numbers: Blue - Numeric values stand out
- Booleans: Magenta - true/false values are distinctive
- Null: Red - Missing values are immediately visible
- Logger names: Cyan - Source identification
Integration with SemanticLogger:
This formatter preserves all SemanticLogger benefits (performance, threading, reliability) while adding visual enhancements. It processes LogStruct types, hashes, and plain messages with appropriate colorization.
The formatter is automatically enabled in development when enable_color_output
is true (default), providing zero-configuration enhanced logging experience.
Instance Method Summary collapse
Constructor Details
#initialize(color_map: nil, **args) ⇒ void
53 54 55 56 57 58 59 |
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 53 def initialize(color_map: nil, **args) super(**args) @logstruct_formatter = T.let(LogStruct::Formatter.new, LogStruct::Formatter) # Set up custom color mapping @custom_colors = T.let(color_map || default_color_map, T::Hash[Symbol, Symbol]) end |
Instance Method Details
#call(log, logger) ⇒ String
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/log_struct/semantic_logger/color_formatter.rb', line 62 def call(log, logger) # Handle LogStruct types specially with colorization if log.payload.is_a?(LogStruct::Log::Interfaces::CommonFields) formatted = format_logstruct_payload(log) formatted if formatted elsif log.payload.is_a?(Hash) || log.payload.is_a?(T::Struct) formatted = format_logstruct_payload(log) formatted if formatted else # For plain messages, use SemanticLogger's default colorization super end end |