Is it possible to print log statements on clojure repl as well?

1,597 views
Skip to first unread message

Mayank Jain

unread,
Dec 16, 2015, 11:28:40 AM12/16/15
to Clojure
Hi,

I would like to be able to print log statements via clojure.tools.logging to print on clojure repl as well.
I am using log4j logging framework for configurations.
Is this possible? I am unable to figure it out. 
I am open to any other logging framework if that solves this.

Thanks.

Shantanu Kumar

unread,
Dec 16, 2015, 11:43:12 AM12/16/15
to Clojure
If you enabled Log4j console logger, the logs should appear in the REPL too.


You do not necessarily need log4j to log to the REPL via clojure.tools.logging - you can just use c.t.l. without any logging library and it will fall back to java.util.logging using STDOUT.

Shantanu

Mayank Jain

unread,
Dec 16, 2015, 1:17:39 PM12/16/15
to Clojure
Thanks Shantanu for your reply,

It still didn't work.

Here's my project.clj
(defproject log "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :license {:name "Eclipse Public License"
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/tools.logging "0.3.1"]
                 [org.slf4j/slf4j-log4j12 "1.7.13"]])


And here's my resources/log4j.properties file
# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

And here's my the output from my Clojure repl in Emacs:
user> (require '[clojure.tools.logging :as ctl])
nil
user> (ctl/error "Fail!")
nil

And here's what I get on my console
$ lein repl :headless :port 4005
nREPL server started on port 4005 on host 127.0.0.1 - nrepl://127.0.0.1:4005
2015-12-16 23:44:50 ERROR user:288 - Fail!

I would like it to print the same on both Emacs repl and on console (and add to a file).

I can get it to write on console and file but not on repl.

Shantanu Kumar

unread,
Dec 16, 2015, 9:28:37 PM12/16/15
to Clojure
Below is the order in which c.t.l picks up loggers:
1. SLF4j (which also includes Logback, since it natively implements the SLF4j API)
2. Apache Commons-logging
3. Log4j
4. java.util.logging


Both #1 and #2 are merely logger facade, incapable of logging anywhere except to the console by themselves. They need an underlying logger for real-world logging.

Your dependencies only include the SLF4j-log4j bridge (without the log4j dependency), which I am not sure is supposed to be picked up by c.t.l. I would suggest either remove slf4j-log4j dependency altogether (allowing j.u.l to be chosen) or add the slf4j [org.slf4j/slf4j-api "1.7.13"]
 and log4j [log4j/log4j "1.2.17"] dependencies to your current config.

Hope this helps.

Shantanu

Mayank Jain

unread,
Dec 17, 2015, 1:54:36 AM12/17/15
to Clojure
I tried both the solutions, neither worked.

Solution #1 - Use only clojure.tools.logging

My Dependencies
[[org.clojure/clojure "1.7.0"]
 
[org.clojure/tools.logging "0.3.1"]]

Repl Output
> (require '[clojure.tools.logging :as ctl])
nil
> (ctl/error "hi")
nil
> (ctl/info "hi")
nil

Console Output
$ lein repl :headless :port 4005
nREPL server started on port
4005 on host 127.0.0.1 - nrepl://127.0.0.1:4005
Dec 17, 2015 12:07:16 PM clojure.tools.logging$eval406$fn__410 invoke
SEVERE
: hi
Dec 17, 2015 12:07:22 PM clojure.tools.logging$eval406$fn__410 invoke
INFO
: hi


Solution #2 - Add suggested dependencies

My Dependencies
[[org.clojure/clojure "1.7.0"]
 
[org.clojure/tools.logging "0.3.1"]
 
[org.slf4j/slf4j-log4j12 "1.7.13"]

 
[org.slf4j/slf4j-api "1.7.13"]
 
[log4j/log4j "1.2.17"]]

Repl Output
> (require '[clojure.tools.logging :as ctl])
nil
> (ctl/error "hi")
nil

Console Output
$ lein repl :headless :port 4005
nREPL server started on port
4005 on host 127.0.0.1 - nrepl://127.0.0.1:4005
2015-12-17 12:11:50 ERROR user:288 - hi

Am I missing something?

Other infomation:
$ java -version
java version
"1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

$ lein
-v
Leiningen 2.5.3 on Java 1.8.0_45 Java HotSpot(TM) 64-Bit Server VM

Emacs 24.5
OS
: Mac OS 10.10.5

Shantanu Kumar

unread,
Dec 17, 2015, 6:41:45 AM12/17/15
to Clojure
Sorry Mayank, my bad. I didn't realize you were connected via an nREPL client. I guess you need an appender supported by nREPL server to do this kind of a thing.

Shantanu
Reply all
Reply to author
Forward
0 new messages