Skip to content

Commit

Permalink
[nop] Misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Aug 29, 2024
1 parent 7e8f692 commit 0ae0199
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
6 changes: 3 additions & 3 deletions projects/main/src/taoensso/telemere/tools_logging.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
(defmacro ^:private when-debug [& body] (when #_true false `(do ~@body)))

(deftype TelemereLogger [logger-name]

;; `logger-name` is typically ns string
clojure.tools.logging.impl/Logger
(enabled? [_ level]
(when-debug (println [:tools-logging/enabled? level logger-name]))
(impl/signal-allowed?
{:location {:ns logger-name} ; Typically *ns* string
{:location {:ns logger-name}
:kind :tools-logging
:level level}))

(write! [_ level throwable message]
(when-debug (println [:tools-logging/write! level logger-name]))
(impl/signal!
{:allow? true ; Pre-filtered by `enabled?` call
:location {:ns logger-name} ; Typically *ns* string
:location {:ns logger-name}
:kind :tools-logging
:level level
:error throwable
Expand Down
29 changes: 15 additions & 14 deletions projects/main/test/taoensso/telemere_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
:refer [signal! with-signal with-signals]
:rename {signal! sig!, with-signal with-sig, with-signals with-sigs}]

[taoensso.telemere.api :as api]
[taoensso.telemere.utils :as utils]
[taoensso.telemere.timbre :as timbre]
#_[taoensso.telemere.tools-logging :as tools-logging]
#_[taoensso.telemere.streams :as streams]
#?(:clj [taoensso.telemere.slf4j :as slf4j])
#?(:clj [taoensso.telemere.open-telemetry :as otel])
#?(:clj [taoensso.telemere.files :as files])
#?(:clj [clojure.tools.logging :as ctl])))
[taoensso.telemere.api :as api]
[taoensso.telemere.utils :as utils]
[taoensso.telemere.timbre :as timbre]
#_[taoensso.telemere.tools-logging :as tools-logging]
#_[taoensso.telemere.streams :as streams]
#?@(:clj
[[taoensso.telemere.slf4j :as slf4j]
[taoensso.telemere.open-telemetry :as otel]
[taoensso.telemere.files :as files]
[clojure.tools.logging :as ctl]])))

(comment
(remove-ns 'taoensso.telemere-tests)
Expand Down Expand Up @@ -687,9 +688,9 @@
(is (sm? (with-sig (-> (.atWarn sl) (.log "Hello"))) {:level :warn, :ns "my.class", :kind :slf4j, :msg_ "Hello", :inst pinst?}) "Fluent API: warn basics")])

(testing "Message formatting"
(let [msgp "X is {} and Y is {}", expected {:msg_ "X is x and Y is y", :data {:slf4j/args ["x" "y"]}}]
[(is (sm? (with-sig (.info sl msgp "x" "y")) expected) "Legacy API: formatted message, raw args")
(is (sm? (with-sig (-> (.atInfo sl) (.setMessage msgp) (.addArgument "x") (.addArgument "y") (.log))) expected) "Fluent API: formatted message, raw args")]))
(let [msgp "x={},y={}", expected {:msg_ "x=1,y=2", :data {:slf4j/args ["1" "2"]}}]
[(is (sm? (with-sig (.info sl msgp "1" "2")) expected) "Legacy API: formatted message, raw args")
(is (sm? (with-sig (-> (.atInfo sl) (.setMessage msgp) (.addArgument "1") (.addArgument "2") (.log))) expected) "Fluent API: formatted message, raw args")]))

(is (sm? (with-sig (-> (.atInfo sl) (.addKeyValue "k1" "v1") (.addKeyValue "k2" "v2") (.log))) {:data {:slf4j/kvs {"k1" "v1", "k2" "v2"}}}) "Fluent API: kvs")

Expand All @@ -698,8 +699,8 @@
m2 (#'slf4j/est-marker! "M2")
cm (#'slf4j/est-marker! "Compound" "M1" "M2")]

[(is (sm? (with-sig (.info sl cm "Hello")) {:data #:slf4j{:marker-names #{"Compound" "M1" "M2"}}}) "Legacy API: markers")
(is (sm? (with-sig (-> (.atInfo sl) (.addMarker m1) (.addMarker cm) (.log))) {:data #:slf4j{:marker-names #{"Compound" "M1" "M2"}}}) "Fluent API: markers")]))
[(is (sm? (with-sig (.info sl cm "Hello")) {:data {:slf4j/marker-names #{"Compound" "M1" "M2"}}}) "Legacy API: markers")
(is (sm? (with-sig (-> (.atInfo sl) (.addMarker m1) (.addMarker cm) (.log))) {:data {:slf4j/marker-names #{"Compound" "M1" "M2"}}}) "Fluent API: markers")]))

(testing "Errors"
[(is (sm? (with-sig (.warn sl "An error" ^Throwable ex1)) {:level :warn, :error pex1?}) "Legacy API: errors")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ static void lazyInit() {
private static IFn isAllowedFn;

static void init() {
IFn requireFn = Clojure.var("clojure.core", "require");
requireFn.invoke(Clojure.read("taoensso.telemere.slf4j"));
isAllowedFn = Clojure.var("taoensso.telemere.slf4j", "allowed?");
logFn = Clojure.var("taoensso.telemere.slf4j", "log!");
IFn requireFn = Clojure.var("clojure.core", "require");
requireFn.invoke(Clojure.read("taoensso.telemere.slf4j"));
isAllowedFn = Clojure.var("taoensso.telemere.slf4j", "allowed?");
logFn = Clojure.var("taoensso.telemere.slf4j", "log!");
}

protected TelemereLogger(String name) { this.name = name; }
Expand All @@ -72,7 +72,7 @@ static void init() {
@Override protected String getFullyQualifiedCallerName() { return null; }
@Override
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable) {
logFn.invoke(this.name, level, throwable, messagePattern, arguments, marker); // Legacy API, called after level check
logFn.invoke(this.name, level, throwable, messagePattern, arguments, marker); // Legacy API, called after level check
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ public TelemereLoggerFactory() {
TelemereLogger.lazyInit();
}

public Logger getLogger(String name) {
return loggerMap.computeIfAbsent(name, this::createLogger);
}

protected Logger createLogger(String name) {
return new TelemereLogger(name);
}

protected void reset() {
loggerMap.clear();
}
public Logger getLogger(String name) { return loggerMap.computeIfAbsent(name, this::createLogger); }
protected Logger createLogger(String name) { return new TelemereLogger(name); }
protected void reset() { loggerMap.clear(); }
}
11 changes: 5 additions & 6 deletions projects/slf4j/src/taoensso/telemere/slf4j.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns taoensso.telemere.slf4j
"Interop support for SLF4J -> Telemere.
"Interop support for SLF4Jv2 -> Telemere.
Telemere will attempt to load this ns automatically when possible.
To use Telemere as your SLF4J backend/provider, just include the
Expand All @@ -16,6 +16,7 @@
- SLF4J uses standard `ServiceLoader` mechanism to find its logging backend,
searches for `SLF4JServiceProvider` provider on classpath."

{:author "Peter Taoussanis (@ptaoussanis)"}
(:require
[taoensso.encore :as enc :refer [have have?]]
[taoensso.telemere.impl :as impl])
Expand All @@ -40,7 +41,7 @@
org.slf4j.event.EventConstants/ERROR_INT :error
(throw
(ex-info "Unexpected `org.slf4j.event.Level`"
{:level {:value level, :type (type level)}}))))
{:level (enc/typed-val level)}))))

(comment (enc/qb 1e6 (sig-level org.slf4j.event.Level/INFO))) ; 36.47

Expand Down Expand Up @@ -100,8 +101,7 @@
;;;; Interop fns (called by `TelemereLogger`)

(defn- allowed?
"Private, don't use.
Called by `com.taoensso.telemere.slf4j.TelemereLogger`."
"Called by `com.taoensso.telemere.slf4j.TelemereLogger`."
[logger-name level]
(when-debug (println [:slf4j/allowed? (sig-level level) logger-name]))
(impl/signal-allowed?
Expand Down Expand Up @@ -137,8 +137,7 @@
nil)

(defn- log!
"Private, don't use.
Called by `com.taoensso.telemere.slf4j.TelemereLogger`."
"Called by `com.taoensso.telemere.slf4j.TelemereLogger`."

;; Modern "fluent" API calls
([logger-name ^org.slf4j.event.LoggingEvent event]
Expand Down

0 comments on commit 0ae0199

Please sign in to comment.