|
| 1 | +.. _pymongo-monitoring-logging: |
| 2 | + |
| 3 | +====================== |
| 4 | +Monitoring and Logging |
| 5 | +====================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: debugging, printing, event, subscribe, listener |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to configure **monitoring** in {+driver-short+} |
| 24 | +by using {+driver-short+}'s callback-based interface. Monitoring is the process of |
| 25 | +gathering information about your application's performance and resource usage as it runs. |
| 26 | +This can help you make informed decisions when designing and debugging your application. |
| 27 | + |
| 28 | +This guide also covers **logging**, which lets you record information obtained during |
| 29 | +monitoring to an external log. |
| 30 | + |
| 31 | +Event Types |
| 32 | +----------- |
| 33 | + |
| 34 | +The driver provides information about your application by emitting events. You can |
| 35 | +listen for driver events to monitor your application. |
| 36 | + |
| 37 | +The type of event that the driver emits depends on the operation being performed. |
| 38 | +The following table describes the types of events that the driver emits: |
| 39 | + |
| 40 | +.. list-table:: |
| 41 | + :header-rows: 1 |
| 42 | + :widths: 30 70 |
| 43 | + |
| 44 | + * - Event Type |
| 45 | + - Description |
| 46 | + * - Command events |
| 47 | + - Events related to MongoDB database commands, such as ``find``, ``insert``, |
| 48 | + ``delete``, and ``count``. To learn how to use {+driver-short+} to run a |
| 49 | + database command, see :ref:`<pymongo-run-command>`. For more information about |
| 50 | + MongoDB database commands, see :manual:`Database Commands </reference/command/>` |
| 51 | + in the {+mdb-server+} manual. |
| 52 | + |
| 53 | + As a security measure, the driver redacts the contents of some |
| 54 | + command events. This protects the sensitive information contained in these command |
| 55 | + events. |
| 56 | + |
| 57 | + * - Server Discovery and Monitoring (SDAM) events |
| 58 | + - Events related to changes in the state of the MongoDB deployment. |
| 59 | + |
| 60 | + * - Connection Pool events |
| 61 | + - Events related to the connection pool held by the driver. |
| 62 | + |
| 63 | +For a complete list of events the driver emits, see the |
| 64 | +`pymongo.monitoring <{+api-root+}pymongo/monitoring.html>`__ API documentation. |
| 65 | + |
| 66 | +Listening for Events |
| 67 | +-------------------- |
| 68 | + |
| 69 | +To monitor an event, you must pass an event listener to your application's ``MongoClient``. |
| 70 | +The following steps describe how to monitor your application by using an event listener: |
| 71 | + |
| 72 | +1. Create a class that inherits from one of the event listener base classes |
| 73 | + provided by {+driver-short+}. The base class you choose depends on the type of event |
| 74 | + you want to monitor. For example, to monitor command events, create a class |
| 75 | + that inherits from ``CommandListener``. |
| 76 | +#. Implement the methods of the base class that correpond to the events you want to monitor. |
| 77 | +#. Pass an instance of your listener class to the ``MongoClient`` constructor. |
| 78 | + |
| 79 | +The following code implements a ``CommandListener`` to listen for command events, a |
| 80 | +``ServerListener`` to listen for SDAM events, and a ``ConnectionPoolListener`` to listen for |
| 81 | +connection pool events: |
| 82 | + |
| 83 | +.. literalinclude:: /includes/monitoring/monitoring.py |
| 84 | + :language: python |
| 85 | + :start-after: start-monitoring |
| 86 | + :end-before: end-monitoring |
| 87 | + :copyable: true |
| 88 | + |
| 89 | +Logging |
| 90 | +------- |
| 91 | + |
| 92 | +{+driver-short+} supports {+language+}'s native logging library. You can configure the logging |
| 93 | +verbosity for the following components: |
| 94 | + |
| 95 | +- ``pymongo.command``, which logs command operations |
| 96 | +- ``pymongo.connection``, which logs connection management operations |
| 97 | +- ``pymongo.serverSelection``, which logs server selection operations |
| 98 | + |
| 99 | +In addition to configuring these options individually, you can configure the global |
| 100 | +logging level by setting the log level on ``pymongo``. To learn more about the native |
| 101 | +logging library, see the `Python logging library documentation <https://docs.python.org/3/howto/logging.html>`__. |
| 102 | + |
| 103 | +Examples |
| 104 | +~~~~~~~~ |
| 105 | + |
| 106 | +The following example sets the global logging level to ``INFO``: |
| 107 | + |
| 108 | +.. code-block:: python |
| 109 | + |
| 110 | + import logging |
| 111 | + logging.getLogger("pymongo").setLevel(logging.INFO) |
| 112 | + |
| 113 | +The following example sets the log level on the ``pymongo.command`` component to |
| 114 | +``DEBUG``: |
| 115 | + |
| 116 | +.. code-block:: python |
| 117 | + |
| 118 | + import logging |
| 119 | + logging.getLogger("pymongo.command").setLevel(logging.DEBUG) |
| 120 | + |
| 121 | +Configuring Truncation |
| 122 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +If you enable logging for the ``pymongo.command`` component, the resulting logs will |
| 125 | +be truncated after 1000 bytes by default. You can configure this truncation limit |
| 126 | +by setting the ``MONGODB_LOG_MAX_DOCUMENT_LENGTH`` environment variable to your |
| 127 | +desired length, as shown in the following example: |
| 128 | + |
| 129 | +.. code-block:: python |
| 130 | + |
| 131 | + import os |
| 132 | + os.environ["MONGODB_LOG_MAX_DOCUMENT_LENGTH"] = "2000" |
| 133 | + |
| 134 | +API Documentation |
| 135 | +----------------- |
| 136 | + |
| 137 | +To learn more about the methods and classes used to monitor events in the driver, see the |
| 138 | +following API documentation: |
| 139 | + |
| 140 | +- `monitoring <{+api-root+}pymongo/monitoring.html>`__ |
| 141 | +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
0 commit comments