【Python深度学习】------使用Logging记录模型训练过程
- [1. 导入logging库, getLogger()创建日志记录器](#1. 导入logging库, getLogger()创建日志记录器)
- [2. logger.setLevel()设置log级别](#2. logger.setLevel()设置log级别)
- [3. FileHandler()设置日志文件路径](#3. FileHandler()设置日志文件路径)
- [4. StreamHandler()将日志输出到控制台](#4. StreamHandler()将日志输出到控制台)
- [5. 其他性质](#5. 其他性质)
- [6. 代码示例](#6. 代码示例)
1. 导入logging库, getLogger()创建日志记录器
python
import logging
logger = logging.getLogger()
在getLogger()函数中,可以设置日志记录器的名称, 例如:
python
logger = logging.getLogger(__name__)
## 或者
logger = logging.getLogger('my_logger')
2. logger.setLevel()设置log级别
python
logger.setLevel(level=logging.DEBUG)
级别 | 示例 | 级别数值 |
---|---|---|
DEBUG | logging.debug | 10 |
INFO | logging.info | 20 |
WARNING | logging.warning | 30 |
ERROR | logging.error | 40 |
CRITICAL | logging.critical | 50 |
3. FileHandler()设置日志文件路径
python
file_handler = logging.FileHandler('my_log.log')
4. StreamHandler()将日志输出到控制台
python
stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)
5. 其他性质
- propagate: 是否将日志消息传递给上级日志记录器
python
logger.propagate = False
6. 代码示例
在Geo Transformer代码中, 对logging库中要使用的一些功能, 封装成了一个Logger类, 详情如下:
python
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)