Class: LogStruct::SemanticLogger::Logger
- Inherits:
-
SemanticLogger::Logger
- Object
- SemanticLogger::Logger
- LogStruct::SemanticLogger::Logger
- Extended by:
- T::Sig
- Defined in:
- lib/log_struct/semantic_logger/logger.rb
Overview
High-Performance Logger with LogStruct Integration
This logger extends SemanticLogger::Logger to provide optimal logging performance while seamlessly integrating with LogStruct's typed logging system.
Key Benefits Over Rails.logger:
Performance
- 10-100x faster than Rails' default logger for high-volume applications
- Non-blocking I/O: Uses background threads for actual log writes
- Minimal memory allocation: Efficient object reuse and zero-copy operations
- Batched writes: Reduces system calls by batching multiple log entries
Reliability
- Thread-safe operations: Safe for use in multi-threaded environments
- Error resilience: Logger failures don't crash your application
- Graceful fallbacks: Continues operating even if appenders fail
Features
- Structured logging: Native support for LogStruct types and hashes
- Rich metadata: Automatic inclusion of process ID, thread ID, timestamps
- Tagged context: Hierarchical tagging for request/job tracking
- Multiple destinations: Simultaneously log to files, STDOUT, cloud services
Development Experience
- Colorized output: Beautiful ANSI-colored logs in development
- Detailed timing: Built-in measurement of log processing time
- Context preservation: Maintains Rails.logger compatibility
Usage Examples
The logger automatically handles LogStruct types, hashes, and plain messages:
logger = LogStruct::SemanticLogger::Logger.new("MyApp")
# LogStruct typed logging (optimal performance)
log_entry = LogStruct::Log::Plain.new(
message: "User authenticated",
source: LogStruct::Source::App,
event: LogStruct::Event::Security
)
logger.info(log_entry)
# Hash logging (automatically structured)
logger.info({
action: "user_login",
user_id: 123,
ip_address: "192.168.1.1"
})
# Plain string logging (backward compatibility)
logger.info("User logged in successfully")
The logger is a drop-in replacement for Rails.logger and maintains full API compatibility while providing significantly enhanced performance.
Direct Known Subclasses
Integrations::GoodJob::Logger, Integrations::Sidekiq::Logger
Instance Method Summary collapse
- #clear_tags! ⇒ void
-
#current_tags ⇒ Array<String, Symbol>
Ensure compatibility with Rails.logger interface.
- #initialize(name = "Application", level: nil, filter: nil) ⇒ void constructor
- #pop_tags(count = 1) ⇒ void
- #push_tags(*tags) ⇒ Array<T.untyped>
-
#tagged(*tags, &block) ⇒ T.untyped
Support for tagged logging.
Constructor Details
#initialize(name = "Application", level: nil, filter: nil) ⇒ void
69 70 71 72 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 69 def initialize(name = "Application", level: nil, filter: nil) # SemanticLogger::Logger expects positional arguments, not named arguments super(name, level, filter) end |
Instance Method Details
#clear_tags! ⇒ void
This method returns an undefined value.
110 111 112 113 114 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 110 def # SemanticLogger doesn't have clear_tags!, use pop_tags instead count = ::SemanticLogger..length ::SemanticLogger.(count) if count > 0 end |
#current_tags ⇒ Array<String, Symbol>
Ensure compatibility with Rails.logger interface
105 106 107 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 105 def ::SemanticLogger. end |
#pop_tags(count = 1) ⇒ void
This method returns an undefined value.
124 125 126 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 124 def (count = 1) ::SemanticLogger.(count) end |
#push_tags(*tags) ⇒ Array<T.untyped>
117 118 119 120 121 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 117 def (*) flat = .flatten.compact flat.each { |tag| ::SemanticLogger.(tag) } flat end |
#tagged(*tags, &block) ⇒ T.untyped
Support for tagged logging
93 94 95 96 97 98 99 100 101 |
# File 'lib/log_struct/semantic_logger/logger.rb', line 93 def tagged(*, &block) # Convert tags to array and pass individually to avoid splat issues tag_array = .flatten if tag_array.empty? super(&block) else super(*T.unsafe(tag_array), &block) end end |