1. 使用logging
logging为python的内置模块,用于记录日志。
当运行项目的时候,想要追踪各种事件,比如异常,错误,警告,信息等等,那么就需要使用logging模块。日志记录就是你用来追踪各种事件的方式。
日志级别有五种级别:
- logging.debug(“this is a debug message”). 调试信息
- logging.info(“this is a info message”). 信息级别
- logging.warning(“this is a warning message”). 警告级别
- logging.error(“this is a error message”). 错误级别
- logging.critical(“this is a critical message”). 严重错误级别
下面是官方文档中介绍的日志级别:
级别 | 数值 | 含义 / 使用场景 |
---|---|---|
logging.NOTSET | 0 | 当设置在记录器上时,表示需要向上查找祖先记录器以确定有效级别。若最终仍为NOTSET ,则记录所有事件。当设置在处理器上时,处理所有事件。 |
logging.DEBUG | 10 | 详细信息,通常仅供开发人员诊断问题时使用。 |
logging.INFO | 20 | 用于确认程序按预期正常运行。 |
logging.WARNING | 30 | 表示发生了意外情况,或短期内可能出现问题(如"磁盘空间不足")。此时程序仍正常运行。 |
logging.ERROR | 40 | 由于更严重的问题,软件已无法执行某些功能。 |
logging.CRITICAL | 50 | 严重错误,表明程序本身可能无法继续运行。 |
运行下面的代码:
import logging
logging.debug("this is a debug message")
logging.info("this is a info message")
logging.warning("this is a warning message")
logging.error("this is a error message")
logging.critical("this is a critical message")
输出打印如下:
WARNING:root:this is a warning message
ERROR:root:this is a error message
CRITICAL:root:this is a critical message
控制台中没有打印debug信息,因为默认的日志输出是WARNING及以上级别的日志。
如果想输出debug信息,需要设置日志级别为DEBUG,需要对日志模块进行配置。
import logging
# level=logging.DEBUG表示最低的日志级别是DEBUG,DEBUG及以上的级别日志会输出。
logging.basicConfig(level=logging.DEBUG)
logging.debug("this is a debug message")
logging.info("this is a info message")
logging.warning("this is a warning message")
logging.error("this is a error message")
logging.critical("this is a critical message")
运行结果如下:
DEBUG:root:this is a debug message
INFO:root:this is a info message
WARNING:root:this is a warning message
ERROR:root:this is a error message
CRITICAL:root:this is a critical message
2. 将日志输出到文件
import logging
# level=logging.DEBUG表示最低的日志级别是DEBUG,DEBUG及以上的级别日志会输出。
logging.basicConfig(
filename='my_logs.log',
encoding='utf-8',
# filemode='w'表示每次运行程序都会覆盖原来的日志文件,而不是追加
filemode='w',
# 设置需要输出日志的最低级别
level=logging.DEBUG,
)
logging.debug("this is a debug message")
logging.info("this is a info message")
logging.warning("this is a warning message")
logging.error("this is a error message")
logging.critical("this is a critical message")
这样就将日志输出到my_logs.log文件中了。不会输出到控制台了。
3. 日志格式
日志格式可以自定义,通过format参数进行设置。
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - (Line: %(lineno)d [%(filename)s]) - %(levelname)s - %(message)s (Line: %(lineno)d [%(filename)s])', level=logging.INFO)
logging.info(f'hello this is info message')
logging.warning(f'this is warning message')
运行效果如下:
2025-07-10 15:19:52,639 - root - (Line: 5 [test_logging.py]) - INFO - hello this is info message (Line: 5 [test_logging.py])
2025-07-10 15:19:52,639 - root - (Line: 6 [test_logging.py]) - WARNING - this is warning message (Line: 6 [test_logging.py])
4. 在另一个模块中使用logging
例如,在同一级目录下创建一个名为logging_other_module.py
的文件,内容如下:
import logging
def fun():
logging.info('this is other module')
在main.py
文件中,导入logging_other_module
模块,并调用fun()
函数,运行结果如下:
import logging
import test_logging_other_module
logging.basicConfig(format='%(asctime)s - %(name)s - (Line: %(lineno)d [%(filename)s]) - %(levelname)s - %(message)s (Line: %(lineno)d [%(filename)s])', level=logging.INFO)
logging.info(f'hello this is info message')
logging.warning(f'this is warning message')
test_logging_other_module.fun()
运行结果如下:
2025-07-10 15:25:27,952 - root - (Line: 7 [test_logging.py]) - INFO - hello this is info message (Line: 7 [test_logging.py])
2025-07-10 15:25:27,952 - root - (Line: 8 [test_logging.py]) - WARNING - this is warning message (Line: 8 [test_logging.py])
2025-07-10 15:25:27,952 - root - (Line: 4 [test_logging_other_module.py]) - INFO - this is other module (Line: 4 [test_logging_other_module.py])
可以看到另一个模块的日志输出也显示出来了。不需要在test_logging_other_module.py
中重复配置配置日志模块了。
需要注意的是下面的代码
import logging
import test_logging_other_module
test_logging_other_module.fun()
logging.basicConfig(format='%(asctime)s - %(name)s - (Line: %(lineno)d [%(filename)s]) - %(levelname)s - %(message)s (Line: %(lineno)d [%(filename)s])', level=logging.INFO)
logging.info(f'hello this is info message')
logging.warning(f'this is warning message')
输出结果如下:
WARNING:root:this is warning message
如果在logging.basicConfig
之前已经调用过logging.info
或者logging.warning
,那么logging.basicConfig
将不会生效。