Skip to content

Log

NUMERICS = 15 module-attribute

SCATTER = 25 module-attribute

scattering_logger

Create a logger with custom log levels for scattering-related logging.

Parameters:

Name Type Description Default
name str

The name of the logger.

required

Returns:

Type Description
Logger

The logger object.

Source code in yasfpy/log.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def scattering_logger(name):
    """
    Create a logger with custom log levels for scattering-related logging.

    Args:
        name (str): The name of the logger.

    Returns:
        (logging.Logger): The logger object.

    """
    levels = ["NUMERICS", "SCATTER"]
    methods = [_log_for_numerics, _log_for_scatter]
    for i, level in enumerate(levels):
        level_name = level
        level_value = globals()[level]
        method_name = level_name.lower()
        if hasattr(logging, level_name):
            # print('{} already defined in logging module'.format(level_name))
            continue
        if hasattr(logging, method_name):
            # print('{} already defined in logging module'.format(method_name))
            continue
        if hasattr(logging.getLoggerClass(), method_name):
            # print('{} already defined in logger class'.format(method_name))
            continue

        logging.addLevelName(level_value, level_name)
        setattr(logging, level_name, level_value)
        setattr(logging.getLoggerClass(), method_name, methods[i])
        # setattr(logging, method_name, _log_to_root)

    logger = logging.getLogger(name)
    if not logger.hasHandlers():
        formatter = logging.Formatter(fmt="%(levelname)s (%(name)s): %(message)s")
        handler = logging.StreamHandler()
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        # logger.handlers.clear()
    logger.propagate = False

    return logger

Comments