10

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.

3 Answers 3

8

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)
Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't work for me, still using slf4j directly this works instead (.setLevel (Logger/getRootLogger) Level/DEBUG)
7

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.

Comments

7

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.