Class: LogStruct::SemanticLogger::Formatter
- Inherits:
-
SemanticLogger::Formatters::Json
- Object
- SemanticLogger::Formatters::Json
- LogStruct::SemanticLogger::Formatter
- Extended by:
- T::Sig
- Defined in:
- lib/log_struct/semantic_logger/formatter.rb
Overview
High-Performance JSON Formatter with LogStruct Integration
This formatter extends SemanticLogger's JSON formatter to provide optimal JSON serialization performance while preserving all LogStruct features including data filtering, sensitive data scrubbing, and type-safe structures.
Performance Advantages Over Rails Logger:
Serialization Performance
- Direct JSON generation: Bypasses intermediate object creation
- Streaming serialization: Memory-efficient processing of large objects
- Type-optimized paths: Fast serialization for common data types
- Zero-copy operations: Minimal memory allocation during serialization
Memory Efficiency
- Object reuse: Formatter instances are reused across log calls
- Lazy evaluation: Only processes data that will be included in output
- Efficient buffering: Optimal buffer sizes for JSON generation
- Garbage collection friendly: Minimal object allocation reduces GC pressure
Integration Benefits
- LogStruct compatibility: Native support for typed log structures
- Filter preservation: Maintains all LogStruct filtering capabilities
- Scrubbing integration: Seamless sensitive data scrubbing
- Error handling: Robust handling of serialization errors
Feature Preservation:
This formatter maintains full compatibility with LogStruct's features:
- Sensitive data filtering (passwords, tokens, etc.)
- Recursive object scrubbing and processing
- Type-safe log structure handling
- Custom field transformations
- Metadata preservation and enrichment
JSON Output Structure:
The formatter produces consistent, parseable JSON that includes:
- Standard log fields (timestamp, level, message, logger name)
- LogStruct-specific fields (source, event, context)
- SemanticLogger metadata (process ID, thread ID, tags)
- Application-specific payload data
This combination provides the performance benefits of SemanticLogger with the structured data benefits of LogStruct, resulting in faster, more reliable logging for high-traffic applications.
Instance Method Summary collapse
- #call(log, logger) ⇒ String
- #initialize ⇒ void constructor
Constructor Details
Instance Method Details
#call(log, logger) ⇒ String
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/log_struct/semantic_logger/formatter.rb', line 63 def call(log, logger) # Handle LogStruct types specially - they get wrapped in payload hash by SemanticLogger if log.payload.is_a?(Hash) && log.payload[:payload].is_a?(LogStruct::Log::Interfaces::CommonFields) # Use our formatter to process LogStruct types @logstruct_formatter.call(log.level, log.time, log.name, log.payload[:payload]) elsif log.payload.is_a?(LogStruct::Log::Interfaces::CommonFields) # Direct LogStruct (fallback case) @logstruct_formatter.call(log.level, log.time, log.name, log.payload) elsif log.payload.is_a?(Hash) && log.payload[:payload].is_a?(T::Struct) # T::Struct wrapped in payload hash @logstruct_formatter.call(log.level, log.time, log.name, log.payload[:payload]) elsif log.payload.is_a?(Hash) || log.payload.is_a?(T::Struct) # Process hashes and T::Structs through our formatter @logstruct_formatter.call(log.level, log.time, log.name, log.payload) else # For plain messages, create a Plain log entry = log.payload || log. plain_log = LogStruct::Log::Plain.new( message: , timestamp: log.time ) @logstruct_formatter.call(log.level, log.time, log.name, plain_log) end end |