import logging
def create_logger(fp):
#打印日志的时间、日志级别名称、日志信息
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
console_logger = logging.getLogger('ConsoleLoggoer')
file_logger = logging.getLogger('FileLogger')
#向文件输出日志信息
file_handler = logging.FileHandler(fp, mode='a', encoding='utf-8')
file_logger.addHandler(file_handler)
return console_logger, file_logger
# 创建logger
console_logger, file_logger = create_logger(os.path.join('./', 'train.log'))
file_logger.info('Args: %s' % str(123))
file_logger.info('Checkpoint path: %s' % "test")
# 2020-10-25 19:57:12,776 - INFO: Arga: 12
写入文件
logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
level=logging.DEBUG,
filename='test.log',
filemode='a')