File: log.py

package info (click to toggle)
dataclass-wizard 0.39.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,112 kB
  • sloc: python: 19,560; makefile: 126; javascript: 23
file content (39 lines) | stat: -rw-r--r-- 1,023 bytes parent folder | download
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
from __future__ import annotations
from logging import getLogger, Logger, StreamHandler, DEBUG

from .constants import LOG_LEVEL, PACKAGE_NAME


LOG = getLogger(PACKAGE_NAME)
LOG.setLevel(LOG_LEVEL)


def enable_library_debug_logging(
    debug: bool | int,
    logger: Logger = LOG,
) -> int:
    """
    Enable debug logging for a library logger without touching global logging.

    - Attaches a StreamHandler if none exists
    - Sets logger + handler level
    - Disables propagation to avoid duplicate logs

    Returns the resolved logging level.
    """
    lvl = DEBUG if isinstance(debug, bool) else debug

    logger.setLevel(lvl)

    if not any(isinstance(h, StreamHandler) for h in logger.handlers):
        h = StreamHandler()
        h.setLevel(lvl)
        logger.addHandler(h)
    else:
        # ensure existing stream handlers honor the new level
        for h in logger.handlers:
            if isinstance(h, StreamHandler):
                h.setLevel(lvl)

    logger.propagate = False
    return lvl