I would like to set the logging level, but am neither familiar with Java logging nor the deprecated contrib library.
(ns com.etc.etc (:require [clojure.tools.logging :as log]))
com.etc.etc=> (log/info "foo")
INFO com.etc.etc.invoke nREPL-worker-1 - foo
nil
com.etc.etc=> (log/debug "bar")
nil
I want to set the logging level such that log/debug
will be output.
clojure.tools.logging requires that you configure logging the java way, which will effectively log4j as the underlying logging framework, most of the time (if you add it/have it as a dependency) and so eventually the only you need to do is to have a log4j.properties file on your class path with the following content:
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c: %m%n
While you can keep going the clojure.tools.logging route, these days quite a few people enjoy timbre.
With timbre, you add it to your project.clj with:
[com.taoensso/timbre "4.1.0"]
Then require it with:
(require '[taoensso.timbre :as timbre])
Finally use it with:
(timbre/debug "hello")
; will print
(def example-config {:level :warn })
(timbre/merge-config! example-config)
; update the configuration
(timbre/debug "hello")
; will not print.
For more detailed configuration options, see here.
If you are using Log4J, here is a quick solution using Java interop.
(ns example
(:require [clojure.tools.logging :as log])
(:import (org.apache.log4j Logger Level)))
(.setLevel (Logger/getLogger (str *ns*)) Level/ALL)
(log/debug "works")
Same as Aaron's answer, but using logback and changing root logger:
(import ch.qos.logback.classic.Logger)
(import ch.qos.logback.classic.Level)
(.setLevel
(org.slf4j.LoggerFactory/getLogger (Logger/ROOT_LOGGER_NAME)) Level/ALL)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With