-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.clj
95 lines (68 loc) · 2.07 KB
/
log.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(ns nl.epij.gcf.log
(:require
[cheshire.core :as json]
[cheshire.generate :refer [add-encoder encode-str]]
[clojure.string :as str]
[clojure.walk :as walk])
(:import
(com.fasterxml.jackson.core
JsonGenerationException)
(jdk.internal.net.http
HttpClientFacade
HttpRequestImpl)
(net.logstash.logback.argument
StructuredArguments)
(org.slf4j
Logger
LoggerFactory)))
(def ^Logger logger
(LoggerFactory/getLogger ^String (.toString *ns*)))
(add-encoder HttpRequestImpl encode-str)
(add-encoder HttpClientFacade encode-str)
(defn assoc-edn
[m]
(assoc m :edn (pr-str m)))
(declare log)
(defn structured-arguments
[level data]
(let [data' (-> data
(assoc-edn)
(walk/stringify-keys)
(assoc "severity" (-> level name str/upper-case)))]
(mapcat (fn [[k v]]
(cond (nil? v)
[]
(coll? v)
(try
[(StructuredArguments/raw k (json/generate-string v))]
(catch JsonGenerationException ^JsonGenerationException e
(log "WARNING" "Couldn't generate JSON map for logging" (Throwable->map e))
[(StructuredArguments/keyValue k (.toString v))]))
(string? v)
[(StructuredArguments/keyValue k v)]
(number? v)
[(StructuredArguments/keyValue k v)]
:else
[(StructuredArguments/keyValue k (.toString v))]))
data')))
(defn log
[level ^String message data]
(.error logger
message
(to-array (structured-arguments level data))))
(defn debug
[message data]
(log "DEBUG" message data))
(defn info
[message data]
(log "INFO" message data))
(defn warning
[message data]
(log "WARNING" message data))
(defn error
[message data]
(log "ERROR" message data))
(comment
(error "berichtje"
{:event-id 1337
:api-params {:a 42 :b "yo"}}))