系统信息:
OS:Ubuntu 22.04.1
Python Version :3.11.6
Code
import logging
# 配置日志
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='example.log')
# 创建一个控制台处理程序并设置其级别为DEBUG
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
# 创建一个格式化程序,用于在控制台上显示日志
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# 将格式化程序添加到控制台处理程序
console_handler.setFormatter(console_formatter)
# 将控制台处理程序添加到根日志记录器
logging.getLogger('').addHandler(console_handler)
# 示例日志记录
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
标签:logging,写入,handler,DEBUG,console,日志,message From: https://www.cnblogs.com/IsYuh/p/17963364