Reputation: 1299
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.
Upvotes: 9
Views: 1486
Reputation: 1253
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:
Upvotes: 4