Module: Familia::Logging
- Included in:
- Familia
- Defined in:
- lib/familia/logging.rb
Overview
The Logging module provides logging capabilities for Familia.
Familia uses a custom FamiliaLogger that extends the standard Ruby Logger with a TRACE level for detailed debugging output.
== Log Levels (from most to least verbose):
- TRACE: Extremely detailed debugging (controlled by FAMILIA_TRACE env var)
- DEBUG: Detailed debugging information
- INFO: General informational messages
- WARN: Warning messages
- ERROR: Error messages
- FATAL: Fatal errors that cause termination
== Usage: # Use default logger Familia.info "Connection established" Familia.warn "Cache miss"
# Set custom logger Familia.logger = Logger.new('familia.log')
# Trace-level debugging (requires FAMILIA_TRACE=true) Familia.trace :LOAD, redis_client, "user:123", "from cache"
Class Method Summary collapse
-
.extended(base) ⇒ Object
Thread-safe mutex initialization when module is extended.
Instance Method Summary collapse
-
#debug(message = nil, **context) ⇒ true?
Log a debug message with optional structured context.
-
#error(message = nil, **context) ⇒ true
Log an error message with optional structured context.
-
#info(message = nil, **context) ⇒ true
Log an informational message with optional structured context.
-
#logger ⇒ FamiliaLogger
Get the logger instance, initializing with defaults if not yet set.
-
#logger=(new_logger) ⇒ Logger
Set a custom logger instance.
-
#trace(label, instance_id = nil, ident = nil, extra_context = nil) ⇒ nil
Logs a structured trace message for debugging Familia operations.
-
#warn(msg) ⇒ true
Log a warning message.
Class Method Details
.extended(base) ⇒ Object
Thread-safe mutex initialization when module is extended
179 180 181 |
# File 'lib/familia/logging.rb', line 179 def self.extended(base) base.instance_variable_set(:@logger_mutex, Mutex.new) end |
Instance Method Details
#debug(message = nil, **context) ⇒ true?
Log a debug message with optional structured context.
Only outputs when FAMILIA_DEBUG environment variable is enabled. Supports both simple string messages and structured logging with keyword context for operational observability.
269 270 271 272 |
# File 'lib/familia/logging.rb', line 269 def debug( = nil, **context) return unless Familia.debug? logger.debug(format_log(, context)) end |
#error(message = nil, **context) ⇒ true
Log an error message with optional structured context.
302 303 304 |
# File 'lib/familia/logging.rb', line 302 def error( = nil, **context) logger.error(format_log(, context)) end |
#info(message = nil, **context) ⇒ true
Log an informational message with optional structured context.
286 287 288 |
# File 'lib/familia/logging.rb', line 286 def info( = nil, **context) logger.info(format_log(, context)) end |
#logger ⇒ FamiliaLogger
Get the logger instance, initializing with defaults if not yet set
Thread-safe lazy initialization using double-checked locking to ensure only a single logger instance is created even under concurrent logging calls.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/familia/logging.rb', line 196 def logger # Fast path: return existing logger if already initialized return @logger if @logger # Slow path: thread-safe initialization @logger_mutex.synchronize do @logger ||= FamiliaLogger.new($stderr).tap do |log| log.progname = name log.formatter = LogFormatter.new end end @logger end |
#logger=(new_logger) ⇒ Logger
Set a custom logger instance.
Allows replacing the default FamiliaLogger with any Logger-compatible object. Useful for integrating with application logging frameworks.
Automatically synchronizes the logger to DatabaseLogger if it's loaded, ensuring consistent logging across Familia's middleware stack.
230 231 232 233 234 235 236 237 |
# File 'lib/familia/logging.rb', line 230 def logger=(new_logger) @logger_mutex.synchronize do @logger = new_logger # Auto-sync to DatabaseLogger if loaded (inside mutex for atomicity) DatabaseLogger.logger = new_logger if defined?(DatabaseLogger) end @logger end |
#trace(label, instance_id = nil, ident = nil, extra_context = nil) ⇒ nil
Controlled by FAMILIA_TRACE environment variable (set to '1', 'true', or 'yes')
The instance_id can be a Redis client, Redis::Future, or nil
Logs a structured trace message for debugging Familia operations.
This method only executes when both FAMILIA_TRACE and FAMILIA_DEBUG environment variables are enabled.
326 327 328 329 330 331 |
# File 'lib/familia/logging.rb', line 326 def trace(label, instance_id = nil, ident = nil, extra_context = nil) return unless trace_enabled? && Familia.debug? ident_str = ident.nil? ? '<nil>' : ident.to_s logger.trace format('[%s] %s -> %s <-%s', label, instance_id, ident_str, extra_context) end |
#warn(msg) ⇒ true
Log a warning message.
248 249 250 |
# File 'lib/familia/logging.rb', line 248 def warn(msg) logger.warn(msg) end |