Skip to content

Logging

get_logger

get_logger(name: str = LOGGER_NAME) -> logging.Logger

Return the AutoTuneNet logger.

Importing AutoTuneNet has no side effects: nothing is written to disk unless file logging is enabled, either with enable_file_logging() or by setting the AUTOTUNENET_LOG_FILE environment variable to a file path. Messages go to the console once (the logger does not propagate to the root logger, so they are not printed twice).

Source code in src/autotunenet/logging/logger.py
def get_logger(name: str = LOGGER_NAME) -> logging.Logger:
    """
    Return the AutoTuneNet logger.

    Importing AutoTuneNet has no side effects: nothing is written to disk
    unless file logging is enabled, either with enable_file_logging() or by
    setting the AUTOTUNENET_LOG_FILE environment variable to a file path.
    Messages go to the console once (the logger does not propagate to the
    root logger, so they are not printed twice).
    """
    log = logging.getLogger(name)
    if getattr(log, "_autotunenet_configured", False):
        return log

    log.setLevel(logging.INFO)
    log.propagate = False
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(_FORMATTER)
    log.addHandler(stream_handler)
    log._autotunenet_configured = True

    env_path = os.environ.get("AUTOTUNENET_LOG_FILE")
    if env_path:
        enable_file_logging(env_path, name=name)
    return log

enable_file_logging

enable_file_logging(path: str | None = None, name: str = LOGGER_NAME) -> str

Also write logs to a file and return its path.

Calling it again with the same path does not add a second handler.

Parameters:

Name Type Description Default
path str | None

Log file path. Defaults to ./logs/<timestamp>.log; the directory is created on demand.

None
name str

Logger name.

LOGGER_NAME

Returns:

Type Description
str

The path written to.

Source code in src/autotunenet/logging/logger.py
def enable_file_logging(path: str | None = None, name: str = LOGGER_NAME) -> str:
    """
    Also write logs to a file and return its path.

    Calling it again with the same path does not add a second handler.

    Args:
        path: Log file path. Defaults to `./logs/<timestamp>.log`; the
            directory is created on demand.
        name: Logger name.

    Returns:
        The path written to.
    """
    if path is None:
        path = os.path.join("logs", f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log")
    directory = os.path.dirname(path)
    if directory:
        os.makedirs(directory, exist_ok=True)

    log = get_logger(name)
    target = os.path.abspath(path)
    for handler in log.handlers:
        if isinstance(handler, logging.FileHandler) and handler.baseFilename == target:
            return path
    file_handler = logging.FileHandler(path)
    file_handler.setFormatter(_FORMATTER)
    log.addHandler(file_handler)
    return path