From 44f0a4aeb3983ec6e6199d43bfc190bde260633b Mon Sep 17 00:00:00 2001 From: Ngoc Khuat Date: Wed, 2 Nov 2022 16:32:55 +0700 Subject: [PATCH] consolidate logging --- deps.edn | 44 +++++++++++--------- resources/log4j2.xml | 32 ++++++++++++++ src/frontend/components/Page/View.tsx | 18 ++++---- src/ubinote/cmd.clj | 2 +- src/ubinote/migration.clj | 2 +- src/ubinote/server/core.clj | 12 +++++- src/ubinote/server/db.clj | 2 - src/ubinote/server/middleware/api.clj | 2 +- src/ubinote/server/middleware/exceptions.clj | 2 +- src/ubinote/server/middleware/log.clj | 2 +- src/ubinote/server/middleware/request.clj | 39 ----------------- 11 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 resources/log4j2.xml delete mode 100644 src/ubinote/server/middleware/request.clj diff --git a/deps.edn b/deps.edn index 5f17483..cf5d04f 100644 --- a/deps.edn +++ b/deps.edn @@ -2,29 +2,35 @@ :deps { - org.clojure/clojure {:mvn/version "1.11.0"} + org.clojure/clojure {:mvn/version "1.11.0"} ;; util - prismatic/schema {:mvn/version "1.2.0"} ; schema validator - slingshot/slingshot {:mvn/version "0.12.2"} ; better exception handling - com.cemerick/friend {:mvn/version "0.2.3"} ; auth library - cheshire/cheshire {:mvn/version "5.10.2"} ; fast JSON encoding (used by Ring JSON middleware) - potemkin/potemkin {:mvn/version "0.4.5"} ; utility macros & fns + prismatic/schema {:mvn/version "1.2.0"} ; schema validator + slingshot/slingshot {:mvn/version "0.12.2"} ; better exception handling + com.cemerick/friend {:mvn/version "0.2.3"} ; auth library + cheshire/cheshire {:mvn/version "5.10.2"} ; fast JSON encoding (used by Ring JSON middleware) + potemkin/potemkin {:mvn/version "0.4.5"} ; utility macros & fns ;; webserver+routing - compojure/compojure {:mvn/version "1.6.2"} - ring/ring-core {:mvn/version "1.9.4"} - ring/ring-jetty-adapter {:mvn/version "1.9.5"} - ring/ring-json {:mvn/version "0.5.1"} - ring-cors/ring-cors {:mvn/version "0.1.13"} + compojure/compojure {:mvn/version "1.6.2"} + ring/ring-core {:mvn/version "1.9.4"} + ring/ring-jetty-adapter {:mvn/version "1.9.5"} + ring/ring-json {:mvn/version "0.5.1"} + ring-cors/ring-cors {:mvn/version "0.1.13"} ;; log - com.taoensso/timbre {:mvn/version "5.1.2"} + org.clojure/tools.logging {:mvn/version "1.2.4"} + org.apache.logging.log4j/log4j-1.2-api {:mvn/version "2.18.0"} ; apache logging framework + org.apache.logging.log4j/log4j-api {:mvn/version "2.18.0"} ; add compatibility with log4j 1.2 + org.apache.logging.log4j/log4j-core {:mvn/version "2.18.0"} ; apache logging framework + org.apache.logging.log4j/log4j-jcl {:mvn/version "2.18.0"} ; allows the commons-logging API to work with log4j 2 + org.apache.logging.log4j/log4j-jul {:mvn/version "2.18.0"} ; java.util.logging (JUL) -> Log4j2 adapter + org.apache.logging.log4j/log4j-slf4j-impl {:mvn/version "2.18.0"} ; allows the slf4j API to work with log4j 2 ;; database - org.clojure/java.jdbc {:mvn/version "0.7.12"} - com.h2database/h2 {:mvn/version "2.1.210"} ; H2 driver - org.postgresql/postgresql {:mvn/version "42.2.13"} ; Postgres driver - com.mchange/c3p0 {:mvn/version "0.9.5.5"} - toucan/toucan {:mvn/version "1.18.0"} - ;; hickory/hickory {:mvn/version "0.7.1"} ; seems like a fater parser than enlive, but using enlive for now bc it has DOM selector - enlive/enlive {:mvn/version "1.1.6"}} ; html parser + org.clojure/java.jdbc {:mvn/version "0.7.12"} + com.h2database/h2 {:mvn/version "2.1.210"} ; H2 driver + org.postgresql/postgresql {:mvn/version "42.2.13"} ; Postgres driver + com.mchange/c3p0 {:mvn/version "0.9.5.5"} + toucan/toucan {:mvn/version "1.18.0"} + ;; hickory/hickory {:mvn/version "0.7.1"} ; seems like a fater parser than enlive, but using enlive for now bc it has DOM selector + enlive/enlive {:mvn/version "1.1.6"}} ; html parser :aliases {; clj -M:dev diff --git a/resources/log4j2.xml b/resources/log4j2.xml new file mode 100644 index 0000000..803f647 --- /dev/null +++ b/resources/log4j2.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/frontend/components/Page/View.tsx b/src/frontend/components/Page/View.tsx index 0b0e52d..79459e7 100644 --- a/src/frontend/components/Page/View.tsx +++ b/src/frontend/components/Page/View.tsx @@ -24,14 +24,17 @@ const addHighlight = (pageId: number, selection: window.Selection, color = "red" // need to calculate textpos before highlight, otherwise the position will be messed up // when try to highlight on re-load const textPos = fromRange(document.body, range); - const highlightElements = highlightRange(range, 'span', {class: colorToCSS[color]}); - console.log("Elements: ", highlightElements); + console.log("range:", range); + console.log("textpos:", textPos); + console.log("bounding: ", range.getBoundingClientRect()); + // from this bounding => display a dom at the middle end + //const highlightElements = highlightRange(range, 'span', {class: colorToCSS[color]}); // save it - createAnnotation( - {coordinate: textPos, - page_id: pageId}) - .catch(err => console.error("Failed to add annotaiton: ", err)); - return highlightElements; + //createAnnotation( + // {coordinate: textPos, + // page_id: pageId}) + // .catch(err => console.error("Failed to add annotaiton: ", err)); + //return highlightElements; } const PageView = (props: Props) => { @@ -43,6 +46,7 @@ const PageView = (props: Props) => { document.addEventListener("mouseup", () => { const selection = window.getSelection(); if (!selection.isCollapsed) { + // open up a popover addHighlight(page.id, selection); } }); diff --git a/src/ubinote/cmd.clj b/src/ubinote/cmd.clj index ccbf4ac..0265896 100644 --- a/src/ubinote/cmd.clj +++ b/src/ubinote/cmd.clj @@ -2,7 +2,7 @@ (:require [ubinote.util.fs :as fs] [clojure.string :as string] [clojure.java.shell :refer [sh]] - [taoensso.timbre :as log])) + [clojure.tools.logging :as log])) (defn which "like `which` command" diff --git a/src/ubinote/migration.clj b/src/ubinote/migration.clj index 2bb5498..e129382 100644 --- a/src/ubinote/migration.clj +++ b/src/ubinote/migration.clj @@ -4,7 +4,7 @@ [ubinote.config :as cfg] [clojure.java.jdbc :as jdbc] [clojure.string :as string] - [taoensso.timbre :as log] + [clojure.tools.logging :as log] [toucan.db :as db])) (def migrations (atom [])) diff --git a/src/ubinote/server/core.clj b/src/ubinote/server/core.clj index 6ebc86c..f87461a 100644 --- a/src/ubinote/server/core.clj +++ b/src/ubinote/server/core.clj @@ -8,9 +8,19 @@ [compojure.route :as route] [compojure.core :refer [context defroutes GET]] [ring.util.response :refer [resource-response]] - [taoensso.timbre :as log] + [clojure.tools.logging :as log] [ring.adapter.jetty :refer [run-jetty]])) +;; ensure we use a `BasicContextSelector` instead of a `ClassLoaderContextSelector` for log4j2. Ensures there is only +;; one LoggerContext instead of one per classpath root. Practical effect is that now `(LogManager/getContext true)` +;; and `(LogManager/getContext false)` will return the same (and only) +;; LoggerContext. https://logging.apache.org/log4j/2.x/manual/logsep.html +(System/setProperty "log4j2.contextSelector" "org.apache.logging.log4j.core.selector.BasicContextSelector") + +;; ensure the [[clojure.tools.logging]] logger factory is the log4j2 version (slf4j is far slower and identified first) +(System/setProperty "clojure.tools.logging.factory" "clojure.tools.logging.impl/log4j2-factory") +(System/setProperty "log4j2.configurationFile" "log4j2.xml") + (defroutes routes ;; serving bundle.js this seems hacky? (GET "/static/js/bundle.js" [_req] (resource-response "frontend/static/js/bundle.js")) ;; inside the resources folder diff --git a/src/ubinote/server/db.clj b/src/ubinote/server/db.clj index dd81313..a68ffd6 100644 --- a/src/ubinote/server/db.clj +++ b/src/ubinote/server/db.clj @@ -3,7 +3,6 @@ [clojure.java.jdbc :as jdbc] [clojure.java.io :as io] [clojure.string :as string] - [taoensso.timbre :as log] [toucan.models :as models] [toucan.db :as db]) (:import java.util.Properties @@ -91,6 +90,5 @@ [] (let [db-type (cfg/config-kw :un-db-type)] (models/set-root-namespace! 'ubinote.model) - ;(db/set-default-automatically-convert-dashes-and-underscores! true) (db/set-default-quoting-style! (db-type quoting-style)) (db/set-default-db-connection! (db-details db-type)))) diff --git a/src/ubinote/server/middleware/api.clj b/src/ubinote/server/middleware/api.clj index d63815b..5599e40 100644 --- a/src/ubinote/server/middleware/api.clj +++ b/src/ubinote/server/middleware/api.clj @@ -1,5 +1,5 @@ (ns ubinote.server.middleware.api - (:require [taoensso.timbre :as log])) + (:require [clojure.tools.logging :as log])) (defn resp->log-level [{:keys [status]}] diff --git a/src/ubinote/server/middleware/exceptions.clj b/src/ubinote/server/middleware/exceptions.clj index f228ee5..a3d7ff2 100644 --- a/src/ubinote/server/middleware/exceptions.clj +++ b/src/ubinote/server/middleware/exceptions.clj @@ -1,5 +1,5 @@ (ns ubinote.server.middleware.exceptions - (:require [taoensso.timbre :as log])) + (:require [clojure.tools.logging :as log])) (defn- explain-one-schema-error [[k v]] diff --git a/src/ubinote/server/middleware/log.clj b/src/ubinote/server/middleware/log.clj index def88b8..97d78bc 100644 --- a/src/ubinote/server/middleware/log.clj +++ b/src/ubinote/server/middleware/log.clj @@ -1,5 +1,5 @@ (ns ubinote.server.middleware.log - (:require [taoensso.timbre :as log])) + (:require [clojure.tools.logging :as log])) (defn resp->log-level [{:keys [status]}] diff --git a/src/ubinote/server/middleware/request.clj b/src/ubinote/server/middleware/request.clj deleted file mode 100644 index f9f807b..0000000 --- a/src/ubinote/server/middleware/request.clj +++ /dev/null @@ -1,39 +0,0 @@ -(ns ubinote.server.middleware.log - (:require [taoensso.timbre :as log])) - -(defn resp->log-level - [{:keys [status]}] - (cond - (< 400 status) :info - :else :info)) - -(defn format-nanoseconds - "Format a time interval in nanoseconds to something more readable. (µs/ms/etc.)" - ^String [nanoseconds] - ;; The basic idea is to take `n` and see if it's greater than the divisior. If it is, we'll print it out as that - ;; unit. If more, we'll divide by the divisor and recur, trying each successively larger unit in turn. e.g. - ;; - ;; (format-nanoseconds 500) ; -> "500 ns" - ;; (format-nanoseconds 500000) ; -> "500 µs" - (loop [n nanoseconds, [[unit divisor] & more] [[:ns 1000] [:µs 1000] [:ms 1000] [:s 60] [:mins 60] [:hours 24] - [:days 7] [:weeks (/ 365.25 7)] [:years Double/POSITIVE_INFINITY]]] - (if (and (> n divisor) - (seq more)) - (recur (/ n divisor) more) - (format "%.1f %s" (double n) (name unit))))) - -(defn wrap-request-logger - "Log a message that looks like `:get /api/login 401 263.8 µs` for each request." - [handler] - (fn [req] - (let [start-time (System/nanoTime) - resp (handler req) - end-time (System/nanoTime)] - (log/logf - (resp->log-level resp) - "%s %s %d %s" - (:request-method req) - (:uri req) - (:status resp) - (format-nanoseconds (- end-time start-time)))) - resp))