Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging raw queries Generated by MongoEngine

I need to log all the queries generated by MongoEngine in my Django app.

I cannot use cursor._query() as I need to log queries for all the MongoEngine Documents.

The logging should be done at app level and not DB level so setting MongoDB profiling level to 2 will not help.

I found this gist, which seems helpful but I fail to understand it.

Is there a simpler way to write a middleware or any other way I can log all the queries.

like image 234
Shipra Avatar asked Nov 14 '16 06:11

Shipra


People also ask

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

What is the difference between PyMongo and MongoEngine?

PyMongo is the low-level driver wrapping the MongoDB API into Python and delivering JSON in and out. MongoEngine or other layers like MongoKit map your MongoDB-based data to objects similar to native Python database drivers + SQLAlchemy as ORM.


1 Answers

I used pymongo.monitoring in my project.

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())

More detailed see:

  • pymongo documentation
  • info about profiler
like image 151
Peter Avatar answered Sep 21 '22 07:09

Peter