0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 16:00:19 -05:00

Improve logging ordering of message parts

This commit is contained in:
Andrey Antukh 2022-04-04 09:26:05 +02:00 committed by Andrés Moya
parent ca02999ae9
commit df4b92fb6b
2 changed files with 61 additions and 48 deletions

View file

@ -6,13 +6,13 @@
(ns app.common.logging
(:require
[app.common.data :as d]
[app.common.exceptions :as ex]
[app.common.uuid :as uuid]
[app.common.spec :as us]
[cuerdas.core :as str]
[clojure.spec.alpha :as s]
[fipp.edn :as fpp]
#?(:clj [io.aviso.exception :as ie])
#?(:cljs [goog.log :as glog]))
#?(:cljs (:require-macros [app.common.logging])
:clj (:import
@ -21,7 +21,6 @@
org.apache.logging.log4j.Logger
org.apache.logging.log4j.ThreadContext
org.apache.logging.log4j.CloseableThreadContext
org.apache.logging.log4j.message.MapMessage
org.apache.logging.log4j.spi.LoggerContext)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -30,11 +29,22 @@
#?(:clj (set! *warn-on-reflection* true))
#?(:clj
(defn build-map-message
[m]
(let [message (MapMessage. (count m))]
(reduce-kv #(.with ^MapMessage %1 (name %2) %3) message m))))
(def ^:private reserved-props
#{:level :cause ::logger ::async ::raw ::context})
(def ^:private props-xform
(comp (partition-all 2)
(remove (fn [[k]] (contains? reserved-props k)))
(map vec)))
(defn build-message
[props]
(loop [pairs (sequence props-xform props)
result []]
(if-let [[k v] (first pairs)]
(recur (rest pairs)
(conj result (str/concat (d/name k) "=" (pr-str v))))
result)))
#?(:clj
(def logger-context
@ -44,13 +54,6 @@
(def logging-agent
(agent nil :error-mode :continue)))
(defn- simple-prune
([s] (simple-prune s (* 1024 1024)))
([s max-length]
(if (> (count s) max-length)
(str (subs s 0 max-length) " [...]")
s)))
#?(:clj
(defn stringify-data
[val]
@ -131,22 +134,25 @@
(defn write-log!
[logger level exception message]
#?(:clj
(if exception
(.log ^Logger logger
^Level level
^Object message
^Throwable exception)
(.log ^Logger logger
^Level level
^Object message))
:cljs
(when glog/ENABLED
(when-let [l (get-logger logger)]
(let [level (get-level level)
record (glog/LogRecord. level message (.getName ^js l))]
(when exception (.setException record exception))
(glog/publishLogRecord l record))))))
(let [message (if (string? message)
message
(str/join ", " message))]
#?(:clj
(if exception
(.log ^Logger logger
^Level level
^Object message
^Throwable exception)
(.log ^Logger logger
^Level level
^Object message))
:cljs
(when glog/ENABLED
(when-let [l (get-logger logger)]
(let [level (get-level level)
record (glog/LogRecord. level message (.getName ^js l))]
(when exception (.setException record exception))
(glog/publishLogRecord l record)))))))
#?(:clj
(defn enabled?
@ -166,13 +172,13 @@
{:spec-explain (us/pretty-explain data)})))))
(defmacro log
[& {:keys [level cause ::logger ::async ::raw ::context] :or {async true} :as props}]
[& props]
(if (:ns &env) ; CLJS
`(write-log! ~(or logger (str *ns*))
~level
~cause
(or ~raw ~(dissoc props :level :cause ::logger ::raw ::context)))
(let [props (dissoc props :level :cause ::logger ::async ::raw ::context)
(let [{:keys [level cause ::logger ::raw]} props
message (or raw (build-message props))]
`(write-log! ~(or logger (str *ns*)) ~level ~cause (or ~raw (build-message ~(vec props)))))
(let [{:keys [level cause ::logger ::async ::raw ::context] :or {async true}} props
logger (or logger (str *ns*))
logger-sym (gensym "log")
level-sym (gensym "log")]
@ -183,17 +189,17 @@
`(do
(send-off logging-agent
(fn [_#]
(try
(let [message# (or ~raw (build-message ~(vec props)))]
(with-context (-> {:id (uuid/next)}
(into ~context)
(into (get-error-context ~cause)))
(->> (or ~raw (build-map-message ~props))
(write-log! ~logger-sym ~level-sym ~cause)))
(catch Throwable cause#
(write-log! ~logger-sym (get-level :error) cause#
"unexpected error on writting log")))))
(try
(write-log! ~logger-sym ~level-sym ~cause message#)
(catch Throwable cause#
(write-log! ~logger-sym (get-level :error) cause#
"unexpected error on writting log")))))))
nil)
`(let [message# (or ~raw (build-map-message ~props))]
`(let [message# (or ~raw (build-message ~(vec props)))]
(write-log! ~logger-sym ~level-sym ~cause message#)
nil)))))))
@ -283,8 +289,8 @@
#?(:cljs
(defn- prepare-message
[message]
(loop [kvpairs (seq message)
message (array-map)
(loop [kvpairs (seq message)
message []
specials []]
(if (nil? kvpairs)
[message specials]
@ -303,7 +309,7 @@
:else
(recur (next kvpairs)
(assoc message k v)
(conj message (str/concat (d/name k) "=" (pr-str v)))
specials)))))))
#?(:cljs
@ -319,7 +325,7 @@
(js/console.log message header-styles normal-styles))
(let [[message specials] (prepare-message message)]
(if (seq specials)
(let [message (str header "%c" (pr-str message))]
(let [message (str header "%c" message)]
(js/console.group message header-styles normal-styles)
(doseq [[type n v] specials]
(case type
@ -328,7 +334,7 @@
(js/console.error (pr-str v))
(js/console.error v))))
(js/console.groupEnd message))
(let [message (str header "%c" (pr-str message))]
(let [message (str header "%c" message)]
(js/console.log message header-styles normal-styles)))))
(when exception

View file

@ -7,6 +7,7 @@
(ns debug
(:require
[app.common.data :as d]
[app.common.logging :as l]
[app.common.pages.helpers :as cph]
[app.common.transit :as t]
[app.common.uuid :as uuid]
@ -25,6 +26,12 @@
[potok.core :as ptk]
[promesa.core :as p]))
(defn ^:export set-logging
([level]
(l/set-level! :app (keyword level)))
([ns level]
(l/set-level! (keyword ns) (keyword level))))
(def debug-options
#{;; Displays the bounding box for the shapes
:bounding-boxes