|
| 1 | +.. _pymongo-logging: |
| 2 | + |
| 3 | +======= |
| 4 | +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 |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to configure logging options for different |
| 24 | +{+driver-short+} components. |
| 25 | + |
| 26 | +{+driver-short+} supports {+language+}'s native logging library. You can configure the logging |
| 27 | +verbosity for the following components: |
| 28 | + |
| 29 | +- ``pymongo.command``, which logs command operations |
| 30 | +- ``pymongo.connection``, which logs connection management operations |
| 31 | +- ``pymongo.serverSelection``, which logs server selection operations |
| 32 | + |
| 33 | +In addition to configuring these options individually, you can configure the global |
| 34 | +logging level by setting the log level on ``pymongo``. To learn more about the native |
| 35 | +logging library, see the `Python logging library documentation <https://docs.python.org/3/howto/logging.html>`__. |
| 36 | + |
| 37 | +Examples |
| 38 | +-------- |
| 39 | + |
| 40 | +The follwing example sets the global logging level to ``INFO``: |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | + |
| 44 | + import logging |
| 45 | + logging.getLogger("pymongo").setLevel(logging.INFO) |
| 46 | + |
| 47 | +The following example sets the log level on the ``pymongo.command`` component to |
| 48 | +``DEBUG``: |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | + |
| 52 | + import logging |
| 53 | + logging.getLogger("pymongo.command").setLevel(logging.DEBUG) |
| 54 | + |
| 55 | +Configuring Truncation |
| 56 | +---------------------- |
| 57 | + |
| 58 | +If you enable logging for the ``pymongo.command`` component, the resulting logs will |
| 59 | +be truncated after 1000 bytes by default. You can configure this truncation limit |
| 60 | +by setting the ``MONGODB_LOG_MAX_DOCUMENT_LENGTH`` environment variable to your |
| 61 | +desired length, as shown in the following example: |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | + |
| 65 | + import os |
| 66 | + os.environ["MONGODB_LOG_MAX_DOCUMENT_LENGTH"] = "2000" |
0 commit comments