Log configuration, overall I prefer to use clojure.tools.logging
rather than
Timbre. No particular reason, only that I feel more comfortable with it.
Here's a really basic logback.xml
file that you can put in your resources
subdirectory.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{ISO8601,Europe/London} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Show debug logs that originate from our namespace -->
<property name="level" value="DEBUG"/>
<logger name="net.solasistim" level="${level}"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Appropriate dependency vectors for this are as follows.
[org.clojure/tools.logging "0.4.0"]
[ch.qos.logback/logback-classic "1.1.3"]
This is generally sensible, make sure that you change websocket-clj
to the
root of your code. Also note that only INFO or higher messages are shown from
any other code. While debugging, it's not unreasonable to change this level to
<root level="DEBUG">
or <root level="TRACE">
. Or if a library logs too much
at INFO, you can add explit exclusions for the package that issues it, as such:
<logger name="org.eclipse.jetty.server" level="WARN"/>
<logger name="org.eclipse.jetty.util.log" level="WARN"/>
The log pattern will produce lines like this, when using debugf
:
2017-09-28 15:52:48,457 [nREPL-worker-0] DEBUG websocket-clj.util - foo: 42
[%thread]
may be redundant for you, but it can be very useful when you have
asynchronous actions occurring.
You also need these lines in project.clj
.
[org.clojure/tools.logging "0.4.0"]
[ch.qos.logback/logback-classic "1.1.3"]
Personally I always use logging in every project, because non-log methods of output can sometimes suffer from strange race conditions that are generally avoided by using a logging framework. Don't spend hours debugging a problem that turns out to be an illusory artifact of the output function.
%logger{36}
means that the logger will abbreviate package names after 36
characters. This can be useful, or you can just use %logger
to disable
abbreviation.
Note that in the pattern
element, we don't use a \n
to give a newline. I
don't know why, but in every environment I use this in, log statements
automatically receive newline termination. I'm still not entirely clear on why
this happens, because it doesn't happen in a pure Java project with the same
logback.xml
file (although that's using Slf4j). More investigation is needed.