1. 导入logging库, getLogger()创建日志记录器

import logging
logger = logging.getLogger()

在getLogger()函数中,可以设置日志记录器的名称, 例如:

logger = logging.getLogger(__name__)
## 或者
logger = logging.getLogger('my_logger')

2. logger.setLevel()设置log级别

logger.setLevel(level=logging.DEBUG)
级别示例级别数值
DEBUGlogging.debug10
INFOlogging.info20
WARNINGlogging.warning30
ERRORlogging.error40
CRITICALlogging.critical50

3. FileHandler()设置日志文件路径

file_handler = logging.FileHandler('my_log.log')

4. StreamHandler()将日志输出到控制台

stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)

5. 其他性质

  • propagate: 是否将日志消息传递给上级日志记录器
logger.propagate = False

6. 代码示例

在Geo Transformer代码中, 对logging库中要使用的一些功能, 封装成了一个Logger类, 详情如下:

import logging

import coloredlogs


def create_logger(log_file=None):
    logger = logging.getLogger()
    logger.handlers.clear()
    logger.setLevel(level=logging.DEBUG)
    logger.propagate = False

    format_str = '[%(asctime)s] [%(levelname).4s] %(message)s'

    stream_handler = logging.StreamHandler()
    colored_formatter = coloredlogs.ColoredFormatter(format_str)
    stream_handler.setFormatter(colored_formatter)
    logger.addHandler(stream_handler)

    if log_file is not None:
        file_handler = logging.FileHandler(log_file)
        formatter = logging.Formatter(format_str, datefmt='%Y-%m-%d %H:%M:%S')
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

    return logger


class Logger:
    def __init__(self, log_file=None, local_rank=-1):
        if local_rank == 0 or local_rank == -1:
            self.logger = create_logger(log_file=log_file)
        else:
            self.logger = None

    def debug(self, message):
        if self.logger is not None:
            self.logger.debug(message)

    def info(self, message):
        if self.logger is not None:
            self.logger.info(message)

    def warning(self, message):
        if self.logger is not None:
            self.logger.warning(message)

    def error(self, message):
        if self.logger is not None:
            self.logger.error(message)

    def critical(self, message):
        if self.logger is not None:
            self.logger.critical(message)
Logo

尧米是由西云算力与CSDN联合运营的AI算力和模型开源社区品牌,为基于DaModel智算平台的AI应用企业和泛AI开发者提供技术交流与成果转化平台。

更多推荐