Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efb35195f1 | ||
|
|
fc0898d70e | ||
|
|
011588a712 | ||
|
|
ddc81dd8fe | ||
|
|
74c5a42c5a | ||
|
|
ff29e05322 |
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# claude
|
# claude
|
||||||
CLAUDE.md
|
.claude/
|
||||||
|
|
||||||
# python
|
# python
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
@@ -13,11 +13,13 @@ and emit; their records flow into the handlers `log_setup` wired.
|
|||||||
## Install
|
## Install
|
||||||
|
|
||||||
```
|
```
|
||||||
log_setup @ git+ssh://git@git.rethinkstudios.io/rethink-public/log_setup.git@v0.3.0
|
log_setup @ git+ssh://git@git.rethinkstudios.io/rethink-public/log_setup.git@v0.3.2
|
||||||
```
|
```
|
||||||
|
|
||||||
No dependencies — stdlib only.
|
No dependencies — stdlib only.
|
||||||
|
|
||||||
|
Drop the `@v0.3.2` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -177,4 +179,4 @@ color formatting, per-logger filters, remote handlers.
|
|||||||
|
|
||||||
## Versioning
|
## Versioning
|
||||||
|
|
||||||
Tagged `vX.Y.Z`. Pin the tag.
|
Releases are tagged `vX.Y.Z`. The install line above pins a release; drop the `@vX.Y.Z` suffix to install the latest unpinned. Pin deliberately for reproducible installs.
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "log_setup"
|
name = "log_setup"
|
||||||
version = "0.3.0"
|
version = "0.3.2"
|
||||||
description = "stdlib app-entry-point logging setup: live run.log, rotation, gzip, retention, consistent format"
|
description = "stdlib app-entry-point logging setup: live run.log, rotation, gzip, retention, consistent format"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = []
|
dependencies = []
|
||||||
|
|||||||
@@ -19,4 +19,4 @@ from .setup import setup_logging
|
|||||||
|
|
||||||
__all__ = ["setup_logging"]
|
__all__ = ["setup_logging"]
|
||||||
|
|
||||||
__version__ = "0.3.0"
|
__version__ = "0.3.2"
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ DEFAULT_FORMAT = "%(asctime)s | %(name)s | %(levelname)s | %(message)s"
|
|||||||
DEFAULT_DATEFMT = "%Y-%m-%d %H:%M:%S"
|
DEFAULT_DATEFMT = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
_RESERVED = frozenset(vars(logging.makeLogRecord({})).keys()) | {"message", "asctime"}
|
_RESERVED = frozenset(vars(logging.makeLogRecord({})).keys()) | {"message", "asctime"}
|
||||||
|
# this formatter's own canonical output keys — stdlib's LogRecord rejects `extra` keys
|
||||||
|
# colliding with real attribute names (e.g. `module`), but `time`/`level` are NOT
|
||||||
|
# LogRecord attrs, so a caller's extra={"time":...}/{"level":...} would otherwise
|
||||||
|
# overwrite the UTC timestamp / levelname. guard them explicitly
|
||||||
|
_OUTPUT_KEYS = frozenset({"time", "level", "module", "message"})
|
||||||
|
|
||||||
|
|
||||||
class JsonLinesFormatter(logging.Formatter):
|
class JsonLinesFormatter(logging.Formatter):
|
||||||
@@ -38,10 +43,14 @@ class JsonLinesFormatter(logging.Formatter):
|
|||||||
"message": record.getMessage(),
|
"message": record.getMessage(),
|
||||||
}
|
}
|
||||||
for key, value in record.__dict__.items():
|
for key, value in record.__dict__.items():
|
||||||
if key not in _RESERVED and not key.startswith("_"):
|
if key not in _RESERVED and key not in _OUTPUT_KEYS and not key.startswith("_"):
|
||||||
payload[key] = value
|
payload[key] = value
|
||||||
if record.exc_info:
|
if record.exc_info:
|
||||||
payload["exc_info"] = self.formatException(record.exc_info)
|
# cache the rendered traceback on the record (as stdlib Formatter does) so a
|
||||||
|
# second handler/format() of the same record doesn't re-render it
|
||||||
|
if not record.exc_text:
|
||||||
|
record.exc_text = self.formatException(record.exc_info)
|
||||||
|
payload["exc_info"] = record.exc_text
|
||||||
elif record.exc_text:
|
elif record.exc_text:
|
||||||
payload["exc_info"] = record.exc_text
|
payload["exc_info"] = record.exc_text
|
||||||
if record.stack_info:
|
if record.stack_info:
|
||||||
|
|||||||
@@ -13,6 +13,25 @@ import time
|
|||||||
from typing import Callable, Optional, Tuple
|
from typing import Callable, Optional, Tuple
|
||||||
|
|
||||||
|
|
||||||
|
def _move(source: str, dest: str) -> None:
|
||||||
|
"""rename source to dest, falling back to copy+unlink across filesystems
|
||||||
|
|
||||||
|
os.replace is atomic but raises OSError(EXDEV) when source and dest are on
|
||||||
|
different filesystems — exactly the container bind-mount / separate-logs-volume
|
||||||
|
case this lib targets. fall back to shutil.move (copy+unlink) so the roll still
|
||||||
|
lands instead of failing every rotation via the handler's silent handleError.
|
||||||
|
|
||||||
|
precondition: `dest` is a free, non-directory path (all call sites generate a unique
|
||||||
|
timestamped/dated dest). os.replace and shutil.move differ on a dest that already
|
||||||
|
exists as a directory, so this helper is not safe for arbitrary dests — only the
|
||||||
|
rotation paths that guarantee a fresh file dest.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
os.replace(source, dest)
|
||||||
|
except OSError:
|
||||||
|
shutil.move(source, dest)
|
||||||
|
|
||||||
|
|
||||||
def make_namer(log_dir: str, compress: bool) -> Callable[[str], str]:
|
def make_namer(log_dir: str, compress: bool) -> Callable[[str], str]:
|
||||||
"""namer: redirect a rolled filename into log_dir, adding .gz when compressing
|
"""namer: redirect a rolled filename into log_dir, adding .gz when compressing
|
||||||
|
|
||||||
@@ -46,7 +65,7 @@ def make_rotator(
|
|||||||
shutil.copyfileobj(src, dst)
|
shutil.copyfileobj(src, dst)
|
||||||
os.remove(source)
|
os.remove(source)
|
||||||
else:
|
else:
|
||||||
os.replace(source, dest)
|
_move(source, dest)
|
||||||
if log_dir is not None and prune_stem is not None:
|
if log_dir is not None and prune_stem is not None:
|
||||||
prune(log_dir, prune_stem, backup_count)
|
prune(log_dir, prune_stem, backup_count)
|
||||||
return rotator
|
return rotator
|
||||||
@@ -62,14 +81,21 @@ def rotate_on_start(live_path: str, log_dir: str, compress: bool, clock=time.loc
|
|||||||
return
|
return
|
||||||
stem = os.path.splitext(os.path.basename(live_path))[0]
|
stem = os.path.splitext(os.path.basename(live_path))[0]
|
||||||
stamp = time.strftime("%Y-%m-%d_%H-%M-%S", clock())
|
stamp = time.strftime("%Y-%m-%d_%H-%M-%S", clock())
|
||||||
dest = os.path.join(log_dir, f"{stem}.{stamp}.log")
|
suffix = ".log.gz" if compress else ".log"
|
||||||
|
# the stamp is 1-second resolution; two starts in the same second would collide
|
||||||
|
# and the second clobber the first. disambiguate with a numeric counter so a rapid
|
||||||
|
# crash-restart loop doesn't lose the earlier rolled file
|
||||||
|
dest = os.path.join(log_dir, f"{stem}.{stamp}{suffix}")
|
||||||
|
counter = 1
|
||||||
|
while os.path.exists(dest):
|
||||||
|
dest = os.path.join(log_dir, f"{stem}.{stamp}.{counter}{suffix}")
|
||||||
|
counter += 1
|
||||||
if compress:
|
if compress:
|
||||||
dest += ".gz"
|
|
||||||
with open(live_path, "rb") as src, gzip.open(dest, "wb") as dst:
|
with open(live_path, "rb") as src, gzip.open(dest, "wb") as dst:
|
||||||
shutil.copyfileobj(src, dst)
|
shutil.copyfileobj(src, dst)
|
||||||
os.remove(live_path)
|
os.remove(live_path)
|
||||||
else:
|
else:
|
||||||
os.replace(live_path, dest)
|
_move(live_path, dest)
|
||||||
|
|
||||||
|
|
||||||
def prune(log_dir: str, stem: str, backup_count: int) -> None:
|
def prune(log_dir: str, stem: str, backup_count: int) -> None:
|
||||||
|
|||||||
+25
-6
@@ -12,7 +12,7 @@ import atexit
|
|||||||
import logging
|
import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import os
|
import os
|
||||||
import queue
|
import queue as _queue
|
||||||
from typing import Dict, Optional, Union
|
from typing import Dict, Optional, Union
|
||||||
|
|
||||||
from .formats import build_formatter
|
from .formats import build_formatter
|
||||||
@@ -22,10 +22,15 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
_MARKER = "_log_setup_owned"
|
_MARKER = "_log_setup_owned"
|
||||||
_listener = None
|
_listener = None
|
||||||
|
_atexit_registered = False
|
||||||
|
|
||||||
|
|
||||||
def _level_value(level: Union[int, str]) -> int:
|
def _level_value(level: Union[int, str]) -> int:
|
||||||
"""coerce a level name or int to a logging level int (defaults to INFO)"""
|
"""coerce a level name or int to a logging level int (defaults to INFO)"""
|
||||||
|
if isinstance(level, bool):
|
||||||
|
# bool is an int subclass (True==1, below DEBUG) but is never a real level —
|
||||||
|
# reject it consistently with the per-module path rather than set level 1
|
||||||
|
return logging.INFO
|
||||||
if isinstance(level, int):
|
if isinstance(level, int):
|
||||||
return level
|
return level
|
||||||
if not isinstance(level, str):
|
if not isinstance(level, str):
|
||||||
@@ -76,6 +81,14 @@ def _clear_owned(root: logging.Logger) -> None:
|
|||||||
global _listener
|
global _listener
|
||||||
if _listener is not None:
|
if _listener is not None:
|
||||||
_listener.stop()
|
_listener.stop()
|
||||||
|
# the listener owns the real file/console handlers (only the QueueHandler is
|
||||||
|
# root-attached + marked); stopping it doesn't close them, so close them here
|
||||||
|
# to avoid relying on GC finalizers across a re-setup
|
||||||
|
for wrapped in getattr(_listener, "handlers", ()):
|
||||||
|
try:
|
||||||
|
wrapped.close()
|
||||||
|
except Exception:
|
||||||
|
log.warning("log_setup: failed to close queued handler %r", wrapped, exc_info=True)
|
||||||
_listener = None
|
_listener = None
|
||||||
for handler in list(root.handlers):
|
for handler in list(root.handlers):
|
||||||
if getattr(handler, _MARKER, False):
|
if getattr(handler, _MARKER, False):
|
||||||
@@ -83,7 +96,9 @@ def _clear_owned(root: logging.Logger) -> None:
|
|||||||
try:
|
try:
|
||||||
handler.close()
|
handler.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
# a handler failing to close must not abort re-setup, but log it
|
||||||
|
# rather than swallow silently (consistent with the lib's warn pattern)
|
||||||
|
log.warning("log_setup: failed to close handler %r during re-setup", handler, exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
def _tag(handler: logging.Handler) -> logging.Handler:
|
def _tag(handler: logging.Handler) -> logging.Handler:
|
||||||
@@ -154,7 +169,7 @@ def setup_logging(
|
|||||||
unwritable `log_dir` falls back to console-only with a warning even when `console` is
|
unwritable `log_dir` falls back to console-only with a warning even when `console` is
|
||||||
off, so output is never silently lost; an unknown `output` falls back to text.
|
off, so output is never silently lost; an unknown `output` falls back to text.
|
||||||
"""
|
"""
|
||||||
global _listener
|
global _listener, _atexit_registered
|
||||||
|
|
||||||
root = logging.getLogger()
|
root = logging.getLogger()
|
||||||
root.setLevel(_level_value(level))
|
root.setLevel(_level_value(level))
|
||||||
@@ -186,12 +201,16 @@ def setup_logging(
|
|||||||
handlers.append(sh)
|
handlers.append(sh)
|
||||||
|
|
||||||
if queue:
|
if queue:
|
||||||
record_queue: "queue.Queue" = _make_queue()
|
record_queue: "_queue.Queue" = _make_queue()
|
||||||
qh = _tag(logging.handlers.QueueHandler(record_queue))
|
qh = _tag(logging.handlers.QueueHandler(record_queue))
|
||||||
root.addHandler(qh)
|
root.addHandler(qh)
|
||||||
_listener = logging.handlers.QueueListener(record_queue, *handlers, respect_handler_level=True)
|
_listener = logging.handlers.QueueListener(record_queue, *handlers, respect_handler_level=True)
|
||||||
_listener.start()
|
_listener.start()
|
||||||
|
if not _atexit_registered:
|
||||||
|
# register once — atexit doesn't dedupe, so repeated queue re-setups would
|
||||||
|
# otherwise stack identical callbacks (harmless but unbounded)
|
||||||
atexit.register(_stop_listener)
|
atexit.register(_stop_listener)
|
||||||
|
_atexit_registered = True
|
||||||
else:
|
else:
|
||||||
for handler in handlers:
|
for handler in handlers:
|
||||||
root.addHandler(_tag(handler))
|
root.addHandler(_tag(handler))
|
||||||
@@ -202,9 +221,9 @@ def setup_logging(
|
|||||||
return root
|
return root
|
||||||
|
|
||||||
|
|
||||||
def _make_queue() -> "queue.Queue":
|
def _make_queue() -> "_queue.Queue":
|
||||||
"""unbounded in-memory queue for the QueueHandler -> QueueListener path"""
|
"""unbounded in-memory queue for the QueueHandler -> QueueListener path"""
|
||||||
return queue.Queue(-1)
|
return _queue.Queue(-1)
|
||||||
|
|
||||||
|
|
||||||
def _stop_listener() -> None:
|
def _stop_listener() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user