Logging in Clojure

2020-05-18 14:33发布

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?

4条回答
淡お忘
2楼-- · 2020-05-18 15:18

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))
查看更多
萌系小妹纸
3楼-- · 2020-05-18 15:28

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-05-18 15:30

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

查看更多
小情绪 Triste *
5楼-- · 2020-05-18 15:31

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/

查看更多
登录 后发表回答