This library for Boot projects does something boring in an exciting new way: it configures tools.logging to use SLF4J and logback-classic in classpath isolation, using a pod.
With your logging dependencies isolated, you no longer need to juggle
:exclusions
in your project's dependencies, and hours are added to
your life.
First, craft a Logback configuration in
Hiccup-style XML in your build.boot
, like this:
(def log-config
[:configuration {:scan true, :scanPeriod "10 seconds"}
[:appender {:name "FILE" :class "ch.qos.logback.core.rolling.RollingFileAppender"}
[:encoder [:pattern "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"]]
[:rollingPolicy {:class "ch.qos.logback.core.rolling.TimeBasedRollingPolicy"}
[:fileNamePattern "logs/%d{yyyy-MM-dd}.%i.log"]
[:timeBasedFileNamingAndTriggeringPolicy {:class "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"}
[:maxFileSize "64 MB"]]]
[:prudent true]]
[:appender {:name "STDOUT" :class "ch.qos.logback.core.ConsoleAppender"}
[:encoder [:pattern "%-5level %logger{36} - %msg%n"]]
[:filter {:class "ch.qos.logback.classic.filter.ThresholdFilter"}
[:level "INFO"]]]
[:root {:level "INFO"}
[:appender-ref {:ref "FILE"}]
[:appender-ref {:ref "STDOUT"}]]
[:logger {:name "user" :level "ALL"}]
[:logger {:name "boot.user" :level "ALL"}]])
Then, add tools.logging and this library to your dependencies:
(set-env! :dependencies '[[org.clojure/tools.logging "0.3.1"]
[adzerk/boot-logservice "X.Y.Z"]])
Next, require
things:
(require '[adzerk.boot-logservice :as log-service]
'[clojure.tools.logging :as log])
Initialize the log service and configure tools.logging
to use it:
(alter-var-root #'log/*logger-factory* (constantly (log-service/make-factory log-config)))
Note:
make-factory
defaults to printing to STDOUT when nolog-config
is supplied.
Wow:
(log/info "Wow")
clojure.tools.logging/error
takes an Exception argument that is
converted to a string before logging. This is necessary because only
print-readable objects can pass through the pod to the underlying
logger. It's not clear to us yet if this limitation prohibits anything important.
Much was learned from the logging configurations in the Pedestal samples.
Copyright Adzerk
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.