On this page
In this guide, you can learn how to set up and configure a logger in the MongoDB Java driver.
You will learn how to:
Set up a logger using the Simple Logging Facade For Java (SLF4J)
Configure the log level of your logger
This guide shows how to record events in the driver. If you would like to learn how to use information about the activity of the driver in code, consider reading our guide on monitoring.
This section gives background on the dependencies necessary to set up a logger and provides an example logger setup.
The MongoDB Java driver uses the Simple Logging Facade For Java (SLF4J). SLF4J allows you to specify your logging framework of choice at deployment time. For more information on SLF4J, see the SLF4J documentation.
Setting up a logger is optional. When you start your application the MongoDB
Java driver looks for the
slf4j-api artifact in your classpath. If the driver
can't find the
slf4j-api artifact, the driver logs the following warning with
java.util.logging and disables all further logging:
WARNING: SLF4J not found on the classpath. Logging is disabled for the 'org.mongodb.driver' component
To set up a logger, you must include the following in your project.
A logging framework
For the most popular logging frameworks, there is often a single binding
artifact that lists the
slf4j-api and the logging framework as
dependencies. This means that you can set up a logger by adding one artifact
to your project's dependency list. You will see this in the example below.
A binding is a piece of code that connects the
slf4j-api artifact with a
logging framework. The following example shows how to bind the
to the two most popular logging frameworks, Log4j2 and Logback.
This example shows how to set up your logger. Click the tab corresponding to the logging framework you would like to use in your project.
To configure your logger, you must use the configuration system of the logging framework bound to SLF4J.
In the following example we show how you can use your logging framework's configuration system to set your logger's log level.
A logger's log level specifies a lower bound for how urgent a message must be for the logger to output that message.
This example shows how to configure your logger's log level to INFO. Select the tab corresponding to the logging framework you are using in your project.
Your logger uses logger names to help organize different logging events. Logger
names are strings that form a hierarchy. A logger is an ancestor of another logger if
its name followed by a
"." is a prefix of the other logger's name. For example,
"grandparent" is an ancestor of
"grandparent.parent" which is an
For a concrete example, this is what a logger hierarchy looks like in code.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; ... Logger logger_parent = LoggerFactory.getLogger("parent"); Logger logger_child = LoggerFactory.getLogger("parent.child");
A logger inherits the properties of its ancestor logger and can define its own. You can think of this as similar to class inheritance in Java.
The MongoDB Java driver defines the following logger names to organize different logging events in the driver. Here are the logger names defined in the driver and the logging events they correspond to.
org.mongodb.driver.client: events related to
org.mongodb.driver.cluster: monitoring of MongoDB deployments
org.mongodb.driver.connection: connections and connection pools
org.mongodb.driver.operation: operations, including logging related to automatic retries
org.mongodb.driver.protocol: commands sent to and replies received from MongoDB deployments
org.mongodb.driver.uri: connection string parsing
org.mongodb.driver.management: JMX (Java Management Extensions)
This example shows how to change the log level for a specific driver logger.
We set the root logger to OFF and the
org.mongodb.driver.connection logger to
INFO. This will cause the application to only log messages related to connecting
to a MongoDB instance.
Select the tab corresponding to the logging framework you are using in your project.