Class: RosettAi::StructuredLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/rosett_ai/structured_logger.rb

Overview

Structured logger with per-invocation correlation IDs and JSON/text output.

Drop-in replacement for Ruby's +Logger+. Every log entry includes a correlation ID (UUID v4) generated once per CLI invocation so that all entries from one run can be correlated.

Log format is controlled by +RAI_LOG_FORMAT+:

  • +"text"+ (default): [LEVEL] [correlation_id] message
  • +"json"+: JSON Lines (one JSON object per line)

Examples:

logger = RosettAi::StructuredLogger.new
logger.info("Loading behaviour files")
# Text: [INFO] [a1b2c3d4-...] Loading behaviour files
# JSON: {"timestamp":"...","level":"info","correlation_id":"a1b2c3d4-...","pid":12345,"message":"Loading behaviour files"}

Author:

  • hugo

  • claude

Constant Summary collapse

VALID_FORMATS =
['text', 'json'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stderr, correlation_id: nil) ⇒ StructuredLogger

Creates a new structured logger writing to stderr.

Parameters:

  • output (IO) (defaults to: $stderr)

    output stream (default: $stderr)

  • correlation_id (String, nil) (defaults to: nil)

    explicit correlation ID (default: generates new UUID)



42
43
44
45
46
47
48
49
50
# File 'lib/rosett_ai/structured_logger.rb', line 42

def initialize(output = $stderr, correlation_id: nil)
  super(output)
  @correlation_id = correlation_id || SecureRandom.uuid
  @command = nil
  @mutex = Mutex.new

  self.level = resolve_level
  self.formatter = build_formatter
end

Instance Attribute Details

#commandString?

Returns current command name (set via #command=).

Returns:

  • (String, nil)

    current command name (set via #command=)



36
37
38
# File 'lib/rosett_ai/structured_logger.rb', line 36

def command
  @command
end

#correlation_idString (readonly)

Returns correlation ID for the current invocation (UUID v4).

Returns:

  • (String)

    correlation ID for the current invocation (UUID v4)



33
34
35
# File 'lib/rosett_ai/structured_logger.rb', line 33

def correlation_id
  @correlation_id
end

Instance Method Details

#reset!

This method returns an undefined value.

Reset logger state. Generates a new correlation ID. Intended for test isolation.



64
65
66
67
68
69
# File 'lib/rosett_ai/structured_logger.rb', line 64

def reset!
  @mutex.synchronize do
    @correlation_id = SecureRandom.uuid
    @command = nil
  end
end