Skip to content
aakash-anko edited this page May 25, 2026 · 1 revision

log.py

Shared logging utility — provides a single log() function that writes to both stderr (for real-time visibility) and a file (data/codewalk.log) for persistence.


Module-level setup

One-line: Creates data/ directory if missing, configures a file handler for the "codewalk" logger.

Example: Module import

Input: First import of log.py, data/ directory does not exist
Line 12: _log_dir = Path("data")
         → _log_dir = PosixPath("data")

Line 13: _log_dir.mkdir(exist_ok=True)
         → Creates data/ directory on disk (no error if exists)

Line 15: logger = logging.getLogger("codewalk")
         → logger = <Logger codewalk (WARNING)>

Line 16: logger.setLevel(logging.INFO)
         → logger.level = INFO (20)

Line 17: if not logger.handlers:  → True (first import, no handlers yet)

Line 18: fh = logging.FileHandler(_log_dir / "codewalk.log")
         → fh = FileHandler writing to "data/codewalk.log"

Line 19: fh.setFormatter(logging.Formatter("%(asctime)s | %(message)s", datefmt="%H:%M:%S"))
         → Format: "14:32:05 | message text"

Line 20: logger.addHandler(fh)
         → logger now has 1 handler: FileHandler → data/codewalk.log

Result: logger is configured. data/codewalk.log will receive all INFO+ messages.


Function: log(msg)

One-line: Prints a message to stderr and writes it to data/codewalk.log.

Example: Logging a pipeline message

Input: msg = "[parallel] Starting: src/codewalk"
Line 24: print("[parallel] Starting: src/codewalk", file=sys.stderr)
         → stderr output: [parallel] Starting: src/codewalk

Line 25: logger.info("[parallel] Starting: src/codewalk")
         → data/codewalk.log appended: "14:32:05 | [parallel] Starting: src/codewalk"

Return: None (side effects: stderr print + file write)

Clone this wiki locally