I’ve mentioned in the past how you can configure the MongoDB Java driver output from Java. Most Clojure applications that use MongoDB use a database driver that wraps the official MongoDB Java driver. I personally use monger for a lot of my projects, but also occasionally created my own wrapper. The methods described in this post should be applicable to other Clojure MongoDB drivers as long as they wrap the official MongoDB Java driver. They should also work if your application directly wraps the MongoDB Java driver itself rather than using a more idiomatic driver.
The MongoDB Java driver helpfully logs a lot of information via slf4j under the assumption that your application configures its output to an appropriate place. That’s all very nice if you already use slf4j and have it configured it accordingly. However, if you use Clojure (or you’re me) that’s probably not the case. At least I didn’t set up slf4j in any of my Clojure code.
Enter timbre. Timbre is a logging library for Clojure that optionally interoperates with Java logging libraries like slf4j. You can use the interop for slf4j via the slf4j-timbre library and most importantly for me, control the slf4j logging output from Clojure directly.
The timbre configuration for timbre 5.1 that I use in some of my Clojure projects that interact with MongoDB is as follows:
(timbre/merge-config! {:min-level `[[#{"org.mongodb.*"} :error]]})
This configuration sets a minimum log level of error for any log output from the org.mongodb
namespace. This is the namespace the Java driver uses for logging. Normally you wouldn’t just configure the minimum log level for the MongoDB Java driver, but for several different tools. As a result the timbre configuration for my projets tends to look more like this:
(timbre/merge-config! {:min-level `[[#{"org.mongodb.*"} :error] [#{"clj-ssh.*"} :warn] [#{"*"} :debug]]})
The configuration above sets a default minimum log output level of :debug
. It also sets two additional log levels specific to the org.mongodb
and clj-ssh
namespaces. Obviously if you use other tools or have other namespaces that require special handling, you would add those to the configuration settings as well.
Note that using timbre and slj4f-timbre requires the following dependencies in project.clj:
:dependencies [[org.clojure/clojure "1.10.0"]
[com.taoensso/timbre "5.1.0"]
[com.fzakaria/slf4j-timbre "0.3.14"] ;; Attempt to send all log output through timbre
[org.slf4j/jul-to-slf4j "1.7.14"]]