fix: tiered rotate='size' actually rotates and tiers (logsetup-1, logsetup-4)

tiered size mode was broken two ways: (1) stdlib RotatingFileHandler's numbered .1/.2 shift
can't manage files redirected into log_dir, so every roll overwrote slot 1 -> ~99% of
history silently lost; (2) doRollover is gated on backupCount>0, so backup_count=0 (which
the docstring says is ignored in tiered mode) meant NO rotation + unbounded live file.

fix: tiered size now uses a timestamped per-roll namer (make_size_namer) like daily/on_start
so retier manages the pile, and forces a nonzero internal backupCount so the roll always
fires (retier bounds retention, not backupCount). non-tiered size unchanged.

verified: tiered size rotates + bounds to tier total (was stuck at 1); backup_count=0 rotates
+ live file bounded (was unbounded); legacy size back-compat intact; v0.4.0/v0.4.1 suites
still pass. bump v0.4.1 -> v0.4.2

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
2026-06-30 21:04:58 -04:00
parent ece9a6b9ca
commit fb0a8cf799
5 changed files with 45 additions and 6 deletions
+2 -2
View File
@@ -13,12 +13,12 @@ and emit; their records flow into the handlers `log_setup` wired.
## Install
```
log_setup @ git+ssh://git@git.rethinkstudios.io/rethink-public/log_setup.git@v0.4.1
log_setup @ git+ssh://git@git.rethinkstudios.io/rethink-public/log_setup.git@v0.4.2
```
No dependencies — stdlib only.
Drop the `@v0.4.1` suffix from the line above to install the latest unpinned.
Drop the `@v0.4.2` suffix from the line above to install the latest unpinned.
## Quick start
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "log_setup"
version = "0.4.1"
version = "0.4.2"
description = "stdlib app-entry-point logging setup: live run.log, rotation, gzip, retention, consistent format"
requires-python = ">=3.10"
dependencies = []
+1 -1
View File
@@ -19,4 +19,4 @@ from .setup import setup_logging
__all__ = ["setup_logging"]
__version__ = "0.4.1"
__version__ = "0.4.2"
+33
View File
@@ -62,6 +62,30 @@ def make_namer(log_dir: str, compress: bool) -> Callable[[str], str]:
return namer
def make_size_namer(
stem: str, log_dir: str, clock=time.localtime,
) -> Callable[[str], str]:
"""namer for tiered SIZE mode: a unique timestamped dest per roll, plain (no .gz)
stdlib RotatingFileHandler names rolls `<live>.1`, `<live>.2`, ... and shifts them —
a scheme that breaks once files are redirected into log_dir (the shift can't find
them, so every roll reuses slot 1). tiered retention wants unique per-roll names it
can rank + tier like the daily/on_start paths, so ignore the handler's `.N` suffix
entirely and mint `<stem>.<Y-m-d_H-M-S>.log`, disambiguating a same-second collision
(against both the .log and .log.gz forms) with a counter. always plain — retier
decides compression.
"""
def namer(default_name: str) -> str:
stamp = time.strftime("%Y-%m-%d_%H-%M-%S", clock())
dest = os.path.join(log_dir, f"{stem}.{stamp}.log")
counter = 1
while os.path.exists(dest) or os.path.exists(dest + ".gz"):
dest = os.path.join(log_dir, f"{stem}.{stamp}.{counter}.log")
counter += 1
return dest
return namer
def make_rotator(
compress: bool, log_dir: Optional[str] = None,
prune_stem: Optional[str] = None, backup_count: int = 0,
@@ -231,6 +255,7 @@ def attach_rolling(
handler, log_dir: str, compress: bool,
prune_stem: Optional[str] = None, backup_count: int = 0,
keep_uncompressed: Optional[int] = None, keep_compressed: Optional[int] = None,
size_tiered: bool = False,
) -> Tuple[Callable, Callable]:
"""wire the custom namer + rotator onto a rotating handler; return them
@@ -238,7 +263,15 @@ def attach_rolling(
(the handler's own retention can't see the redirected rolled files). pass
`keep_uncompressed`/`keep_compressed` instead to use tiered retention (newest plain,
next gzipped, rest deleted) — see make_rotator.
`size_tiered` uses a timestamped per-roll namer (make_size_namer) instead of the
default one, for a tiered RotatingFileHandler (size mode): stdlib's `.1/.2` numbered
shift can't manage files redirected into log_dir, so each roll gets a unique dated
name that retier ranks/tiers like the daily/on_start paths.
"""
if size_tiered:
namer = make_size_namer(os.path.basename(prune_stem or ""), log_dir)
else:
namer = make_namer(log_dir, compress)
rotator = make_rotator(
compress, log_dir, prune_stem, backup_count, keep_uncompressed, keep_compressed,
+7 -1
View File
@@ -128,12 +128,18 @@ def _file_handler(
"""build the configured file handler with custom rolling into log_dir"""
tiered = keep_uncompressed is not None or keep_compressed is not None
if rotate == "size":
# stdlib doRollover is a no-op when backupCount == 0, and its numbered .1/.2 shift
# can't manage files redirected into log_dir. in tiered size mode force a nonzero
# backupCount so the roll always fires (retier bounds retention, not backupCount)
# and use the timestamped size-namer (size_tiered) instead of the .N shift.
size_backup = backup_count if not tiered else max(backup_count, 1)
handler = logging.handlers.RotatingFileHandler(
live_path, maxBytes=max_bytes, backupCount=backup_count, encoding="utf-8",
live_path, maxBytes=max_bytes, backupCount=size_backup, encoding="utf-8",
)
attach_rolling(
handler, log_dir, compress, prune_stem=name, backup_count=backup_count,
keep_uncompressed=keep_uncompressed, keep_compressed=keep_compressed,
size_tiered=tiered,
)
elif rotate == "daily":
handler = logging.handlers.TimedRotatingFileHandler(