跳到内容

日志记录

smac.utils.logging #

get_logger #

get_logger(logger_name: str) -> Logger

按名称获取日志记录器。

源代码位于 smac/utils/logging.py
def get_logger(logger_name: str) -> logging.Logger:
    """Get the logger by name."""
    logger = logging.getLogger(logger_name)
    return logger

setup_logging #

setup_logging(
    level: int | Path | Literal[False] | None = False,
) -> None

为所有模块设置日志记录配置。

参数#

level : int | Path | Literal[False] | None, 默认为 None 一个表示日志记录级别的整数。传递路径时可以使用自定义日志记录配置。如果为 False,则不执行日志记录设置。

源代码位于 smac/utils/logging.py
def setup_logging(
    level: int | Path | Literal[False] | None = False,
) -> None:
    """Sets up the logging configuration for all modules.

    Parameters
    ----------
    level : int | Path | Literal[False] | None, defaults to None
        An integer representing the logging level. An custom logging configuration can be used when passing a path.
        If False, no logging setup is performed.
    """
    if level is False:
        return

    if isinstance(level, Path):
        log_filename = level
    else:
        path = Path() / smac.__file__
        log_filename = path.parent / "logging.yml"

    with (log_filename).open("r") as stream:
        config = yaml.safe_load(stream)

    if isinstance(level, int):
        config["root"]["level"] = level
        config["handlers"]["console"]["level"] = level

    logging.config.dictConfig(config)