Skip to content

qten.utils.loggings

Module reference for qten.utils.loggings.

loggings

Logging helpers for QTen internals.

This module keeps logger creation consistent across the package by installing a small stream formatter the first time a named logger is requested.

get_logger

get_logger(
    name: str, show_datetime: bool = False
) -> logging.Logger

Return a configured logger for this package.

Creates or reuses a named logger and, on first use for that logger, attaches a StreamHandler with a fixed format of: [{name}|{level}] {message} or, when requested, [{datetime}|{name}|{level}] {message}.

Parameters:

Name Type Description Default
name str

Logger name, typically __name__ from the caller module.

required
show_datetime bool

Whether to include %(asctime)s in the formatter.

False

Returns:

Type Description
Logger

A logger instance configured with the package formatter.

Examples:

logger = get_logger(__name__, show_datetime=True)
logger.debug("message")
Source code in src/qten/utils/loggings.py
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
def get_logger(name: str, show_datetime: bool = False) -> logging.Logger:
    """
    Return a configured logger for this package.

    Creates or reuses a named logger and, on first use for that logger,
    attaches a StreamHandler with a fixed format of:
    `[{name}|{level}] {message}` or, when requested,
    `[{datetime}|{name}|{level}] {message}`.

    Parameters
    ----------
    name : str
        Logger name, typically `__name__` from the caller module.
    show_datetime : bool, default=False
        Whether to include `%(asctime)s` in the formatter.

    Returns
    -------
    logging.Logger
        A logger instance configured with the package formatter.

    Examples
    --------
    ```python
    logger = get_logger(__name__, show_datetime=True)
    logger.debug("message")
    ```
    """
    logger = logging.getLogger(name)
    if not logger.handlers:
        handler = logging.StreamHandler()
        if show_datetime:
            fmt = "[%(asctime)s|%(name)s|%(levelname)s] %(message)s"
        else:
            fmt = "[%(name)s|%(levelname)s] %(message)s"
        handler.setFormatter(logging.Formatter(fmt))
        logger.addHandler(handler)
        logger.propagate = False
    return logger