Skip to content

DOCSP-46696: Logging page #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MongoDB {+driver-short+} Documentation
Aggregation </aggregation>
Security </security>
Data Formats </data-formats>
Logging </logging>
Third-Party Tools </tools>
FAQ </faq>
Troubleshooting </troubleshooting>
Expand Down
66 changes: 66 additions & 0 deletions source/logging.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.. _pymongo-logging:

=======
Logging
=======

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: debugging, printing

Overview
--------

In this guide, you can learn how to configure logging options for different
{+driver-short+} components.

{+driver-short+} supports {+language+}'s native logging library. You can configure the logging
verbosity for the following components:

- ``pymongo.command``, which logs command operations
- ``pymongo.connection``, which logs connection management operations
- ``pymongo.serverSelection``, which logs server selection operations

In addition to configuring these options individually, you can configure the global
logging level by setting the log level on ``pymongo``. To learn more about the native
logging library, see the `Python logging library documentation <https://docs.python.org/3/howto/logging.html>`__.

Examples
--------

The follwing example sets the global logging level to ``INFO``:

.. code-block:: python

import logging
logging.getLogger("pymongo").setLevel(logging.INFO)

The following example sets the log level on the ``pymongo.command`` component to
``DEBUG``:

.. code-block:: python

import logging
logging.getLogger("pymongo.command").setLevel(logging.DEBUG)

Configuring Truncation
----------------------

If you enable logging for the ``pymongo.command`` component, the resulting logs will
be truncated after 1000 bytes by default. You can configure this truncation limit
by setting the ``MONGODB_LOG_MAX_DOCUMENT_LENGTH`` environment variable to your
desired length, as shown in the following example:

.. code-block:: python

import os
os.environ["MONGODB_LOG_MAX_DOCUMENT_LENGTH"] = "2000"
Loading