File size: 2,783 Bytes
65295d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """Package logging helpers.
Library code logs through the ``autoslm`` logger and never configures handlers on import (it
attaches a :class:`logging.NullHandler`), so importing AutoSLM stays silent for downstream
applications. The CLI calls :func:`configure_logging` to attach a console handler whose
level is controlled by ``-v/--verbose`` or the ``AUTOSLM_LOG_LEVEL`` environment variable.
"""
from __future__ import annotations
import logging
import os
_ROOT_NAME = "autoslm"
# Attach a NullHandler once so "No handlers could be found" warnings never appear and
# importing the library produces no output unless the app opts in.
_root = logging.getLogger(_ROOT_NAME)
if not any(isinstance(h, logging.NullHandler) for h in _root.handlers):
_root.addHandler(logging.NullHandler())
def get_logger(name: str | None = None) -> logging.Logger:
"""Return a logger under the ``autoslm`` namespace (e.g. ``get_logger(__name__)``)."""
if not name or name == _ROOT_NAME:
return logging.getLogger(_ROOT_NAME)
if name.startswith(_ROOT_NAME + "."):
return logging.getLogger(name)
return logging.getLogger(f"{_ROOT_NAME}.{name}")
def _level_from_env(default: int = logging.WARNING) -> int:
raw = os.environ.get("AUTOSLM_LOG_LEVEL")
if not raw:
return default
raw = raw.strip()
if raw.isdigit():
return int(raw)
# Map names (INFO/DEBUG/...) to ints via the canonical name->level mapping rather
# than logging.getLevelName, whose name->int behaviour is deprecated and which
# returns "Level FOO" for unknown names instead of signalling failure.
return logging.getLevelNamesMapping().get(raw.upper(), default)
def configure_logging(verbosity: int = 0, level: int | None = None) -> None:
"""Attach a console handler to the ``autoslm`` logger and set its level.
``verbosity`` maps repeated ``-v`` flags to levels (0=WARNING, 1=INFO, >=2=DEBUG).
An explicit ``level`` (or the ``AUTOSLM_LOG_LEVEL`` env var) overrides the verbosity mapping.
"""
if level is None:
if os.environ.get("AUTOSLM_LOG_LEVEL"):
level = _level_from_env()
else:
level = {0: logging.WARNING, 1: logging.INFO}.get(verbosity, logging.DEBUG)
logger = logging.getLogger(_ROOT_NAME)
logger.setLevel(level)
# Replace any prior console handler we installed so repeated calls don't stack handlers.
for h in [h for h in logger.handlers if getattr(h, "_autoslm_console", False)]:
logger.removeHandler(h)
handler = logging.StreamHandler() # stderr
handler.setLevel(level)
handler.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
handler._autoslm_console = True # type: ignore[attr-defined]
logger.addHandler(handler)
|