Docs Home → Develop Applications → MongoDB Drivers → Java Sync
Logging
On this page
Overview
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.
Set Up a Logger
This section gives background on the dependencies necessary to set up a logger and provides an example logger setup.
Background
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.
The
slf4j-api
artifactA logging framework
A binding
Note
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 slf4j-api
artifact
to the two most popular logging frameworks, Log4j2 and Logback.
Example - Set Up
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.
Tip
Dependency Versions
The following versions listed are illustrative rather than a source of truth. You should check the official documentation for SLF4J and your logging framework of choice for guaranteed up-to-date version information.
Configure Your Logger
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.
Example - Configure
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.
Logger Names
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
ancestor of "grandparent.parent.child"
.
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.authenticator
: authenticationorg.mongodb.driver.client
: events related toMongoClient
instancesorg.mongodb.driver.cluster
: monitoring of MongoDB deploymentsorg.mongodb.driver.connection
: connections and connection poolsorg.mongodb.driver.connection.tls
: TLS/SSLorg.mongodb.driver.operation
: operations, including logging related to automatic retriesorg.mongodb.driver.protocol
: commands sent to and replies received from MongoDB deploymentsorg.mongodb.driver.uri
: connection string parsingorg.mongodb.driver.management
: JMX (Java Management Extensions)
Example - Names
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.