Skip to content

Commit 658e7d2

Browse files
committed
Add readable/spyf
1 parent 2472b6f commit 658e7d2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66
([despite its flaws](https://www.youtube.com/watch?v=oyLBGkS5ICk)).
77

88
## [Unreleased]
9+
### Added
10+
- Add implementation of `spyf` to `clojure.tools.logging.readable`.
11+
912
### Changed
1013
- Decreased the per-call overhead when using SLF4J, Commons Logging, and Log4j2.
1114
Previously, their associated `logger-factory` implementations were calling

src/main/clojure/clojure/tools/logging/readable.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,13 @@
159159
{:arglists '([fmt & fmt-args] [throwable fmt & fmt-args])}
160160
[& args]
161161
`(logf :fatal ~@args))
162+
163+
(defmacro spyf
164+
"Evaluates expr and may write (logf level fmt result) to the log. Returns the
165+
result of expr. Defaults to :debug log level."
166+
([fmt expr]
167+
`(spyf :debug ~fmt ~expr))
168+
([level fmt expr]
169+
`(let [a# ~expr]
170+
(logf ~level ~fmt a#)
171+
a#)))

src/test/clojure/clojure/tools/logging/test_readable.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@
188188
(is (logged? "clojure.tools.logging.test-readable" :debug e "hello \"world\"")))))
189189

190190

191+
(deftest spyf-default
192+
(with-log
193+
(spyf "result: %s" (str "hello" " " "world"))
194+
(is (logged? "clojure.tools.logging.test-readable" :debug nil "result: \"hello world\""))))
195+
196+
(deftest spyf-level
197+
(doseq [level #{:trace :debug :info :warn :error :fatal}]
198+
(with-log
199+
(spyf level "result: %s" (str "hello" " " "world"))
200+
(is (logged? "clojure.tools.logging.test-readable" level nil "result: \"hello world\"")))))
201+
202+
191203
(deftest println-style
192204
(are [f kw] (with-log
193205
(f "hello" "world")

0 commit comments

Comments
 (0)