Logging
On this page
Overview
In this guide, you can learn how to configure logging options for different PyMongo components.
PyMongo supports Python's native logging library. You can configure the logging verbosity for the following components:
pymongo.command
, which logs command operationspymongo.connection
, which logs connection management operationspymongo.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.
Examples
The follwing example sets the global logging level to INFO
:
import logging logging.getLogger("pymongo").setLevel(logging.INFO)
The following example sets the log level on the pymongo.command
component to
DEBUG
:
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:
import os os.environ["MONGODB_LOG_MAX_DOCUMENT_LENGTH"] = "2000"