Logging in Clojure
Asked Answered
B

5

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?

Balbinder answered 7/3, 2011 at 12:40 Comment(1)
There are two overloads for getLog(): one uses a Class, the other takes a String. You could simply use the String one.Arawak
S
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/

Seibel answered 7/3, 2011 at 13:11 Comment(1)
contrib is deprecated in favor of tools.logging github.com/clojure/tools.loggingShilohshim
M
13

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

Matchlock answered 9/4, 2013 at 13:58 Comment(2)
Actually timbre is what I prefer as out of the box logging! spy and other functionality is very helpful.Kafiristan
Here is another question that covers Timbre logging in more detail: #32311119Vaudevillian
N
9

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

Nickelson answered 12/10, 2011 at 15:18 Comment(0)
B
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))
Bacteriostat answered 7/3, 2011 at 19:42 Comment(0)
W
0

Mulog is library that defines log events as data, with a wide range of publisher for popular log aggregation services, e.g. Elastic Search, Cloudwatch, Kinesis, Prometheus, etc.)

As each event log is data, then searching through logs is far simpler to filter on specific keys (compared to when logs are strings).

mulog/log function is used to define an event message. The first argument is a unique event name, followed by any key/value pairs that are useful to define the contents of the log message.

An example of a simple Mulog event:

mulog/log ::dev-user-ns :message "Example event message" :ns (ns-publics *ns*))

Using a custom publisher, Mulog events can automatically be sent to data inspectors such as Portal.

Weymouth answered 21/9, 2023 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.