Class: LogStruct::SemanticLogger::Logger

Inherits:
SemanticLogger::Logger
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(name = "Application", level: nil, filter: nil) ⇒ void

Parameters:

  • name (String, Symbol, Module, T::Class[T.anything]) (defaults to: "Application")
  • level (Symbol, nil) (defaults to: nil)
  • filter (T.untyped) (defaults to: nil)


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 clear_tags!
  # SemanticLogger doesn't have clear_tags!, use pop_tags instead
  count = ::SemanticLogger.tags.length
  ::SemanticLogger.pop_tags(count) if count > 0
end

#current_tagsArray<String, Symbol>

Ensure compatibility with Rails.logger interface

Returns:

  • (Array<String, Symbol>)


105
106
107
# File 'lib/log_struct/semantic_logger/logger.rb', line 105

def current_tags
  ::SemanticLogger.tags
end

#pop_tags(count = 1) ⇒ void

This method returns an undefined value.

Parameters:

  • count (Integer) (defaults to: 1)


124
125
126
# File 'lib/log_struct/semantic_logger/logger.rb', line 124

def pop_tags(count = 1)
  ::SemanticLogger.pop_tags(count)
end

#push_tags(*tags) ⇒ Array<T.untyped>

Parameters:

  • tags (T.untyped)

Returns:

  • (Array<T.untyped>)


117
118
119
120
121
# File 'lib/log_struct/semantic_logger/logger.rb', line 117

def push_tags(*tags)
  flat = tags.flatten.compact
  flat.each { |tag| ::SemanticLogger.push_tags(tag) }
  flat
end

#tagged(*tags, &block) ⇒ T.untyped

Support for tagged logging

Parameters:

  • tags (T.untyped)
  • block (T.proc.returns(T.untyped))

Returns:

  • (T.untyped)


93
94
95
96
97
98
99
100
101
# File 'lib/log_struct/semantic_logger/logger.rb', line 93

def tagged(*tags, &block)
  # Convert tags to array and pass individually to avoid splat issues
  tag_array = tags.flatten
  if tag_array.empty?
    super(&block)
  else
    super(*T.unsafe(tag_array), &block)
  end
end