🎉 Add unified logging api.

This commit is contained in:
Andrey Antukh 2021-09-29 16:39:25 +02:00 committed by Alonso Torres
parent 2eccf77986
commit b64d5ef357
54 changed files with 361 additions and 413 deletions

View file

@ -10,6 +10,7 @@
[app.common.spec :as us]
[app.common.uri :as u]
[app.common.version :as v]
[app.common.logging :as log]
[app.util.avatars :as avatars]
[app.util.dom :as dom]
[app.util.globals :refer [global location]]
@ -102,10 +103,6 @@
(not (str/ends-with? (:path uri) "/"))
(update :path #(str % "/")))))
(when (= :browser @target)
(js/console.log
(str/format "Welcome to penpot! version='%s' base-uri='%s'." (:full @version) (str public-uri))))
;; --- Helper Functions
(defn ^boolean check-browser? [candidate]

View file

@ -6,9 +6,10 @@
(ns app.main
(:require
[app.common.logging :as log]
[app.common.spec :as us]
[app.common.uuid :as uuid]
[app.config :as cfg]
[app.config :as cf]
[app.main.data.events :as ev]
[app.main.data.messages :as dm]
[app.main.data.users :as du]
@ -20,7 +21,6 @@
[app.main.worker]
[app.util.dom :as dom]
[app.util.i18n :as i18n]
[app.util.logging :as log]
[app.util.router :as rt]
[app.util.storage :refer [storage]]
[app.util.theme :as theme]
@ -33,6 +33,11 @@
(log/set-level! :root :warn)
(log/set-level! :app :info)
(when (= :browser @cf/target)
(log/info :message "wecome to penpot" :version (:full @cf/version) :public-uri (str cf/public-uri)))
(declare reinit)
(s/def ::any any?)
@ -105,8 +110,8 @@
(defn ^:export init
[]
(sentry/init!)
(i18n/init! cfg/translations)
(theme/init! cfg/themes)
(i18n/init! cf/translations)
(theme/init! cf/themes)
(init-ui)
(st/emit! (initialize)))

View file

@ -8,12 +8,12 @@
(:require
["opentype.js" :as ot]
[app.common.data :as d]
[app.common.logging :as log]
[app.common.media :as cm]
[app.common.spec :as us]
[app.common.uuid :as uuid]
[app.main.fonts :as fonts]
[app.main.repo :as rp]
[app.util.logging :as log]
[app.util.webapi :as wa]
[beicon.core :as rx]
[cuerdas.core :as str]

View file

@ -8,9 +8,9 @@
(:refer-clojure :exclude [meta reset!])
(:require
["mousetrap" :as mousetrap]
[app.common.logging :as log]
[app.common.spec :as us]
[app.config :as cfg]
[app.util.logging :as log]
[app.config :as cf]
[cljs.spec.alpha :as s]
[potok.core :as ptk]))
@ -37,7 +37,7 @@
"Adds the control/command modifier to a shortcuts depending on the
operating system for the user"
[shortcut]
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
(str "command+" shortcut)
(str "ctrl+" shortcut)))
@ -55,12 +55,12 @@
[key]
;; If the key is "+" we need to surround with quotes
;; otherwise will not be very readable
(let [key (if (and (not (cfg/check-platform? :macos))
(let [key (if (and (not (cf/check-platform? :macos))
(= key "+"))
"\"+\""
key)]
(str
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-command
"Ctrl+")
key)))
@ -68,7 +68,7 @@
(defn shift
[key]
(str
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-shift
"Shift+")
key))
@ -76,7 +76,7 @@
(defn alt
[key]
(str
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-option
"Alt+")
key))
@ -91,19 +91,19 @@
(defn supr
[]
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-delete
"Supr"))
(defn esc
[]
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-esc
"Escape"))
(defn enter
[]
(if (cfg/check-platform? :macos)
(if (cf/check-platform? :macos)
mac-enter
"Enter"))

View file

@ -7,6 +7,7 @@
(ns app.main.data.workspace.changes
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.pages :as cp]
[app.common.pages.spec :as spec]
[app.common.spec :as us]
@ -14,7 +15,6 @@
[app.main.data.workspace.undo :as dwu]
[app.main.store :as st]
[app.main.worker :as uw]
[app.util.logging :as log]
[beicon.core :as rx]
[cljs.spec.alpha :as s]
[potok.core :as ptk]))

View file

@ -7,6 +7,7 @@
(ns app.main.data.workspace.common
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.geom.proportions :as gpr]
[app.common.geom.shapes :as gsh]
[app.common.pages :as cp]
@ -17,7 +18,6 @@
[app.main.data.workspace.undo :as dwu]
[app.main.streams :as ms]
[app.main.worker :as uw]
[app.util.logging :as log]
[beicon.core :as rx]
[cljs.spec.alpha :as s]
[potok.core :as ptk]))

View file

@ -7,6 +7,7 @@
(ns app.main.data.workspace.libraries
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as geom]
[app.common.pages :as cp]
@ -21,7 +22,6 @@
[app.main.repo :as rp]
[app.main.store :as st]
[app.util.i18n :refer [tr]]
[app.util.logging :as log]
[app.util.router :as rt]
[app.util.time :as dt]
[beicon.core :as rx]

View file

@ -7,13 +7,13 @@
(ns app.main.data.workspace.libraries-helpers
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as geom]
[app.common.pages :as cp]
[app.common.spec :as us]
[app.common.text :as txt]
[app.main.data.workspace.groups :as dwg]
[app.util.logging :as log]
[cljs.spec.alpha :as s]
[clojure.set :as set]))

View file

@ -9,11 +9,11 @@
(:require-macros [app.main.fonts :refer [preload-gfonts]])
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.text :as txt]
[app.config :as cf]
[app.util.dom :as dom]
[app.util.http :as http]
[app.util.logging :as log]
[app.util.object :as obj]
[beicon.core :as rx]
[clojure.set :as set]

View file

@ -10,6 +10,7 @@
["@sentry/browser" :as sentry]
[app.common.exceptions :as ex]
[app.common.uuid :as uuid]
[app.common.logging :as log]
[app.config :as cf]
[app.main.refs :as refs]))

View file

@ -18,13 +18,10 @@
[app.util.dom :as dom]
[app.util.i18n :as i18n :refer [tr]]
[app.util.keyboard :as kbd]
[app.util.logging :as log]
[beicon.core :as rx]
[cuerdas.core :as str]
[rumext.alpha :as mf]))
(log/set-level! :trace)
(defn- use-set-page-title
[team section]
(mf/use-effect

View file

@ -7,6 +7,7 @@
(ns app.main.ui.dashboard.import
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.main.data.events :as ev]
[app.main.data.modal :as modal]
[app.main.store :as st]
@ -16,7 +17,6 @@
[app.util.dom :as dom]
[app.util.i18n :as i18n :refer [tr]]
[app.util.keyboard :as kbd]
[app.util.logging :as log]
[beicon.core :as rx]
[potok.core :as ptk]
[rumext.alpha :as mf]))

View file

@ -11,13 +11,10 @@
[app.main.store :as st]
[app.util.dom :as dom]
[app.util.dom.dnd :as dnd]
[app.util.logging :as log]
[app.util.timers :as ts]
[beicon.core :as rx]
[rumext.alpha :as mf]))
(log/set-level! :warn)
(defn use-rxsub
[ob]
(let [[state reset-state!] (mf/useState @ob)]
@ -101,7 +98,6 @@
subscribe-to-drag-end
(fn []
(when (nil? (:subscr @state))
;; (js/console.log "subscribing" (:name data))
(swap! state
#(assoc % :subscr (rx/sub! global-drag-end cleanup)))))

View file

@ -7,6 +7,7 @@
(ns app.main.ui.share-link
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.config :as cf]
[app.main.data.common :as dc]
[app.main.data.messages :as dm]
@ -16,7 +17,6 @@
[app.main.ui.icons :as i]
[app.util.dom :as dom]
[app.util.i18n :as i18n :refer [tr]]
[app.util.logging :as log]
[app.util.router :as rt]
[app.util.webapi :as wapi]
[rumext.alpha :as mf]))

View file

@ -121,7 +121,6 @@
(fn []
(st/emit! (dw/setup-layout layout-name))))
(mf/use-effect
(mf/deps project-id file-id)
(fn []

View file

@ -6,6 +6,7 @@
(ns app.main.ui.workspace.shapes.text
(:require
[app.common.logging :as log]
[app.common.math :as mth]
[app.main.data.workspace.texts :as dwt]
[app.main.refs :as refs]
@ -13,7 +14,6 @@
[app.main.ui.shapes.shape :refer [shape-container]]
[app.main.ui.shapes.text :as text]
[app.util.dom :as dom]
[app.util.logging :as log]
[app.util.object :as obj]
[app.util.text-editor :as ted]
[app.util.timers :as timers]

View file

@ -1,46 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
(ns app.util.logging)
(defn- log-expr [_form level keyvals]
(let [keyvals-map (apply array-map keyvals)
;;formatter (::formatter keyvals-map 'identity)
]
`(log ~(::logger keyvals-map (str *ns*))
~level
~(-> keyvals-map
(dissoc ::logger)
#_(assoc :line (:line (meta form))))
~(:err keyvals-map))))
(defmacro set-level!
([level]
`(set-level* ~(str *ns*) ~level))
([n level]
`(set-level* ~n ~level)))
(defmacro error [& keyvals]
(log-expr &form :error keyvals))
(defmacro warn [& keyvals]
(log-expr &form :warn keyvals))
(defmacro info [& keyvals]
(log-expr &form :info keyvals))
(defmacro debug [& keyvals]
(log-expr &form :debug keyvals))
(defmacro trace [& keyvals]
(log-expr &form :trace keyvals))
(defmacro spy [form]
(let [res (gensym)]
`(let [~res ~form]
~(log-expr &form :debug [:spy `'~form
:=> res])
~res)))

View file

@ -1,189 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
;;
;; This code is highly inspired on the lambdaisland/glogi library but
;; adapted and simplified to our needs. The adapted code shares the
;; same license. You can found the origianl source code here:
;; https://github.com/lambdaisland/glogi
(ns app.util.logging
(:require
[app.common.exceptions :as ex]
[cuerdas.core :as str]
[goog.log :as glog])
(:require-macros [app.util.logging]))
(defn- logger-name
[s]
(cond
(string? s) s
(= s :root) ""
(simple-ident? s) (name s)
(qualified-ident? s) (str (namespace s) "." (name s))
:else (str s)))
(defn get-logger
[n]
(glog/getLogger (logger-name n)))
(def levels
{:off (.-OFF ^js glog/Level)
:shout (.-SHOUT ^js glog/Level)
:error (.-SEVERE ^js glog/Level)
:severe (.-SEVERE ^js glog/Level)
:warning (.-WARNING ^js glog/Level)
:warn (.-WARNING ^js glog/Level)
:info (.-INFO ^js glog/Level)
:config (.-CONFIG ^js glog/Level)
:debug (.-FINE ^js glog/Level)
:fine (.-FINE ^js glog/Level)
:finer (.-FINER ^js glog/Level)
:trace (.-FINER ^js glog/Level)
:finest (.-FINEST ^js glog/Level)
:all (.-ALL ^js glog/Level)})
(def colors
{:gray3 "#8e908c"
:gray4 "#969896"
:gray5 "#4d4d4c"
:gray6 "#282a2e"
:black "#1d1f21"
:red "#c82829"
:blue "#4271ae"
:orange "#f5871f"})
(defn- get-level-value
[level]
(if (instance? glog/Level level)
(.-value ^js level)
(.-value ^js (get levels level))))
(defn- level->color
[level]
(condp <= (get-level-value level)
(get-level-value :error) (get colors :red)
(get-level-value :warn) (get colors :orange)
(get-level-value :info) (get colors :blue)
(get-level-value :debug) (get colors :gray4)
(get-level-value :trace) (get colors :gray3)
(get colors :gray2)))
(defn- level->short-name
[l]
(case l
:fine "DBG"
:debug "DBG"
:finer "TRC"
:trace "TRC"
:info "INF"
:warn "WRN"
:warning "WRN"
:error "ERR"
(subs (.-name ^js (get levels l)) 0 3)))
(defn- make-log-record
[level message name exception]
(let [record (glog/LogRecord. level message name)]
(when exception (.setException record exception))
record))
(defn log
"Output a log message to the given logger, optionally with an exception to be
logged."
([name lvl message]
(log name lvl message nil))
([name lvl message exception]
(when glog/ENABLED
(when-let [l (get-logger name)]
(glog/publishLogRecord l (make-log-record (get levels lvl) message name exception))))))
(defn set-level*
"Set the level (a keyword) of the given logger, identified by name."
[name lvl]
(assert (contains? levels lvl))
(some-> (get-logger name)
(glog/setLevel (get levels lvl))))
(defn set-levels!
[lvls]
(doseq [[logger level] lvls
:let [level (if (string? level) (keyword level) level)]]
(set-level* logger level)))
(defn record->map
[^js record]
{:seqn (.-sequenceNumber_ record)
:time (.-time_ record)
:level (keyword (str/lower (.-name (.-level_ record))))
:message (.-msg_ record)
:logger-name (.-loggerName_ record)
:exception (.-exception_ record)})
(defn add-handler!
([handler-fn]
(add-handler! :root handler-fn))
([logger-or-name handler-fn]
(when-let [l (get-logger logger-or-name)]
(glog/removeHandler l handler-fn)
(glog/addHandler l handler-fn))))
(defn- prepare-message
[message]
(loop [kvpairs (seq message)
message (array-map)
specials []]
(if (nil? kvpairs)
[message specials]
(let [[k v] (first kvpairs)]
(cond
(= k :err)
(recur (next kvpairs)
message
(conj specials [:error nil v]))
(and (qualified-ident? k)
(= "js" (namespace k)))
(recur (next kvpairs)
message
(conj specials [:js (name k) (if (object? v) v (clj->js v))]))
:else
(recur (next kvpairs)
(assoc message k v)
specials))))))
(defn default-handler
[{:keys [message level logger-name]}]
(let [header-styles (str "font-weight: 600; color: " (level->color level))
normal-styles (str "font-weight: 300; color: " (get colors :gray6))
level-name (level->short-name level)
header (str "%c" level-name " [" logger-name "] ")]
(if (string? message)
(let [message (str header "%c" message)]
(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))]
(js/console.group message header-styles normal-styles)
(doseq [[type n v] specials]
(case type
:js (js/console.log n v)
:error (if (ex/ex-info? v)
(js/console.error (pr-str v))
(js/console.error v))))
(js/console.groupEnd message))
(let [message (str header "%c" (pr-str message))]
(js/console.log message header-styles normal-styles)))))))
(defonce default-console-handler
#(default-handler (record->map %)))
(defn initialize!
[]
(add-handler! :root default-console-handler)
nil)

View file

@ -8,6 +8,7 @@
(:refer-clojure :exclude [resolve])
(:require
[app.common.data :as d]
[app.common.logging :as log]
[app.common.file-builder :as fb]
[app.common.pages :as cp]
[app.common.text :as ct]
@ -17,7 +18,6 @@
[app.util.http :as http]
[app.util.import.parser :as cip]
[app.util.json :as json]
[app.util.logging :as log]
[app.util.zip :as uz]
[app.worker.impl :as impl]
[beicon.core :as rx]