Module: Shoryuken::Logging

Defined in:
lib/shoryuken/logging.rb,
lib/shoryuken/logging/base.rb,
lib/shoryuken/logging/pretty.rb,
lib/shoryuken/logging/without_timestamp.rb

Overview

Provides logging functionality for Shoryuken. Manages the global logger instance and fiber-local context.

Defined Under Namespace

Classes: Base, Pretty, WithoutTimestamp

Class Method Summary collapse

Class Method Details

.context_storageClass

Returns the Fiber class for fiber-local context storage. Uses Fiber[] and Fiber[]= (Ruby 3.2+) for proper isolation in async environments.

Returns:

  • (Class)

    the Fiber class



38
39
40
# File 'lib/shoryuken/logging.rb', line 38

def self.context_storage
  Fiber
end

.current_contextString?

Returns the current logging context value

Returns:

  • (String, nil)

    the current context or nil if not set



30
31
32
# File 'lib/shoryuken/logging.rb', line 30

def self.current_context
  context_storage[:shoryuken_context]
end

.initialize_logger(log_target = STDOUT) ⇒ Logger

Initializes a new logger instance

Parameters:

  • log_target (IO, String) (defaults to: STDOUT)

    the logging target (file path or IO object)

Returns:

  • (Logger)

    the initialized logger



46
47
48
49
50
51
# File 'lib/shoryuken/logging.rb', line 46

def self.initialize_logger(log_target = STDOUT)
  @logger = Logger.new(log_target)
  @logger.level = Logger::INFO
  @logger.formatter = Pretty.new
  @logger
end

.loggerLogger

Returns the current logger instance, initializing if needed

Returns:

  • (Logger)

    the logger instance



56
57
58
# File 'lib/shoryuken/logging.rb', line 56

def self.logger
  @logger ||= initialize_logger
end

.logger=(log) ⇒ Logger

Sets the logger instance

Parameters:

  • log (Logger, nil)

    the logger to use, or nil for null logger

Returns:

  • (Logger)

    the logger instance



64
65
66
# File 'lib/shoryuken/logging.rb', line 64

def self.logger=(log)
  @logger = log || Logger.new('/dev/null')
end

.with_context(msg) { ... } ⇒ Object

Executes a block with a fiber-local logging context. Uses Fiber storage (Ruby 3.2+) for proper isolation in async environments.

Parameters:

  • msg (String)

    the context message to set

Yields:

  • the block to execute within the context

Returns:

  • (Object)

    the result of the block



19
20
21
22
23
24
25
# File 'lib/shoryuken/logging.rb', line 19

def self.with_context(msg)
  previous = context_storage[:shoryuken_context]
  context_storage[:shoryuken_context] = msg
  yield
ensure
  context_storage[:shoryuken_context] = previous
end