with-log will give a function that, when called, will do exactly what function-to-call did, except with the side effect that the log-statement will be printed on *out* just before function-to-call is evaluated with using the arguments of a given anonymous function.
This is an example of a Decorator Pattern - extending the behavior of an existing function by transferring it to another function, that is, an anonymous function created with-log using the form (fn ...) .
In order for the with-log decorator function to work with any conceivable function-to-call , the list of anonymous function arguments is defined so that it can be called with the number of arguments using (fn [& args] ...) . When an anonymous function calls function-to-call , it expands the argument list using the apply function).
Ways to use with-log can be:
((with-log some-fn "Calling some-fn") arg1 arg2)
or
(defn my-fn [ab] (+ ab)) (def my-fn-with-logging (with-log my-fn "Calling my-fn")) (my-fn 1 2) ; evaluates to 3 (my-fn-with-logging 1 2) ; prints "Calling my-fn" and evaluates to 3
source share