Carefree SL4J/Logback logging in boot projects
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/adzerk
.gitignore
.travis.yml
LICENSE
README.md
boot.properties
build.boot

README.md

boot-logservice Build Status

Clojars Project

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.

Usage

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 no log-config is supplied.

Wow:

(log/info "Wow")

Caveats

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.

Thanks

Much was learned from the logging configurations in the Pedestal samples.

License

Copyright Adzerk

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.