将CommandLogger添加到MongoClient - 监控

时间:2017-06-08 15:36:54

标签: mongodb pymongo

我有来自PyMongo的MongoClient。我不知道如何将event_listener添加到现有的MongoClient。这就是我所拥有的:

来自pymongo import MongoClient,监控

class CommandLogger(monitoring.CommandListener):     def开始(自我,事件):         logging.info(“带有请求ID的命令{0.command_name}”                      “{0request_id}在服务器上启动”                      “{} 0.connection_id”。格式(事件))

def succeeded(self, event):
    logging.info("Command {0.command_name} with request id "
                 "{0.request_id} on server {0.connection_id} "
                 "succeeded in {0.duration_micros} "
                 "microseconds".format(event))

def failed(self, event):
    logging.error("Command {0.command_name} with request id "
                 "{0.request_id} on server {0.connection_id} "
                 "failed in {0.duration_micros} "
                 "microseconds".format(event))

monitoring.register(CommandLogger())

def get_mongo_client():
    ...
    ...
    my_mongo_client = MongoClient(connString)

   # Here, I want to add a event_listener - something like this:
   # my_mongo_client = MongoClient(connString, event_listeners=[CommandLogger()])
    return my_mongo_client

如何使用侦听器和连接字符串(uri)创建mongo_client?

1 个答案:

答案 0 :(得分:1)

是的,您注释掉的代码是正确的:

my_mongo_client = MongoClient(connString, event_listeners=[CommandLogger()])

当你尝试时,某些东西没有按预期工作吗?