Created
February 5, 2018 15:15
-
-
Save wdullaer/086a173c549d7de1f7b9c5a72f3c9297 to your computer and use it in GitHub Desktop.
A bunyan configuration for the Clojure Timbre logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns wdullaer.bunyan-log | |
(:require [clojure.data.json :as json] | |
[clojure.string :as string] | |
[taoensso.timbre :as log])) | |
(def bunyan-levels | |
"Maps a logging level keyword into a bunyan integer level" | |
{:trace 10 | |
:debug 20 | |
:info 30 | |
:warn 40 | |
:error 50 | |
:fatal 60}) | |
(defn bunyan-log-output | |
"Function which formats a log command into a bunyan compatible json string", | |
([data] (bunyan-log-output nil data)) | |
([opts data] | |
(let [{:keys [no-stacktrace? stacktrace-fonts]} opts | |
{:keys [name pid level ?err msg_ ?ns-str ?file hostname_ timestamp_ ?line]} data | |
stack (when-not no-stacktrace? (when-let [err ?err] {:stack (log/stacktrace err opts)}))] | |
(json/write-str (merge (:context data) | |
{:v 0 | |
:name name | |
:pid pid | |
:hostname (force hostname_) | |
:time (force timestamp_) | |
:msg (force msg_) | |
:level (level bunyan-levels)} | |
(if (some? ?line) {:src {:file ?file :line ?line :namespace ?ns-str}} {}) | |
(if (some? stack) {:err stack} {})))))) | |
(def currentPID | |
"Get current process PID" | |
(memoize | |
(fn [] | |
(-> (java.lang.management.ManagementFactory/getRuntimeMXBean) | |
(.getName) | |
(string/split #"@") | |
(first))))) | |
(def iso-pattern "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") | |
(defn bunyan-appender | |
"Returns a Timbre appender which emits bunyan compatible json strings to stdout" | |
[name] | |
(assoc (taoensso.timbre.appenders.core/println-appender) | |
:timestamp-opts {:pattern iso-pattern} | |
:output-fn bunyan-log-output | |
:name (str name) | |
:pid (currentPID))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment