Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Clojure logging level in clojure.tools.logging

Tags:

clojure

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.

like image 372
dan Avatar asked Aug 10 '15 18:08

dan


3 Answers

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.

like image 93
Nicolas Modrzyk Avatar answered Oct 13 '22 02:10

Nicolas Modrzyk


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")
like image 37
Aaron Blenkush Avatar answered Oct 13 '22 02:10

Aaron Blenkush


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)
like image 31
henryw374 Avatar answered Oct 13 '22 00:10

henryw374