|
| 1 | +================== |
| 2 | +Logging/Monitoring |
| 3 | +================== |
| 4 | + |
| 5 | +It is possible to use `pymongo.monitoring <https://api.mongodb.com/python/current/api/pymongo/monitoring.html>`_ to monitor |
| 6 | +the driver events (e.g: queries, connections, etc). This can be handy if you want to monitor the queries issued by |
| 7 | +MongoEngine to the driver. |
| 8 | + |
| 9 | +To use `pymongo.monitoring` with MongoEngine, you need to make sure that you are registering the listeners |
| 10 | +**before** establishing the database connection (i.e calling `connect`): |
| 11 | + |
| 12 | +The following snippet provides a basic logging of all command events: |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | +
|
| 16 | + import logging |
| 17 | + from pymongo import monitoring |
| 18 | + from mongoengine import * |
| 19 | +
|
| 20 | + log = logging.getLogger() |
| 21 | + log.setLevel(logging.DEBUG) |
| 22 | + logging.basicConfig(level=logging.DEBUG) |
| 23 | +
|
| 24 | +
|
| 25 | + class CommandLogger(monitoring.CommandListener): |
| 26 | +
|
| 27 | + def started(self, event): |
| 28 | + log.debug("Command {0.command_name} with request id " |
| 29 | + "{0.request_id} started on server " |
| 30 | + "{0.connection_id}".format(event)) |
| 31 | +
|
| 32 | + def succeeded(self, event): |
| 33 | + log.debug("Command {0.command_name} with request id " |
| 34 | + "{0.request_id} on server {0.connection_id} " |
| 35 | + "succeeded in {0.duration_micros} " |
| 36 | + "microseconds".format(event)) |
| 37 | +
|
| 38 | + def failed(self, event): |
| 39 | + log.debug("Command {0.command_name} with request id " |
| 40 | + "{0.request_id} on server {0.connection_id} " |
| 41 | + "failed in {0.duration_micros} " |
| 42 | + "microseconds".format(event)) |
| 43 | +
|
| 44 | + monitoring.register(CommandLogger()) |
| 45 | +
|
| 46 | +
|
| 47 | + class Jedi(Document): |
| 48 | + name = StringField() |
| 49 | +
|
| 50 | +
|
| 51 | + connect() |
| 52 | +
|
| 53 | +
|
| 54 | + log.info('GO!') |
| 55 | +
|
| 56 | + log.info('Saving an item through MongoEngine...') |
| 57 | + Jedi(name='Obi-Wan Kenobii').save() |
| 58 | +
|
| 59 | + log.info('Querying through MongoEngine...') |
| 60 | + obiwan = Jedi.objects.first() |
| 61 | +
|
| 62 | + log.info('Updating through MongoEngine...') |
| 63 | + obiwan.name = 'Obi-Wan Kenobi' |
| 64 | + obiwan.save() |
| 65 | +
|
| 66 | +
|
| 67 | +Executing this prints the following output:: |
| 68 | + |
| 69 | + INFO:root:GO! |
| 70 | + INFO:root:Saving an item through MongoEngine... |
| 71 | + DEBUG:root:Command insert with request id 1681692777 started on server ('localhost', 27017) |
| 72 | + DEBUG:root:Command insert with request id 1681692777 on server ('localhost', 27017) succeeded in 562 microseconds |
| 73 | + INFO:root:Querying through MongoEngine... |
| 74 | + DEBUG:root:Command find with request id 1714636915 started on server ('localhost', 27017) |
| 75 | + DEBUG:root:Command find with request id 1714636915 on server ('localhost', 27017) succeeded in 341 microseconds |
| 76 | + INFO:root:Updating through MongoEngine... |
| 77 | + DEBUG:root:Command update with request id 1957747793 started on server ('localhost', 27017) |
| 78 | + DEBUG:root:Command update with request id 1957747793 on server ('localhost', 27017) succeeded in 455 microseconds |
| 79 | + |
| 80 | +More details can of course be obtained by checking the `event` argument from the `CommandListener`. |
0 commit comments