Skip to content

Commit 558efe8

Browse files
committed
Ensure that traces that are functions are evaluated when logging due to *log-traces*
1 parent df8c2b5 commit 558efe8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/io/aviso/tracker.clj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
(def ^:dynamic ^:private innermost-exception)
1212

13-
(def ^:dynamic *log-traces* "A boolean to control the whether traces are logged at :trace level as they are created" false)
13+
(def ^:dynamic *log-traces*
14+
"A boolean to control the whether traces are logged at :trace level as they are created."
15+
false)
1416

1517
(defn- trace-to-string
1618
"Converts a trace to a string; a trace may be a function which is invoked."
@@ -29,14 +31,20 @@
2931
(defn track*
3032
"Tracks the execution of a function of no arguments. The trace macro converts into a call to track*.
3133
32-
logger - SLF4J Logger where logging should occur
33-
trace-message - String, object, or function. Function evaulation is deferred until an exception is actually thrown.
34-
f - function to invoke."
34+
logger
35+
: SLF4J Logger where logging should occur
36+
37+
trace-message
38+
: String, object, or function. If a function, the function will only be evaluated if the message
39+
needs to be printed (when an exception occurs, or when logging traces via [[*log-traces*]]).
40+
41+
f
42+
: function to invoke. track* returns the result from this function."
3543
[logger trace f]
3644
(if (bound? #'operation-traces)
3745
(try
3846
(swap! operation-traces conj trace)
39-
(when *log-traces* (l/log* logger :trace nil trace))
47+
(when *log-traces* (l/log* logger :trace nil (trace-to-string trace)))
4048
(f)
4149
(catch Throwable e
4250
(if @innermost-exception
@@ -67,7 +75,7 @@
6775
point of the exception will be logged (the logger is determined from the current namespace); thus logging only occurs at the
6876
most deeply nested trace. The exception thrown is formatted and logged as well.
6977
70-
trace may be a string, or a function that returns a string; execution of the function is deferred until needed. "
78+
trace may be a string, an object, or a function that returns a string; execution of the function is deferred until needed."
7179
[trace & body]
7280
`(track* (get-logger ~*ns*) ~trace #(do ~@body)))
7381

0 commit comments

Comments
 (0)