Docs Menu
Docs Home
/ / /
PyMongo

Logging

On this page

  • Overview
  • Examples
  • Configuring Truncation

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 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.

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)

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"

Back

Time Series Data