本文最后更新于:2025年2月5日 晚上

Python 的 logging 模块的日志级别会影响 mongodb 的 debug 输出结果,本文记录相关内容。

问题描述

在使用 mongodb 过程中,发现有的时候系统会输出大量日志信息记录,例如:

1
2
3
4
5
6
7
8
9
DEBUG:pymongo.connection:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Connection pool ready", "serverHost": "boeye-database", "serverPort": 27017}
DEBUG:pymongo.serverSelection:{"message": "Server selection started", "selector": "Primary()", "operation": "buildinfo", "topologyDescription": "<TopologyDescription id: 67a337a7af112c732060c076, topology_type: Sharded, servers: [<ServerDescription ('boeye-database', 27017) server_type: Mongos, rtt: 0.0041245620013796724>]>", "clientId": {"$oid": "67a337a7af112c732060c076"}}
DEBUG:pymongo.serverSelection:{"message": "Server selection succeeded", "selector": "Primary()", "operation": "buildinfo", "topologyDescription": "<TopologyDescription id: 67a337a7af112c732060c076, topology_type: Sharded, servers: [<ServerDescription ('boeye-database', 27017) server_type: Mongos, rtt: 0.0041245620013796724>]>", "clientId": {"$oid": "67a337a7af112c732060c076"}, "serverHost": "boeye-database", "serverPort": 27017}
DEBUG:pymongo.connection:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Connection checkout started", "serverHost": "boeye-database", "serverPort": 27017}
DEBUG:pymongo.connection:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Connection created", "serverHost": "boeye-database", "serverPort": 27017, "driverConnectionId": 1}
DEBUG:pymongo.connection:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Connection ready", "serverHost": "boeye-database", "serverPort": 27017, "driverConnectionId": 1, "durationMS": 0.01286757799971383}
DEBUG:pymongo.connection:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Connection checked out", "serverHost": "boeye-database", "serverPort": 27017, "driverConnectionId": 1, "durationMS": 0.01822680099576246}
DEBUG:pymongo.command:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Command started", "command": "{\"buildinfo\": 1, \"lsid\": {\"id\": {\"$binary\": {\"base64\": \"pnTBlgQFSIu6WYAtmIecNw==\", \"subType\": \"04\"}}}, \"$clusterTime\": {\"clusterTime\": {\"$timestamp\": {\"t\": 1738749863, \"i\": 14}}, \"signature\": {\"hash\": {\"$binary\": {\"base64\": \"AAAAAAAAAAAAAAAAAAAAAAAAAAA=\", \"subType\": \"00\"}}}}, \"$db\": \"admin\"}", "commandName": "buildinfo", "databaseName": "admin", "requestId": 424238335, "operationId": 424238335, "driverConnectionId": 1, "serverConnectionId": 81220, "serverHost": "boeye-database", "serverPort": 27017}
DEBUG:pymongo.command:{"clientId": {"$oid": "67a337a7af112c732060c076"}, "message": "Command succeeded", "durationMS": 4.216, "reply": "{\"version\": \"7.0.12\", \"gitVersion\": \"b6513ce0781db6818e24619e8a461eae90bc94fc\", \"allocator\": \"tcmalloc\", \"javascriptEngine\": \"mozjs\", \"sysInfo\": \"deprecated\", \"versionArray\": [7, 12], \"openssl\": {\"running\": \"OpenSSL 3.0.2 15 Mar 2022\", \"compiled\": \"OpenSSL 3.0.2 15 Mar 2022\"}, \"buildEnvironment\": {\"distmod\": \"ubuntu2204\", \"distarch\": \"x86_64\", \"cc\": \"/opt/mongodbtoolchain/v4/bin/gcc: gcc (GCC) 11.3.0\", \"ccflags\": \"-Werror -include mongo/platform/basic.h -ffp-contract=off -fasynchronous-unwind-tables -g2 -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -gdwarf-5 -fno-omit-frame-pointer -fno-strict-aliasing -O2 -march=sandybridge -mtune=generic -mprefer-vector-width=128 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-const-variable -Wno-unused-but-set-variable -Wno-missing-braces -fstack-protector-strong -gdwarf64 -Wa,--nocompress-debug-sections -fno-builtin-memcmp -Wimplicit-fallthrough=5\", \"cxx\": \"/opt/mongodbtoolchain/v4/bin/g++: g++ (GC...", "commandName": "buildinfo", "databaseName": "admin", "requestId": 424238335, "operationId": 424238335, "driverConnectionId": 1, "serverConnectionId": 81220, "serverHost": "boeye-database", "serverPort": 27017}

不需要的时候严重影响了对正常日志的查看效率。

问题原因

python logging 包的日志级别设置会影响 mongodb 的输出,主要在于是否允许 DEBUG 信息输出。

示例代码

vvdutils 可以通过 pip install vvdutils 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import logging

from pymongo import MongoClient
from vvdutils import log_init, strong_printing


if __name__ == '__main__':

strong_printing("debug Mongo Client test")
logging.basicConfig(level=logging.DEBUG)
test_conn_debug = MongoClient("abc", 123, username="root", password="root")

strong_printing("info Mongo Client test")
logging.basicConfig(level=logging.INFO, force=True)
test_conn_info = MongoClient("abc", 123, username="root", password="root")

strong_printing("debug log test")
logger_debug = log_init('debug.log', level=logging.DEBUG)
logger_debug("debug log test", level=logging.DEBUG)
logger_debug("info log test", level=logging.INFO)

strong_printing("info log test")
info_debug = log_init('info.log', level=logging.INFO)
info_debug("debug log test ---", level=logging.DEBUG)
info_debug("info log test ---", level=logging.INFO)

logging.basicConfig(level=logging.INFO, force=True)
logger_debug("debug log test @@@", level=logging.DEBUG)
# 以下代码会报错: ValueError: I/O operation on closed file. 调试时可以看到
# info_debug("info log test @@@", level=logging.INFO)
pass

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
########################################
DEBUG MONGO CLIENT TEST
########################################

DEBUG:pymongo.connection:{"clientId": {"$oid": "67a33a7ef8dfcfc062359c0e"}, "message": "Connection pool created", "serverHost": "abc", "serverPort": 123}

########################################
INFO MONGO CLIENT TEST
########################################


########################################
DEBUG LOG TEST
########################################

debug log test
info log test

########################################
INFO LOG TEST
########################################

info log test ---
info log test @@@

注意: logging.basicConfig(level=logging.INFO, force=True) 的 force 参数设置为 True 时会删除并关闭连接到根日志记录器的其他 logging 对象,此时再次操作被关闭的日志对象时会报错:

1
ValueError: I/O operation on closed file.


文章链接:
https://www.zywvvd.com/notes/coding/python/python-logging/python-logging/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Python logging 日志级别影响 mongodb 日志输出
https://www.zywvvd.com/notes/coding/python/python-logging/python-logging/
作者
Yiwei Zhang
发布于
2025年2月5日
许可协议