Module: Clacky::Logger

Defined in:
lib/clacky/utils/logger.rb

Overview

Thread-safe daily-rotating file logger.

Log files are written to ~/.clacky/logger/clacky-YYYY-MM-DD.log. At most 7 daily log files are kept; older ones are pruned automatically.

Usage (anywhere in the codebase):

Clacky::Logger.info("server started")
Clacky::Logger.debug("tool result", tool: "shell", exit_code: 0)
Clacky::Logger.warn("retry attempt", n: 3)
Clacky::Logger.error("unhandled exception", error: e)

Constant Summary collapse

LOG_DIR =
File.join(Dir.home, ".clacky", "logger").freeze
MAX_LOG_FILES =
7
MUTEX =
Mutex.new
LEVELS =

Level constants (numeric, for future filtering)

{ debug: 0, info: 1, warn: 2, error: 3 }.freeze
CONSOLE_MIN_LEVEL =

Minimum level to echo to $stderr when console output is enabled. :debug → all; :info → info/warn/error; :warn → warn/error only

:info

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.console=(value) ⇒ Object (writeonly)

Enable/disable echoing log lines to $stderr (in addition to the file). Call Clacky::Logger.console = true from server startup to activate.



32
33
34
# File 'lib/clacky/utils/logger.rb', line 32

def console=(value)
  @console = value
end

Class Method Details

.current_log_fileObject

Path of the log file currently being written to (today’s file). File may not exist yet if no log has been emitted today — callers should check File.exist? before reading.



41
42
43
# File 'lib/clacky/utils/logger.rb', line 41

def current_log_file
  log_file_path(Time.now)
end

.debug(message, **context) ⇒ Object

Log at DEBUG level.



46
47
48
# File 'lib/clacky/utils/logger.rb', line 46

def debug(message, **context)
  write_log(:debug, message, context)
end

.error(message, **context) ⇒ Object

Log at ERROR level. Accepts an optional :error key that may be an Exception; its backtrace is appended automatically.



62
63
64
# File 'lib/clacky/utils/logger.rb', line 62

def error(message, **context)
  write_log(:error, message, context)
end

.info(message, **context) ⇒ Object

Log at INFO level.



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

def info(message, **context)
  write_log(:info, message, context)
end

.warn(message, **context) ⇒ Object

Log at WARN level.



56
57
58
# File 'lib/clacky/utils/logger.rb', line 56

def warn(message, **context)
  write_log(:warn, message, context)
end