python - 更改配置后收到意外的日志消息

标签 python logging

以前我用过这种日志模式

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
fh = logging.FileHandler("logs.log", 'w', encoding="utf-8")
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
log.addHandler(fh)

我的日志文件有这些消息:

2019-08-21 11:08:08,271 - INFO - Started
2019-08-21 11:08:08,271 - INFO - Connecting to Google Sheets...
2019-08-21 11:08:11,857 - INFO - Successfuly connected to Google Sheet
2019-08-21 11:08:11,869 - ERROR - Not found: 'TG'
2019-08-21 11:08:11,869 - DEBUG - Getting values from Sheets...
2019-08-21 11:08:12,452 - DEBUG - Got new event row: "Flex - Flex"
2019-08-21 11:08:12,453 - DEBUG - Done. Values: 
...

它看起来很丑,我把它改成了这样:

logging.basicConfig(
   level = logging.DEBUG,
   format = '%(asctime)s - %(levelname)s - %(message)s',
   filename = 'logs.log', filemode = 'w'
 )
log = logging.getLogger()

现在我的日志文件看起来像那样

2019-08-21 11:14:02,374 - INFO - Started
2019-08-21 11:14:02,374 - INFO - Connecting to Google Sheets...
2019-08-21 11:14:02,406 - DEBUG - [b'eyJ0eX...jcifQ', b'eyJ...NvbSJ9', b'f7BQ...dE2w']
2019-08-21 11:14:02,407 - INFO - Refreshing access_token
2019-08-21 11:14:03,448 - DEBUG - Starting new HTTPS connection (1): www.googleapis.com:443
2019-08-21 11:14:04,447 - DEBUG - https://www.googleapis.com:443 "GET /drive/v3/files?q=mimeType%3D%27application%2Fvnd.google-apps.spreadsheet%27&pageSize=1000&supportsTeamDrives=True&includeTeamDriveItems=True HTTP/1.1" 200 None
2019-08-21 11:14:04,450 - DEBUG - Starting new HTTPS connection (1): sheets.googleapis.com:443
2019-08-21 11:14:05,782 - DEBUG - https://sheets.googleapis.com:443 "GET /v4/spreadsheets/1q6...cTI?includeGridData=false HTTP/1.1" 200 None
2019-08-21 11:14:05,899 - INFO - Successfuly connected to Google Sheet
2019-08-21 11:14:05,901 - ERROR - Not found: 'TG'
2019-08-21 11:14:05,902 - DEBUG - Getting values from Sheets...
2019-08-21 11:14:06,426 - DEBUG - https://sheets.googleapis.com:443 "GET /v4/spreadsheets/1q6...cTI/values/%D0%9B%D0%B8%D1%81%D1%821 HTTP/1.1" 200 None
2019-08-21 11:14:06,543 - DEBUG - Got new event row: xxx
2019-08-21 11:14:06,544 - DEBUG - Done. Values: xxx
2019-08-21 11:14:06,544 - DEBUG - Getting line...
2019-08-21 11:14:06,550 - DEBUG - Starting new HTTPS connection (1): api.site.com:443
2019-08-21 11:14:07,521 - DEBUG - https://api.site.com:443 "GET /v1/fix...?Id=33 HTTP/1.1" 200 6739

我收到一些我没有在我的代码中使用的请求调试日志

如何关闭它? 我发现这是因为请求模块

最佳答案

来自请求模块的所有日志消息的原因是因为下面的代码,

logging.basicConfig(
   level = logging.DEBUG   # This sets the root logger to DEBUG
 )

logging.basicConfig 更改程序中存在的根记录器的记录器配置

由于您在这里使用了 requests 模块,requests 使用 urllib3 来打印那些调试消息。

修复:

您可以使用您的第一个记录器初始化代码来配置您的记录器,您也可以使用下面的代码,

logging.basicConfig(
   format = '%(asctime)s - %(levelname)s - %(message)s',
   filename = 'logs.log', filemode = 'w'
 )
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)   # Here you are changing the level of your logger alone

关于python - 更改配置后收到意外的日志消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57585520/

相关文章:

python - 需要帮助在 Python 中创建原始套接字

python - docker中的tensorflow镜像是否使用GPU?

logging - Logstash Web UI 未启动

Python 记录器,打印没有父名称的子记录器 %(name)

linux - 远程 rsyslog 输出中的省略号

python - 如何在应用程序每次运行时创建一个新的日志文件?

python - 如何在 Tkinter GUI 中显示从硬盘浏览视频?

python - 六边形网格上的元胞自动机?

python - 如何在 Django 2.0 中为变量提供随机值?

c# - Nlog 日志记录方法