Make lib.App logging more customizable #55
Loading…
Reference in a new issue
No description provided.
Delete branch "jan/feature/20260810-make-lib-app-logging-more-customizable"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR brings a number of fixes and improvments to logging via the lib.log.log() framework and the way lib.App uses it. It beautifies log output, modernizes the antique log flag handling, and gives more freedom to customize the default logging behaviour for classes inheriting lib.App.
lib.log.log(): Fix early return without flags set
When position, prio, and date are all absent from the log flags, the msg variable is empty. The early return 'if not len(msg): return' would then skip printing the actual message in margs.
Fix by checking both msg and margs before returning. This ensures messages are still printed even when no prefix flags are set.
lib.log.log(): Fix superfluous message whitespace
When prefix flags (position, prio, date) are combined with message arguments, log() would double-space the output (e.g. ' Created') or omit the separator entirely (e.g. 'Created'). The root cause was that log() prepended a space to every argument, regardless of whether the prefix already ended with one.
Fix: only prepend a space before the first argument when the prefix doesn't already end with one. This produces exactly one separator between prefix and content regardless of which flags are active.
Also fix log_m(): skip empty strings (the sentinel '') so it doesn't contribute a space. And fix the early return that dropped messages when no prefix flags were set (previous commit).
lib.log.log(): Use .join() to build argument string
The argument string builder in log() previously iterated with enumerate and checked per-iteration whether to prepend a space. Replace the loop with ' '.join(str(a) for a in args), which is a single C-level operation.
Move the leading-space logic after the only_printable block so the regex transformations see the raw joined content.
lib.log.log(): Convert string-based flags to Flag
Replace the set of flag strings with a Flag enum for better type safety and bitwise operations. Add parse_flags() to convert comma-separated strings into a LogFlag value. Update set_flags() and set_log_flags() to accept str | LogFlag | None.
set_flags() and set_log_flags() don't return str anylonger which is a breaking change, but shouldn't be a problem because it's not used anywhere.
lib.App: Make default env var names overridable
Add default*_env() helper methods that return the environment variable names, and use them in init() instead of hard-coded strings. This allows subclasses to override the names.
lib.App: Make default log values overridable
The init() method reads default log configuration directly from environment variables, making it impossible for subclasses to change defaults.
Extract the defaults into _default_log_flags(), _default_log_level(), _default_log_file(), and _default_show_backtrace() methods, then call them from init(). Subclasses can now override these methods to customize defaults without needing to override the entire init() method.