Skip to content

Instantly share code, notes, and snippets.

@esuomi
Created August 19, 2022 11:53
Show Gist options
  • Save esuomi/70ceafc4805074f56dc0ffc3691b4957 to your computer and use it in GitHub Desktop.
Save esuomi/70ceafc4805074f56dc0ffc3691b4957 to your computer and use it in GitHub Desktop.
colored logging output for clojure.tools.logging using puget and aviso pretty
; deps:
[org.slf4j/slf4j-api "1.7.36"]
[ch.qos.logback/logback-classic "1.2.11"]
[org.clojure/tools.logging "1.2.4"]
[io.aviso/pretty "1.1.1"]
[mvxcvi/puget "1.3.2"]
; shim code:
(ns mantle.log
(:require [clojure.string :as str]
[clojure.tools.logging.readable :as logr]
[io.aviso.exception :as pretty-exceptions]
[io.aviso.logging :as pretty-logging]
[puget.printer :as puget]))
(defn ^:private install-pretty-print-args
[]
(alter-var-root
#'logr/readable-print-args
(fn [_]
(fn [args]
(for [arg args]
`(puget/pprint-str ~arg {:print-color true})))))))
(defn install-pretty-logging
([]
(install-pretty-logging (-> *ns* ns-name name (str/split #"\." 2) first (str ".*") vector)))
([filters]
(install-pretty-print-args)
(pretty-logging/install-pretty-logging)
(alter-var-root #'pretty-exceptions/*app-frame-names* (constantly filters))))
; install by calling this (preferably just) once:
(install-pretty-logging)
; can also be called with vector of stack frame filter matchers for further formatting of exceptions:
(install-pretty-logging [#"mantle.*" "some.specific.namespace.or.package"])
; log everything through clojure.tools.logging.readable as you normally would:
(logr/info (Exception. "Boom!") {:a ["b" false] "c" #{123 4.6M} 'd (java.util.Date.)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment