Liking cljdoc? Tell your friends :D

Timbre Setup Guide

If you are a com.taoensso/timbre user, this guide will help you setup a portal instance dedicated to your timbre logs.

Timbre Log Viewer

Setup

To get started, you need the following namespaces:

(ns user
  (:require [clojure.datafy :as d]
            [clojure.instant :as i]
            [portal.api :as p]
            [taoensso.timbre :as log]))

Next, you need to map timbre log data to something that can be used by the portal log viewer. Your timbre logs might have more info, so feel free to customize the mapping to include more data.

(defn log->portal [{:keys [level ?err msg_ timestamp_ ?ns-str ?file context ?line]}]
  (merge
   (when ?err
     {:error (d/datafy ?err)})
   (when-let [ts (force timestamp_)]
     {:time (i/read-instant-date ts)})
   {:level   level
    :ns      (symbol (or ?ns-str ?file "?"))
    :line    (or ?line 1)
    :column  1
    :result  (force msg_)
    :runtime :clj}
   context))

Next, you need to setup an atom to store the rolling set of logs and wire it into timbre:

(defonce logs (atom '()))

(defn log
  "Accumulate a rolling log of 100 entries."
  [log]
  (swap! logs
         (fn [logs]
           (take 100 (conj logs (log->portal log))))))

(defn setup []
  (reset! logs '())
  (log/merge-config!
   {:appenders
    {:memory {:enabled? true :fn log}}}))

Finally, we can open a dedicated instance of portal which will receive the logs:

(defn open []
  (p/open {:window-title "Logs Viewer" :value logs}))

Putting everything together, you can now do the following at the REPL:

(setup)
(open)

(log/info  "my info log")
(log/error (ex-info "my exception" {:hello :world}) "my error log")

ClojureScript Logs

Also, if you are using timbre for cljs logs, you can add the following context to differentiate clj from cljs logs:

(log/with-context+
  {:runtime :cljs}
  (log/info "my cljs log"))
Timbre CLJS Log Viewer

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close