Skip to content

Commit 9ce2c71

Browse files
authored
Update deps and expose new option removeEmptyKeyValuePairs (#4)
Updated dependencies, refactored code and exposed a new config option available with the latest [metosin/jsonista](https://github.com/metosin/jsonista) lib: [`:strip-empties`](https://github.com/metosin/jsonista/releases/tag/0.3.10) -> `removeEmptyKeyValuePairs`.
1 parent d63baa6 commit 9ce2c71

File tree

3 files changed

+36
-61
lines changed

3 files changed

+36
-61
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Use Typeset.logback and configure the various options:
5555
<!-- The following are the default values. -->
5656
<prettyPrint>false</prettyPrint>
5757
<removeNullKeyValuePairs>true</removeNullKeyValuePairs>
58+
<removeEmptyKeyValuePairs>false</removeEmptyKeyValuePairs>
5859
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</timestampFormat>
5960
<escapeNonAsciiCharacters>false</escapeNonAsciiCharacters>
6061
<sortKeysLexicographically>false</sortKeysLexicographically>

deps.edn

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
{:paths ["src" "target/classes"]
2-
:deps {org.clojure/clojure {:mvn/version "1.11.2"}
3-
ch.qos.logback/logback-classic {:mvn/version "1.5.3"}
4-
metosin/jsonista {:mvn/version "0.3.8"}}
2+
:deps {org.clojure/clojure {:mvn/version "1.11.4"}
3+
ch.qos.logback/logback-classic {:mvn/version "1.5.6"}
4+
metosin/jsonista {:mvn/version "0.3.10"}}
55
:deps/prep-lib {:alias :build
66
:fn compile
77
:ensure "target/classes"}
88
:aliases
99
{;; clj -X:test
1010
:test {:extra-paths ["test"]
11-
:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
11+
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}
1212
clj-time/clj-time {:mvn/version "0.15.2"}
13-
com.fasterxml.jackson.datatype/jackson-datatype-jsr310 {:mvn/version "2.15.2"}
14-
com.fasterxml.jackson.datatype/jackson-datatype-joda {:mvn/version "2.15.2"}}
13+
com.fasterxml.jackson.datatype/jackson-datatype-jsr310 {:mvn/version "2.17.2"}
14+
com.fasterxml.jackson.datatype/jackson-datatype-joda {:mvn/version "2.17.2"}}
1515
:exec-fn kaocha.runner/exec-fn}
1616
;; clj -T:build ...
1717
:build {:extra-paths ["build"]
18-
:deps {io.github.clojure/tools.build
19-
{:git/tag "v0.10.0", :git/sha "3a2c484"}
18+
:deps {io.github.clojure/tools.build {:git/tag "v0.10.5", :git/sha "2a21b7a"}
2019
slipset/deps-deploy {:mvn/version "0.2.2"}}
2120
:ns-default build}
2221
;; clj -M:lint

src/com/kroo/typeset/logback/JsonLayout.clj

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[clojure.string :as str])
66
(:import (ch.qos.logback.classic.spi ILoggingEvent IThrowableProxy ThrowableProxy ThrowableProxyUtil)
77
(ch.qos.logback.core CoreConstants)
8+
(clojure.lang Reflector)
89
(java.time Instant)
910
(java.util HashMap List Map Map$Entry)
1011
(org.slf4j Marker)
@@ -18,6 +19,7 @@
1819
:exposes-methods {start superStart, stop superStop}
1920
:methods [[setPrettyPrint [Boolean] void]
2021
[setRemoveNullKeyValuePairs [Boolean] void]
22+
[setRemoveEmptyKeyValuePairs [Boolean] void]
2123
[setTimestampFormat [String] void]
2224
[setEscapeNonAsciiCharacters [Boolean] void]
2325
[setSortKeysLexicographically [Boolean] void]
@@ -40,14 +42,11 @@
4042
include-markers
4143
include-exception
4244
include-ex-data
43-
exception-as-str
4445
modules
4546
object-mapper])
4647

4748
(defn- reify-jackson-module [module]
48-
(clojure.lang.Reflector/invokeConstructor
49-
(-> module symbol resolve)
50-
(to-array [])))
49+
(Reflector/invokeConstructor (resolve (symbol module)) (to-array [])))
5150

5251
(defn- reify-jackson-modules [modules]
5352
(into []
@@ -76,6 +75,7 @@
7675
[[] (volatile! (let [opts (map->JsonLayoutOpts
7776
{:pretty false
7877
:strip-nils true
78+
:strip-empties false
7979
:date-format "yyyy-MM-dd'T'HH:mm:ss'Z'"
8080
:escape-non-ascii false
8181
:order-by-keys false
@@ -91,8 +91,8 @@
9191
(assoc opts :object-mapper (new-object-mapper opts))))])
9292

9393
(defn- insert-kvp!
94-
"Inserts a key value pair into a Java map. If a key with the same name
95-
already exists, prepends \"@\"-symbols onto the key until it is unique."
94+
"Inserts an SLF4J `KeyValuePair` into a Java map. If a key with the same
95+
name already exists, prepends \"@\"-symbols onto the key until it is unique."
9696
^Map [^Map m ^KeyValuePair kv]
9797
(loop [^String k (.key kv)
9898
^Object v (.value kv)]
@@ -129,8 +129,7 @@
129129
(if t
130130
(recur (conj! acc (ex-data t)) (.getCause t))
131131
(persistent! acc)))]
132-
(if (every? nil? ex-datas)
133-
nil
132+
(when-not (every? nil? ex-datas)
134133
ex-datas))
135134
(ex-data t)))
136135

@@ -154,7 +153,7 @@
154153
"error.kind" (.getClass e)
155154
"error.stack" (ThrowableProxyUtil/asString (ThrowableProxy. e))})
156155
CoreConstants/LINE_SEPARATOR)
157-
;; Another failover for when something is very seriously wrong!
156+
;; A second failover for when something is *very* seriously wrong!
158157
(catch Throwable _
159158
(format "{\"timestamp\":\"%s\"\"message\":%s,\"exception\":%s,\"level\":\"ERROR\",\"logger.name\":\"%s\"}\n"
160159
(Instant/now)
@@ -186,7 +185,6 @@
186185
(when (:include-markers opts)
187186
(when-let [^List markers (.getMarkerList event)]
188187
(when-not (.isEmpty markers)
189-
;; TODO: markers vs. tags?
190188
(.put m "markers" (mapv #(.getName ^Marker %) markers)))))
191189
(when-let [^IThrowableProxy tp (and (:include-exception opts)
192190
(.getThrowableProxy event))]
@@ -220,59 +218,36 @@
220218
;;; -------------------------------------
221219
;;; Expose Logback configuration options.
222220

223-
(defmacro ^:private set-opt! [this opt]
224-
`(vswap! (.state ~this) assoc ~(keyword opt) ~opt))
225-
226221
(defn- update-opt+mapper
227222
"Update an option in the option map and builds a new Jackson ObjectMapper."
228223
^JsonLayoutOpts [opts k v]
229224
(let [opts (assoc opts k v)]
230225
(assoc opts :object-mapper (new-object-mapper opts))))
231226

232-
(defn -setPrettyPrint [this pretty-print?]
233-
(vswap! (.state this) update-opt+mapper
234-
:pretty pretty-print?))
235-
236-
(defn -setRemoveNullKeyValuePairs [this remove-nil-kvs?]
237-
(vswap! (.state this) update-opt+mapper
238-
:strip-nils remove-nil-kvs?))
239-
240-
(defn -setTimestampFormat [this timestamp-format]
241-
(vswap! (.state this) update-opt+mapper
242-
:date-format timestamp-format))
227+
(defmacro ^:private defopt-json [method-name key]
228+
`(defn ~method-name [this# value#]
229+
(vswap! (.state this#) update-opt+mapper ~key value#)))
243230

244-
(defn -setEscapeNonAsciiCharacters [this escape-non-ascii?]
245-
(vswap! (.state this) update-opt+mapper
246-
:escape-non-ascii escape-non-ascii?))
231+
(defmacro ^:private defopt [method-name key]
232+
`(defn ~method-name [this# value#]
233+
(vswap! (.state this#) assoc ~key value#)))
247234

248-
(defn -setSortKeysLexicographically [this sort-keys?]
249-
(vswap! (.state this) update-opt+mapper
250-
:order-by-keys sort-keys?))
235+
(defopt-json -setPrettyPrint :pretty)
236+
(defopt-json -setRemoveNullKeyValuePairs :strip-nils)
237+
(defopt-json -setRemoveEmptyKeyValuePairs :strip-empties)
238+
(defopt-json -setTimestampFormat :date-format)
239+
(defopt-json -setEscapeNonAsciiCharacters :escape-non-ascii)
240+
(defopt-json -setSortKeysLexicographically :order-by-keys)
251241

252242
(defn -setJacksonModules [this modules]
253243
(vswap! (.state this) update-opt+mapper
254244
:modules (reify-jackson-modules modules)))
255245

256-
(defn -setAppendLineSeparator [this append-newline]
257-
(set-opt! this append-newline))
258-
259-
(defn -setIncludeLoggerContext [this include-logger-ctx]
260-
(set-opt! this include-logger-ctx))
261-
262-
(defn -setIncludeLevelValue [this include-level-val]
263-
(set-opt! this include-level-val))
264-
265-
(defn -setIncludeMdc [this include-mdc]
266-
(set-opt! this include-mdc))
267-
268-
(defn -setFlattenMdc [this flatten-mdc]
269-
(set-opt! this flatten-mdc))
270-
271-
(defn -setIncludeMarkers [this include-markers]
272-
(set-opt! this include-markers))
273-
274-
(defn -setIncludeException [this include-exception]
275-
(set-opt! this include-exception))
276-
277-
(defn -setIncludeExData [this include-ex-data]
278-
(set-opt! this include-ex-data))
246+
(defopt -setAppendLineSeparator :append-newline)
247+
(defopt -setIncludeLoggerContext :include-logger-ctx)
248+
(defopt -setIncludeLevelValue :include-level-val)
249+
(defopt -setIncludeMdc :include-mdc)
250+
(defopt -setFlattenMdc :flatten-mdc)
251+
(defopt -setIncludeMarkers :include-markers)
252+
(defopt -setIncludeException :include-exception)
253+
(defopt -setIncludeExData :include-ex-data)

0 commit comments

Comments
 (0)