logging
logging
¶
File-backed best-effort logger — never raises, never disrupts callers.
BestEffortLogger binds a
destination path on construction so any subsystem in any terok-*
package can spin up its own log file with one shared, audited idiom.
Writes soft-fail: a logging error must never take down the caller, so
every write is wrapped and swallowed. Operator-facing stderr output
is run through sanitize_tty so
attacker-influenced strings can't smuggle terminal escapes (CWE-150);
the file-side write keeps the original bytes for forensic review.
__all__ = ['BestEffortLogger']
module-attribute
¶
BestEffortLogger(log_path_fn)
¶
Append timestamped lines to a state-file log; soft-fail on any error.
The destination is supplied as a callable rather than an eager
Path so XDG / env-var overrides applied between construction
and write time still take effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_path_fn
|
Callable[[], Path]
|
Zero-arg callable returning the destination path.
Called on every write so tests overriding |
required |
Bind the destination resolver.
Source code in src/terok_util/logging.py
log(message, *, level='DEBUG')
¶
Append one [timestamp] LEVEL: message line. Never raises.
File creation goes through os.open with mode 0o600 so the
log lands owner-only by construction — atomically, without
relying on the process umask. The mode bits are honoured by
the kernel only on creation; existing files keep whatever perms
they were created with.
Source code in src/terok_util/logging.py
debug(message)
¶
warning(message)
¶
warn_user(component, message)
¶
Print a structured warning to stderr and append it to the log file.
Stderr output is run through
sanitize_tty so attacker
bytes in component / message (e.g. originating from foreign
config files) can't smuggle terminal escapes into the operator's
terminal. The file-side write is unsanitised so the log keeps
the original bytes for forensic review.