aboutsummaryrefslogtreecommitdiff
path: root/app/logging_kit.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/logging_kit.py')
-rw-r--r--app/logging_kit.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/app/logging_kit.py b/app/logging_kit.py
index 0fe39c2..8950e6a 100644
--- a/app/logging_kit.py
+++ b/app/logging_kit.py
@@ -50,17 +50,27 @@ def log_traceback() -> None:
RETENTION_DAYS = 30
+def stream_path(prefix: str, log_dir: Path = None) -> Path:
+ """The ``<prefix>_YYYYMMDD.log`` stream path, without opening it.
+
+ The one place the stream naming convention is spelled out; callers
+ point users at the file (e.g. a failure's full details) without
+ creating or opening it.
+ """
+ directory = log_dir if log_dir is not None else LOG_DIR
+ return directory / f"{prefix}_{datetime.now():%Y%m%d}.log"
+
+
def day_stream(prefix: str, log_dir: Path = None):
"""Open today's ``<prefix>_YYYYMMDD.log`` stream for appending.
Returns the open text handle (write through write_line so lines are
flushed), or None when the directory/file cannot be opened.
"""
- directory = log_dir if log_dir is not None else LOG_DIR
+ path = stream_path(prefix, log_dir)
try:
- directory.mkdir(parents=True, exist_ok=True)
- return (directory / f"{prefix}_{datetime.now():%Y%m%d}.log"
- ).open("a", encoding="utf-8")
+ path.parent.mkdir(parents=True, exist_ok=True)
+ return path.open("a", encoding="utf-8")
except OSError:
return None