目录

python-操作-mongodb-输出执行命令的日志

目录

python 操作 mongodb 输出执行命令的日志

  • started()
    • 记录 MongoDB 命令开始执行的内容
  • succeeded()
    • 记录 MongoDB 命令成功执行的内容(当前被注释掉)
  • failed()
    • 记录 MongoDB 命令执行失败的内容
import logging
from pymongo import monitoring

logging.basicConfig(level=logging.DEBUG)

class CommandLogger(monitoring.CommandListener):
    def started(self, event):
        logging.debug(f"MongoDB command started: {event.command_name} - {event.command}")

    def succeeded(self, event):
        logging.info(f"MongoDB command succeeded: {event.command_name} - {event.reply}")

    def failed(self, event):
        logging.error(f"MongoDB command failed: {event.command_name} - {event.failure}")

# 注册监听器
monitoring.register(CommandLogger())