/
config.clj
257 lines (223 loc) · 7.78 KB
/
config.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
(ns dialog.config
"Functions for loading and managing the dialog configuration."
(:require
[aero.core :as aero]
[clojure.java.io :as io]
[clojure.string :as str]
[dialog.format.json :as fmt.json]
[dialog.format.pretty :as fmt.pretty]
[dialog.format.simple :as fmt.simple]
[dialog.output.file :as out.file]
[dialog.output.print :as out.print]
[dialog.output.syslog :as out.syslog])
(:import
dialog.logger.Level))
(defn- print-err
"Print a message to stderr when something goes wrong during initialization."
[message & args]
(binding [*out* *err*]
(apply printf (str "[dialog config error] " message \newline) args)
(flush))
nil)
(defn- some-setting
"Check the named environment variable and JVM property for a runtime setting,
or return the default if neither is set."
[env-var jvm-prop default]
(or (System/getenv env-var)
(System/getProperty jvm-prop)
default))
(defn- collect-prop-levels
"Look in the JVM system properties for logger level configs. Returns a map of
collected level settings."
([]
(collect-prop-levels (System/getProperties)))
([properties]
(let [prefix "dialog.level."]
(into {}
(comp
(filter #(str/starts-with? (key %) prefix))
(remove #(= prefix (key %)))
(keep (fn [[prop level-str]]
(let [level (keyword (str/lower-case level-str))]
(if (Level/isValid level)
[(subs prop (count prefix)) level]
(print-err "JVM property %s specifies invalid level %s"
prop
level-str))))))
properties))))
(defn- collect-env-levels
"Look in the process environment for logger level configs. Returns a map of
collected level settings."
([]
(collect-env-levels (System/getenv)))
([env]
(let [prefix "DIALOG_LEVEL_"]
(into {}
(comp
(filter #(str/starts-with? (key %) prefix))
(remove #(= prefix (key %)))
(keep (fn [[var-name level-str]]
(let [level (keyword (str/lower-case level-str))]
(if (Level/isValid level)
[(-> var-name
(subs (count prefix))
(str/lower-case)
(str/replace "_" "."))
level]
(print-err "Environment variable %s specifies invalid level %s"
var-name
level-str))))))
env))))
(defn- resolve-fn
"Safely resolve the given value to a function. Returns the function, or prints
an error and returns nil."
[kind x]
(try
(cond
(or (nil? x)
(var? x)
(fn? x))
x
(symbol? x)
(or (requiring-resolve x)
(print-err "%s function %s was not found"
kind x))
:else
(print-err "%s function %s is not a known type: %s"
kind x (class x)))
(catch Exception ex
(print-err "%s function %s could not be resolved: %s"
kind x (ex-message ex)))))
(defn- resolve-middleware
"Resolve any middleware symbols in the config to functions."
[config]
(if-let [middleware (seq (:middleware config))]
(assoc config
:middleware
(into []
(keep (partial resolve-fn "middleware"))
middleware))
config))
(defn- resolve-init
"Resolve the initialization function, if set."
[config]
(if-let [init-sym (:init config)]
(if-let [init-fn (resolve-fn "init" init-sym)]
(assoc config :init init-fn)
(dissoc config :init))
config))
(defn- apply-init
"Apply the initialization function to the config if present, producing an
updated config. Returning `nil` is interpreted as no changes."
[config]
(if-let [init-fn (:init config)]
(try
(or (init-fn config)
config)
(catch Exception ex
(print-err "error applying init function %s: %s"
(.getName (class init-fn))
(ex-message ex))
config))
config))
(defn- output-formatter
"Construct an output formatting function."
[output]
(case (:format output)
:message :message
:simple (fmt.simple/formatter output)
:pretty (fmt.pretty/formatter output)
:json (fmt.json/formatter output)))
(defn- output-writer
"Construct an output writing function."
[output]
(case (:type output)
:null nil
:file (out.file/writer output)
:print (out.print/writer output)
:syslog (out.syslog/writer output)))
(defn- initialize-output
"Expand an output configuration map into its full form and apply
initialization."
[[id output]]
;; Expand keyword into basic map with default format.
(let [output (if (keyword? output)
{:type output
:format :simple}
output)]
(cond
;; Gracefully handle nil to mean omission.
(nil? output)
nil
;; Check that config has expected type.
(not (map? output))
(print-err "output %s has unknown configuration; expected a map: %s"
id
(pr-str output))
;; Check that output type is understood.
(not (contains? #{:null :file :print :syslog}
(:type output)))
(print-err "output %s has invalid type: %s"
id
(:type output))
;; Check that format type is understood.
(and (contains? output :format)
(not (contains? #{:message :simple :pretty :json}
(:format output))))
(print-err "output %s has invalid format: %s"
id
(:format output))
;; Initialize format and output functions.
:else
(try
(let [output (merge {:format :simple} output)]
[id (assoc output
:formatter (output-formatter output)
:writer (output-writer output))])
(catch Exception ex
(print-err "output %s could not be initialized: %s %s"
id
(.getName (class ex))
(ex-message ex)))))))
(defn- initialize-outputs
"Process a map of outputs and return an updated version with each output
value fully expanded and initialized."
[outputs]
(into {}
(keep initialize-output)
outputs))
(defn- read-config
"Read the raw configuration from an EDN resource. Returns nil if the resource
is not found or can't be read."
[profile]
(when-let [config-edn (io/resource "dialog/config.edn")]
(try
(aero/read-config config-edn {:profile profile})
(catch Exception ex
(print-err "failed to read dialog config file: %s"
(ex-message ex))))))
(defn load-config
"Read logging configuration from an EDN resource (if available) and set any
runtime overrides from JVM properties an the process environment."
[]
(let [profile (keyword (some-setting "DIALOG_PROFILE"
"dialog.profile"
:default))
root-level (some-setting "DIALOG_LEVEL"
"dialog.level"
nil)
base-config (merge {:level :info
:outputs {:console :print}}
(read-config profile))]
(->
base-config
(update :levels merge
(collect-prop-levels)
(collect-env-levels))
(cond->
(Level/isValid root-level)
(assoc :level (keyword root-level)))
(resolve-init)
(apply-init)
(resolve-middleware)
(update :outputs initialize-outputs))))