【问题标题】:Logging raw queries Generated by MongoEngine记录 MongoEngine 生成的原始查询
【发布时间】:2017-03-27 18:28:24
【问题描述】:

我需要在我的 Django 应用中记录 MongoEngine 生成的所有查询。

我不能使用cursor._query(),因为我需要记录对所有 MongoEngine 文档的查询。

日志记录应该在应用程序级别而不是数据库级别完成,因此将 MongoDB 分析级别设置为 2 将无济于事。

我发现了这个gist,看起来很有帮助,但我没看懂。

有没有更简单的方法来编写中间件或任何其他可以记录所有查询的方法。

【问题讨论】:

  • 您是否设法记录查询?我现在也遇到了同样的问题。

标签: python django mongodb logging mongoengine


【解决方案1】:

我在我的项目中使用了pymongo.monitoring

import logging
from pymongo import monitoring

class CommandLogger(monitoring.CommandListener):

    def started(self, event):
        log.debug("Command {0.command}".format(event))
        logging.info("Command {0.command_name} with request id "
                 "{0.request_id} started on server "
                 "{0.connection_id}".format(event))

    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.info("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())

更详细的见:

【讨论】:

  • 哇!太棒了,谢谢!
猜你喜欢
  • 2014-03-10
  • 2019-03-29
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2015-03-13
  • 2014-02-15
相关资源
最近更新 更多