Class: KairosMcp::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/kairos_mcp/logger.rb

Overview

Structured JSON lines logger for KairosChain. Writes to .kairos/logs/kairos.log with daily rotation (max 7 files). All entries pass through SecretRedactor before writing.

Log levels: DEBUG, INFO, WARN, ERROR Entry format: “ts”,“level”,“event”,“source”,“details”,…

Constant Summary collapse

LEVELS =
{ debug: 0, info: 1, warn: 2, error: 3 }.freeze
MAX_ROTATED_FILES =
7
DEFAULT_LOG_DIR =
'.kairos/logs'
DEFAULT_LOG_FILE =
'kairos.log'
SECRET_PATTERNS =

Patterns for secret redaction (applied to serialized JSON). Uses simple patterns compatible with Ruby 3.1+ (no variable-length lookbehind).

[
  # Bare API key formats (provider-specific prefixes)
  /\b(sk-[a-zA-Z0-9]{20,})\b/,
  /\b(anthropic-[a-zA-Z0-9]{20,})\b/,
  /\b(ghp_[a-zA-Z0-9]{20,})\b/,
  /\b(xoxb-[a-zA-Z0-9\-]{20,})\b/,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_dir: nil, level: :info) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • log_dir (String) (defaults to: nil)

    directory for log files (default: .kairos/logs)

  • level (Symbol) (defaults to: :info)

    minimum log level (:debug, :info, :warn, :error)



34
35
36
37
38
39
40
41
# File 'lib/kairos_mcp/logger.rb', line 34

def initialize(log_dir: nil, level: :info)
  @log_dir = log_dir || File.join(Dir.pwd, DEFAULT_LOG_DIR)
  @level = LEVELS[level] || LEVELS[:info]
  @log_path = File.join(@log_dir, DEFAULT_LOG_FILE)
  @mutex = Mutex.new
  @current_date = nil
  @io = nil
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



30
31
32
# File 'lib/kairos_mcp/logger.rb', line 30

def level
  @level
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



30
31
32
# File 'lib/kairos_mcp/logger.rb', line 30

def log_path
  @log_path
end

Instance Method Details

#closeObject

Flush and close the log file.



60
61
62
63
64
65
# File 'lib/kairos_mcp/logger.rb', line 60

def close
  @mutex.synchronize do
    @io&.close
    @io = nil
  end
end

#debug(event, **fields) ⇒ Object



43
44
45
# File 'lib/kairos_mcp/logger.rb', line 43

def debug(event, **fields)
  write_entry(:debug, event, fields)
end

#error(event, **fields) ⇒ Object



55
56
57
# File 'lib/kairos_mcp/logger.rb', line 55

def error(event, **fields)
  write_entry(:error, event, fields)
end

#info(event, **fields) ⇒ Object



47
48
49
# File 'lib/kairos_mcp/logger.rb', line 47

def info(event, **fields)
  write_entry(:info, event, fields)
end

#warn(event, **fields) ⇒ Object



51
52
53
# File 'lib/kairos_mcp/logger.rb', line 51

def warn(event, **fields)
  write_entry(:warn, event, fields)
end