跳转至

Utils.logger(日志记录) 模块

ppsci.utils.logger

init_logger(name='ppsci', log_file=None, log_level=logging.INFO)

Initialize and get a logger by name.

If the logger has not been initialized, this method will initialize the logger by adding one or two handlers, otherwise the initialized logger will be directly returned. During initialization, a StreamHandler will always be added. If log_file is specified a FileHandler will also be added.

Parameters:

Name Type Description Default
name str

Logger name. Defaults to "ppsci".

'ppsci'
log_file Optional[str]

The log filename. If specified, a FileHandler will be added to the logger. Defaults to None.

None
log_level int

The logger level. Note that only the process of rank 0 is affected, and other processes will set the level to "Error" thus be silent most of the time. Defaults to logging.INFO.

INFO
Source code in ppsci/utils/logger.py
def init_logger(
    name: str = "ppsci",
    log_file: Optional[str] = None,
    log_level: int = logging.INFO,
) -> None:
    """Initialize and get a logger by name.

    If the logger has not been initialized, this method will initialize the logger by
    adding one or two handlers, otherwise the initialized logger will be directly
    returned. During initialization, a StreamHandler will always be added. If `log_file`
    is specified a FileHandler will also be added.

    Args:
        name (str, optional): Logger name. Defaults to "ppsci".
        log_file (Optional[str]): The log filename. If specified, a FileHandler
            will be added to the logger. Defaults to None.
        log_level (int, optional): The logger level. Note that only the process of
            rank 0 is affected, and other processes will set the level to
            "Error" thus be silent most of the time. Defaults to logging.INFO.
    """
    # Add custom log level MESSAGE(25), between WARNING(30) and INFO(20)
    logging.addLevelName(_MESSAGE_LEVEL, "MESSAGE")

    if isinstance(log_level, str):
        log_level = getattr(logging, log_level.upper())

    global _logger

    # get a clean logger
    _logger = logging.getLogger(name)
    _logger.handlers.clear()

    # add stream_handler, output to stdout such as terminal
    stream_formatter = colorlog.ColoredFormatter(
        "%(log_color)s[%(asctime)s] %(name)s %(levelname)s: %(message)s",
        datefmt="%Y/%m/%d %H:%M:%S",
        log_colors=_COLORLOG_CONFIG,
    )
    stream_handler = logging.StreamHandler(stream=sys.stdout)
    stream_handler.setFormatter(stream_formatter)
    stream_handler._name = "stream_handler"
    _logger.addHandler(stream_handler)

    # add file_handler, output to log_file(if specified), only for rank 0 device
    if log_file is not None and dist.get_rank() == 0:
        log_file_folder = os.path.dirname(log_file)
        if len(log_file_folder):
            os.makedirs(log_file_folder, exist_ok=True)
        file_formatter = logging.Formatter(
            "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
            datefmt="%Y/%m/%d %H:%M:%S",
        )
        file_handler = logging.FileHandler(log_file, "a")  # append mode
        file_handler.setFormatter(file_formatter)
        file_handler._name = "file_handler"
        _logger.addHandler(file_handler)

    if dist.get_rank() == 0:
        _logger.setLevel(log_level)
    else:
        _logger.setLevel(logging.ERROR)

    _logger.propagate = False

set_log_level(log_level)

Set logger level, only message of level >= log_level will be printed.

Built-in log level are below:

CRITICAL = 50, FATAL = 50, ERROR = 40, WARNING = 30, WARN = 30, INFO = 20, DEBUG = 10, NOTSET = 0.

Parameters:

Name Type Description Default
log_level int

Log level.

required
Source code in ppsci/utils/logger.py
def set_log_level(log_level: int):
    """Set logger level, only message of level >= `log_level` will be printed.

    Built-in log level are below:

    CRITICAL = 50,
    FATAL = 50,
    ERROR = 40,
    WARNING = 30,
    WARN = 30,
    INFO = 20,
    DEBUG = 10,
    NOTSET = 0.

    Args:
        log_level (int): Log level.
    """
    if dist.get_rank() == 0:
        _logger.setLevel(log_level)
    else:
        _logger.setLevel(logging.ERROR)

info(msg, *args)

Source code in ppsci/utils/logger.py
@ensure_logger
@misc.run_at_rank0
def info(msg, *args):
    _logger.info(msg, *args)

message(msg, *args)

Source code in ppsci/utils/logger.py
@ensure_logger
@misc.run_at_rank0
def message(msg, *args):
    _logger.log(_MESSAGE_LEVEL, msg, *args)

debug(msg, *args)

Source code in ppsci/utils/logger.py
@ensure_logger
@misc.run_at_rank0
def debug(msg, *args):
    _logger.debug(msg, *args)

warning(msg, *args)

Source code in ppsci/utils/logger.py
@ensure_logger
@misc.run_at_rank0
def warning(msg, *args):
    _logger.warning(msg, *args)

error(msg, *args)

Source code in ppsci/utils/logger.py
@ensure_logger
@misc.run_at_rank0
def error(msg, *args):
    _logger.error(msg, *args)

scalar(metric_dict, step, vdl_writer=None, wandb_writer=None, tbd_writer=None)

This function will add scalar data to VisualDL or WandB for plotting curve(s).

Parameters:

Name Type Description Default
metric_dict Dict[str, float]

Metrics dict with metric name and value.

required
step int

The step of the metric.

required
vdl_writer Optional[LogWriter]

VisualDL writer to record metrics. Defaults to None.

None
wandb_writer Optional[run]

Run object of WandB to record metrics. Defaults to None.

None
tbd_writer Optional[SummaryWriter]

Run object of WandB to record metrics. Defaults to None.

None
Source code in ppsci/utils/logger.py
def scalar(
    metric_dict: Dict[str, float],
    step: int,
    vdl_writer: Optional["visualdl.LogWriter"] = None,
    wandb_writer: Optional["wandb.run"] = None,
    tbd_writer: Optional["tbd.SummaryWriter"] = None,
):
    """This function will add scalar data to VisualDL or WandB for plotting curve(s).

    Args:
        metric_dict (Dict[str, float]): Metrics dict with metric name and value.
        step (int): The step of the metric.
        vdl_writer (Optional[visualdl.LogWriter]): VisualDL writer to record metrics. Defaults to None.
        wandb_writer (Optional[wandb.run]): Run object of WandB to record metrics. Defaults to None.
        tbd_writer (Optional[tbd.SummaryWriter]): Run object of WandB to record metrics. Defaults to None.
    """
    if vdl_writer is not None:
        with misc.RankZeroOnly() as is_master:
            if is_master:
                for name, value in metric_dict.items():
                    vdl_writer.add_scalar(name, value, step)

    if wandb_writer is not None:
        with misc.RankZeroOnly() as is_master:
            if is_master:
                wandb_writer.log({"step": step, **metric_dict})

    if tbd_writer is not None:
        with misc.RankZeroOnly() as is_master:
            if is_master:
                for name, value in metric_dict.items():
                    tbd_writer.add_scalar(name, value, global_step=step)