Skip to content

Commit

Permalink
Add function for composing loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
johncowie committed Jul 24, 2024
1 parent b805b4f commit a2ca6e8
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 107 deletions.
13 changes: 13 additions & 0 deletions core/src/cartus/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@
(fn [event]
(assoc event :context (merge context (:context event)))))))

(defrecord CompositeLogger
[loggers]
Logger
(log [_ level type context opts]
(doseq [logger loggers]
(log logger level type context opts))))

(defn compose-loggers
"Returns a new logger which logs to each of the provided loggers"
[logger-a logger-b & other-loggers]
(let [loggers (concat [logger-a logger-b] other-loggers)]
(map->CompositeLogger {:loggers loggers})))

(def ^:private all-levels
[:trace :debug :info :warn :error :fatal])

Expand Down
23 changes: 22 additions & 1 deletion core/test/cartus/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
logger (spying-logger)
type ::some.event
context {:some "context"}
opts {:message "Something happened"
opts {:message "Something happened"
:exception (ex-info "Oops" {:data []})}
meta (assoc meta
:ns (find-ns 'cartus.test-support.definitions))]
Expand Down Expand Up @@ -404,3 +404,24 @@
:line 5
:column 5}}}]
(map first (spy/calls (:spy logger)))))))

(deftest compose-loggers-test
(let [logger-a (spying-logger)
logger-b (spying-logger)
logger-c (spying-logger)
composite-logger (cartus/compose-loggers logger-a logger-b logger-c)
event-type ::event-1
context {:some-context "an-event"}]
^{:line 1 :column 1} (cartus/info composite-logger event-type context)
(let [expected-log {:level :info
:type event-type
:context context
:opts {:meta {:ns (find-ns 'cartus.core-test)
:line 1
:column 1}}}]
(is (= [expected-log]
(map first (spy/calls (:spy logger-a)))))
(is (= [expected-log]
(map first (spy/calls (:spy logger-b)))))
(is (= [expected-log]
(map first (spy/calls (:spy logger-c))))))))
30 changes: 22 additions & 8 deletions test/src/cartus/test.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(ns cartus.test
"A [[cartus.core/Logger]] implementation and utilities for use in tests."
(:require
[clojure.test :as test]
[clojure.set :as sets]
[clojure.test :as test]
[clojure.set :as sets]

[matcher-combinators.core :as mc-core]
[matcher-combinators.matchers :as mc-matchers]
[matcher-combinators.clj-test :as mc-test]
[matcher-combinators.core :as mc-core]
[matcher-combinators.matchers :as mc-matchers]
[matcher-combinators.clj-test :as mc-test]

[cartus.core :as cartus]
[cartus.test.matchers :as cartus-matchers])
(:import (cartus.core TransformerLogger)))
[cartus.core :as cartus]
[cartus.test.matchers :as cartus-matchers])
(:import (cartus.core CompositeLogger TransformerLogger)))

(defprotocol TestEventStorage
(-get-events [logger])
Expand Down Expand Up @@ -38,6 +38,20 @@
(-clear-events! [tx-logger]
(-clear-events! (:delegate tx-logger))))

(extend-type CompositeLogger
TestEventStorage
(-get-events [composite-logger]
(->> composite-logger
:loggers
(filter #(satisfies? TestEventStorage %))
(mapcat -get-events)))
(-clear-events! [composite-logger]
(->> composite-logger
:loggers
(filter #(satisfies? TestEventStorage %))
(map -clear-events!)
doall)))

(defn logger
"Constructs a test logger storing all logged events in an atom.
Expand Down
Loading

0 comments on commit a2ca6e8

Please sign in to comment.