Skip to content

Commit

Permalink
Update deps and expose new option removeEmptyKeyValuePairs (#4)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
axvr authored Aug 20, 2024
1 parent d63baa6 commit 9ce2c71
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Use Typeset.logback and configure the various options:
<!-- The following are the default values. -->
<prettyPrint>false</prettyPrint>
<removeNullKeyValuePairs>true</removeNullKeyValuePairs>
<removeEmptyKeyValuePairs>false</removeEmptyKeyValuePairs>
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</timestampFormat>
<escapeNonAsciiCharacters>false</escapeNonAsciiCharacters>
<sortKeysLexicographically>false</sortKeysLexicographically>
Expand Down
15 changes: 7 additions & 8 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{:paths ["src" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.11.2"}
ch.qos.logback/logback-classic {:mvn/version "1.5.3"}
metosin/jsonista {:mvn/version "0.3.8"}}
:deps {org.clojure/clojure {:mvn/version "1.11.4"}
ch.qos.logback/logback-classic {:mvn/version "1.5.6"}
metosin/jsonista {:mvn/version "0.3.10"}}
:deps/prep-lib {:alias :build
:fn compile
:ensure "target/classes"}
:aliases
{;; clj -X:test
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}
clj-time/clj-time {:mvn/version "0.15.2"}
com.fasterxml.jackson.datatype/jackson-datatype-jsr310 {:mvn/version "2.15.2"}
com.fasterxml.jackson.datatype/jackson-datatype-joda {:mvn/version "2.15.2"}}
com.fasterxml.jackson.datatype/jackson-datatype-jsr310 {:mvn/version "2.17.2"}
com.fasterxml.jackson.datatype/jackson-datatype-joda {:mvn/version "2.17.2"}}
:exec-fn kaocha.runner/exec-fn}
;; clj -T:build ...
:build {:extra-paths ["build"]
:deps {io.github.clojure/tools.build
{:git/tag "v0.10.0", :git/sha "3a2c484"}
:deps {io.github.clojure/tools.build {:git/tag "v0.10.5", :git/sha "2a21b7a"}
slipset/deps-deploy {:mvn/version "0.2.2"}}
:ns-default build}
;; clj -M:lint
Expand Down
81 changes: 28 additions & 53 deletions src/com/kroo/typeset/logback/JsonLayout.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.string :as str])
(:import (ch.qos.logback.classic.spi ILoggingEvent IThrowableProxy ThrowableProxy ThrowableProxyUtil)
(ch.qos.logback.core CoreConstants)
(clojure.lang Reflector)
(java.time Instant)
(java.util HashMap List Map Map$Entry)
(org.slf4j Marker)
Expand All @@ -18,6 +19,7 @@
:exposes-methods {start superStart, stop superStop}
:methods [[setPrettyPrint [Boolean] void]
[setRemoveNullKeyValuePairs [Boolean] void]
[setRemoveEmptyKeyValuePairs [Boolean] void]
[setTimestampFormat [String] void]
[setEscapeNonAsciiCharacters [Boolean] void]
[setSortKeysLexicographically [Boolean] void]
Expand All @@ -40,14 +42,11 @@
include-markers
include-exception
include-ex-data
exception-as-str
modules
object-mapper])

(defn- reify-jackson-module [module]
(clojure.lang.Reflector/invokeConstructor
(-> module symbol resolve)
(to-array [])))
(Reflector/invokeConstructor (resolve (symbol module)) (to-array [])))

(defn- reify-jackson-modules [modules]
(into []
Expand Down Expand Up @@ -76,6 +75,7 @@
[[] (volatile! (let [opts (map->JsonLayoutOpts
{:pretty false
:strip-nils true
:strip-empties false
:date-format "yyyy-MM-dd'T'HH:mm:ss'Z'"
:escape-non-ascii false
:order-by-keys false
Expand All @@ -91,8 +91,8 @@
(assoc opts :object-mapper (new-object-mapper opts))))])

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

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

(defmacro ^:private set-opt! [this opt]
`(vswap! (.state ~this) assoc ~(keyword opt) ~opt))

(defn- update-opt+mapper
"Update an option in the option map and builds a new Jackson ObjectMapper."
^JsonLayoutOpts [opts k v]
(let [opts (assoc opts k v)]
(assoc opts :object-mapper (new-object-mapper opts))))

(defn -setPrettyPrint [this pretty-print?]
(vswap! (.state this) update-opt+mapper
:pretty pretty-print?))

(defn -setRemoveNullKeyValuePairs [this remove-nil-kvs?]
(vswap! (.state this) update-opt+mapper
:strip-nils remove-nil-kvs?))

(defn -setTimestampFormat [this timestamp-format]
(vswap! (.state this) update-opt+mapper
:date-format timestamp-format))
(defmacro ^:private defopt-json [method-name key]
`(defn ~method-name [this# value#]
(vswap! (.state this#) update-opt+mapper ~key value#)))

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

(defn -setSortKeysLexicographically [this sort-keys?]
(vswap! (.state this) update-opt+mapper
:order-by-keys sort-keys?))
(defopt-json -setPrettyPrint :pretty)
(defopt-json -setRemoveNullKeyValuePairs :strip-nils)
(defopt-json -setRemoveEmptyKeyValuePairs :strip-empties)
(defopt-json -setTimestampFormat :date-format)
(defopt-json -setEscapeNonAsciiCharacters :escape-non-ascii)
(defopt-json -setSortKeysLexicographically :order-by-keys)

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

(defn -setAppendLineSeparator [this append-newline]
(set-opt! this append-newline))

(defn -setIncludeLoggerContext [this include-logger-ctx]
(set-opt! this include-logger-ctx))

(defn -setIncludeLevelValue [this include-level-val]
(set-opt! this include-level-val))

(defn -setIncludeMdc [this include-mdc]
(set-opt! this include-mdc))

(defn -setFlattenMdc [this flatten-mdc]
(set-opt! this flatten-mdc))

(defn -setIncludeMarkers [this include-markers]
(set-opt! this include-markers))

(defn -setIncludeException [this include-exception]
(set-opt! this include-exception))

(defn -setIncludeExData [this include-ex-data]
(set-opt! this include-ex-data))
(defopt -setAppendLineSeparator :append-newline)
(defopt -setIncludeLoggerContext :include-logger-ctx)
(defopt -setIncludeLevelValue :include-level-val)
(defopt -setIncludeMdc :include-mdc)
(defopt -setFlattenMdc :flatten-mdc)
(defopt -setIncludeMarkers :include-markers)
(defopt -setIncludeException :include-exception)
(defopt -setIncludeExData :include-ex-data)

0 comments on commit 9ce2c71

Please sign in to comment.