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

View file

@ -7,6 +7,7 @@
(ns debug (ns debug
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.logging :as l]
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.transit :as t] [app.common.transit :as t]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
@ -25,6 +26,12 @@
[potok.core :as ptk] [potok.core :as ptk]
[promesa.core :as p])) [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 (def debug-options
#{;; Displays the bounding box for the shapes #{;; Displays the bounding box for the shapes
:bounding-boxes :bounding-boxes