File: logging-monitoring.rst

package info (click to toggle)
python-mongoengine 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 7,194; makefile: 57; sh: 17
file content (80 lines) | stat: -rw-r--r-- 2,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
==================
Logging/Monitoring
==================

It is possible to use `pymongo.monitoring <https://pymongo.readthedocs.io/en/stable/api/pymongo/monitoring.html>`_ to monitor
the driver events (e.g: queries, connections, etc). This can be handy if you want to monitor the queries issued by
MongoEngine to the driver.

To use `pymongo.monitoring` with MongoEngine, you need to make sure that you are registering the listeners
**before** establishing the database connection (i.e calling `connect`):

The following snippet provides a basic logging of all command events:

.. code-block:: python

    import logging
    from pymongo import monitoring
    from mongoengine import *

    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    logging.basicConfig(level=logging.DEBUG)


    class CommandLogger(monitoring.CommandListener):

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

        def succeeded(self, event):
            log.debug("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):
            log.debug("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())


    class Jedi(Document):
        name = StringField()


    connect()


    log.info('GO!')

    log.info('Saving an item through MongoEngine...')
    Jedi(name='Obi-Wan Kenobii').save()

    log.info('Querying through MongoEngine...')
    obiwan = Jedi.objects.first()

    log.info('Updating through MongoEngine...')
    obiwan.name = 'Obi-Wan Kenobi'
    obiwan.save()


Executing this prints the following output::

    INFO:root:GO!
    INFO:root:Saving an item through MongoEngine...
    DEBUG:root:Command insert with request id 1681692777 started on server ('localhost', 27017)
    DEBUG:root:Command insert with request id 1681692777 on server ('localhost', 27017) succeeded in 562 microseconds
    INFO:root:Querying through MongoEngine...
    DEBUG:root:Command find with request id 1714636915 started on server ('localhost', 27017)
    DEBUG:root:Command find with request id 1714636915 on server ('localhost', 27017) succeeded in 341 microseconds
    INFO:root:Updating through MongoEngine...
    DEBUG:root:Command update with request id 1957747793 started on server ('localhost', 27017)
    DEBUG:root:Command update with request id 1957747793 on server ('localhost', 27017) succeeded in 455 microseconds

More details can of course be obtained by checking the `event` argument from the `CommandListener`.