-
Notifications
You must be signed in to change notification settings - Fork 2
/
timbre.clj
75 lines (67 loc) · 2.74 KB
/
timbre.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns clj-journal.timbre
(:require [clj-journal.log :refer [jsend]]))
(def timbre->syslog-map
"Map timbre log levels to syslog levels"
{:trace 7 ;debug
:debug 7 ;debug
:info 6 ;info
:warn 4 ;warning
:error 3 ;err
:report 1 ;alert
:fatal 0 ;emerg
})
(defn ^:dynamic timbre->syslog
"Convert Timbre log level into the syslog equivalent.
Defaults to log level 3 (err) if passed an invalid log level."
[k]
(get timbre->syslog-map k 3))
(defn stacktrace
"Simple stacktrace to string. `_opts` currently do nothing."
([err]
(stacktrace err nil))
([err _opts]
(str err)))
(defn journal-output-fn
"journal (fn [data]) -> string output fn.
Use`(partial default-output-fn <opts-map>)` to modify default opts.
Modified from timbre/default-output-fn to remove timestamp, hostname, log
level information and any hash-maps passed in `vargs`, as this is already
provided by journal.
Note that this output handler does not try to retain information in case of
duplicate keys in maps passed in `vargs`."
([data] (journal-output-fn nil data))
([opts data] ; For partials
(let [{:keys [show-fields? no-stacktrace? stacktrace-fonts]} opts
{:keys [?err vargs msg_ ?ns-str ?file ?line]} data]
(str "[" (or ?ns-str ?file "?") ":" (or ?line "?") "] - "
(if show-fields?
(force msg_)
(clojure.string/join " " (filter (comp not map?) vargs)))
(when-not no-stacktrace?
(when-let [err ?err]
(str "\n" (stacktrace err opts))))))))
(defn journal-appender
"Journal appender for timbre, using `journal-output-fn`. Optionally takes a
single fn that should return a map that will be merged into the structured
fields sent to journal.
Any maps passed to a timbre logger will be sent to journal as structured data.
Any complex data structures in values will be serialized as EDN."
([]
(journal-appender (fn [{:keys [?file ?line ?ns-str] :as data}]
{"CODE_FILE" ?file
"CODE_LINE" ?line
"CODE_NS" ?ns-str})))
([default-fields-fn]
{:enabled? true
:async? false
:min-level :trace
:rate-limit nil
:output-fn journal-output-fn
:fn
(fn [{:keys [instant level output_ vargs]
:as data}]
(let [default-fields (default-fields-fn data)
log-maps (filter map? vargs)
merged-map (merge (apply merge log-maps) default-fields)]
(apply jsend (concat [(timbre->syslog level) (force output_)]
(reduce concat merged-map)))))}))