26

For Java development, I use Slf4j and Logback.

Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.debug("Hello world.");

How to use these two libs in Clojure programs? Majority of Clojure programming doesn't has .class concept (possible of course via AOT).

What do you use for logging in Clojure?

Chiron
  • 20,081
  • 17
  • 81
  • 133
  • There are two overloads for `getLog()`: one uses a `Class`, the other takes a `String`. You could simply use the `String` one. – Joachim Sauer Mar 07 '11 at 12:56

4 Answers4

36

Clojure comes with a logging core library in tools.logging. Add [org.clojure/tools.logging "0.2.3"] to your leiningen project.clj and run $lein deps as usual.

Once you use the library you can start logging away

(use 'clojure.tools.logging)
(warn "something bad happened") 

Now you can also access the logger object and set the required fields, refer to the following article for this (written for the older contrib-lib but the same ideas apply):

http://www.paullegato.com/blog/setting-clojure-log-level/

claj
  • 5,172
  • 2
  • 27
  • 30
pyr
  • 424
  • 4
  • 2
13

Look at this as well https://github.com/ptaoussanis/timbre . It looks very simple and nicely done.

joshua
  • 4,118
  • 3
  • 40
  • 53
  • Actually timbre is what I prefer as out of the box logging! spy and other functionality is very helpful. – claj Oct 23 '14 at 20:04
  • 2
    Here is another question that covers Timbre logging in more detail: http://stackoverflow.com/questions/32310619/clojure-configure-timbre-logging/32340732#32340732 – Alan Thompson Sep 02 '15 at 17:47
9

tools.logging. For details, refer to tools.logging vs clojure.contrib.logging

Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63
5

some excerpts from a one of my projects that uses log4j:

log.clj:

(ns 
    #^{:author "Arthur Ulfeldt", 
       :doc "Polynomial threshold encryption"}
  com.cryptovide.log
  (:gen-class)
  (:use
   clojure.contrib.logging))

...

(def logger (org.apache.log4j.Logger/getLogger "A1"))
(def log-levels (vec ( org.apache.log4j.Level/getAllPossiblePriorities)))

...

(defn start-logging []
  (org.apache.log4j.BasicConfigurator/configure))

main.clj:

(start-logging)
(. logger setLevel (log-levels verbose-level))
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284