输出一个最简单的log
点击查看代码
import logging
# 创建logger对象
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # log等级总开关
# log输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
# 控制台handler
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG) # log等级的开关 DEBUG<INFO<WARNING<ERROR<CRITICAL 设置DEBUG,会展示所有等级log
stream_handler.setFormatter(formatter)
# 添加到logger
logger.addHandler(stream_handler)
# 文件handler
file_handler = logging.FileHandler(r".\logs\test.log")
file_handler.setLevel(logging.DEBUG) # log等级的开关 DEBUG<INFO<WARNING<ERROR<CRITICAL 设置DEBUG,会展示所有等级log
file_handler.setFormatter(formatter)
# 添加到logger
logger.addHandler(file_handler)
# 输出日志
logger.info("info")
logger.debug("debug")
logger.warning("warning")
logger.error("error")
logger.critical("critical")