使用 python 日志记录模块设置日志记录级别的两种不同方法之间的区别

difference between 2 different ways to set logging level with python logging module

以下两种设置日志级别的方法有什么区别,是否有特定的用例?

logger.setLevel(logging.DEBUG)

logging.basicConfig(level=logging.DEBUG)

我问这个问题是因为我使用了一个而不是另一个,它导致了这个奇怪的重复日志输出:

(envCSSS) csss@csss-website:~/csss-site-in-dev/csss-site/src$ python3.7 manage.py getmail
2020-01-14 18:24:56 = INFO - [settings.py] BASE_DIR set to /home/csss
2020-01-14 18:24:56 = INFO - [settings.py] DEBUG set to True
2020-01-14 18:24:56 = INFO - [settings.py] ROOT_DIR set to /home/csss/csss-site-in-dev
2020-01-14 18:24:56 = INFO - [settings.py] STATIC_URL set to /STATIC_URL/
2020-01-14 18:24:56 = INFO - [settings.py] STATIC_ROOT set to /home/csss/static_root/
2020-01-14 18:24:56 = INFO - [settings.py] MEDIA_URL set to /MEDIA_URL/
2020-01-14 18:24:56 = INFO - [settings.py] MEDIA_ROOT set to /home/csss/media_root/
2020-01-14 18:24:56 = INFO - [settings.py] FILE_FORM_MASTER_DIR set to form_uploads/form_uploads/
2020-01-14 18:24:56 = INFO - [settings.py] FILE_FORM_UPLOAD_DIR set to form_uploads/form_uploads/temporary_files/
2020-01-14 18:24:56 = INFO - [settings.py] DJANGO_MAILBOX_ATTACHMENT_UPLOAD_TO set to mailbox_attachments/%Y/%m/%d/
2020-01-14 10:24:57 = WARNING - INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
.....................
2020-01-14 10:24:57 = WARNING - Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):

setLevel 导致上述问题,我不得不将其换成 basicConfig 以更正日志记录问题。

文档本身是不言自明的:

logger.setLevel

这里,logger好像是一个Logger的实例。通过使用 setLevel,您正在为此特定记录器设置阈值级别。

logging.basicConfig(level=logging.DEBUG)

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger. The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

如果尚未定义根记录器,调用 basicConfig 将设置带有处理程序和格式化程序的记录器。如果你在这里传递 level 它将被设置为根记录器的级别。

您会注意到 setLevellogger 实例上可用,而 basicConfiglogging 模块上可用。