1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
;;; logging.clj -- delegated logging for Clojure
;; by Alex Taggart
;; July 27, 2009
;; Copyright (c) Alex Taggart, July 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns
^{:author
"Alex Taggart, with contributions and suggestions by Chris Dean, Phil
Hagelberg, Richard Newman, and Timothy Pratley",
:doc
"Logging macros which delegate to a specific logging implementation. At
runtime a specific implementation is selected from, in order, Apache
commons-logging, slf4j, log4j, and finally java.util.logging.
Logging levels are specified by clojure keywords corresponding to the
values used in log4j and commons-logging:
:trace, :debug, :info, :warn, :error, :fatal
Logging occurs with the log macro, or the level-specific convenience macros,
which write either directly or via an agent. See log* for more details
regarding direct vs agent logging.
The log macros will not evaluate their 'message' unless the specific logging
level is in effect. Alternately, you can use the spy macro when you have code
that needs to be evaluated, and also want to output the code and its result to
the log.
Unless otherwise specified, the current namespace (as identified by *ns*) will
be used as the log-ns (similar to how the java class name is usually used).
Note: your log configuration should display the name that was passed to the
logging implementation, and not perform stack-inspection, otherwise you'll see
some ugly and unhelpful text in your logs.
Use the enabled? macro to write conditional code against the logging level
(beyond simply whether or not to call log, which is handled automatically).
You can redirect all java writes of System.out and System.err to the log
system by calling log-capture!. To bind *out* and *err* to the log system
invoke with-logs. In both cases a log-ns (e.g., \"com.example.captured\")
must be specified in order to namespace the output.
For those new to using a java logging library, the following is a very basic
configuration for log4j. Place it in a file called \"log4j.properties\"
and place that file (and the log4j JAR) on the classpath.
log4j.rootLogger=WARN, A1
log4j.logger.user=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p %c: %m%n
The above will print messages to the console for :debug or higher if one is
in the user namespace, and :warn or higher in all other namespaces."}
clojure.contrib.logging
[:use
[clojure.string :only [trim-newline]]
[clojure.pprint :only [code-dispatch pprint with-pprint-dispatch]]])
(defprotocol Log
"The protocol through which macros will interact with an underlying logging
implementation. Implementations should at least support the six specified
logging levels if they wish to benefit from the level-specific macros."
(impl-enabled? [log level]
"Implementation-specific check if a particular level is enabled. End-users
should not need to call this.")
(impl-write! [log level throwable message]
"Implementation-specific write of a log message. End-users should not need
to call this."))
(defprotocol LogFactory
"The protocol through which macros will obtain an instance satisfying Log as
well as providing information about the particular implementation being used.
Implementations should be bound to *log-factory* in order to be picked up by
this library."
(impl-name [factory]
"Returns some text identifying the underlying implementation.")
(impl-get-log [factory log-ns]
"Returns an implementation-specific Log by namespace. End-users should not
need to call this."))
(def ^{:doc
"The default agent used for performing logging when direct logging is
disabled. See log* for details."}
*logging-agent* (agent nil :error-mode :continue))
(def ^{:doc
"The set of levels that will require using an agent when logging from within a
running transaction. Defaults to #{:info :warn}. See log* for details."}
*tx-agent-levels* #{:info :warn})
(def ^{:doc
"Overrides the default rules for choosing between logging directly or via an
agent. Defaults to nil. See log* for details."}
*force* nil)
(defn log*
"Attempts to log a message, either directly or via an agent; does not check if
the level is enabled.
For performance reasons, an agent will only be used when invoked within a
running transaction, and only for logging levels specified by
*tx-agent-levels*. This allows those entries to only be written once the
transaction commits, and are discarded if it is retried or aborted. As
corollary, other levels (e.g., :debug, :error) will be written even from
failed transactions though at the cost of repeat messages during retries.
One can override the above by setting *force* to :direct or :agent; all
subsequent writes will be direct or via an agent, respectively."
[log level throwable message]
(if (cond
(nil? *force*) (and (clojure.lang.LockingTransaction/isRunning)
(*tx-agent-levels* level))
(= *force* :agent) true
(= *force* :direct) false)
(send-off *logging-agent*
(fn [_#] (impl-write! log level throwable message)))
(impl-write! log level throwable message)))
(declare *log-factory*) ; default LogFactory instance for calling impl-get-log
(defmacro log
"Evaluates and logs a message only if the specified level is enabled. See log*
for more details."
([level message]
`(log ~level nil ~message))
([level throwable message]
`(log ~*ns* ~level ~throwable ~message))
([log-ns level throwable message]
`(log *log-factory* ~log-ns ~level ~throwable ~message))
([log-factory log-ns level throwable message]
`(let [log# (impl-get-log ~log-factory ~log-ns)]
(if (impl-enabled? log# ~level)
(log* log# ~level ~throwable ~message)))))
(defmacro logp
"Logs a message using print style args. Can optionally take a throwable as its
second arg. See level-specific macros, e.g., debug."
{:arglists '([level message & more] [level throwable message & more])}
[level x & more]
(if (or (instance? String x) (nil? more)) ; optimize for common case
`(log ~level (print-str ~x ~@more))
`(let [log# (impl-get-log *log-factory* ~*ns*)]
(if (impl-enabled? log# ~level)
(if (instance? Throwable ~x) ; type check only when enabled
(log* log# ~level ~x (print-str ~@more))
(log* log# ~level (print-str ~x ~@more)))))))
(defmacro logf
"Logs a message using a format string and args. Can optionally take a
throwable as its second arg. See level-specific macros, e.g., debugf."
{:arglists '([level fmt & fmt-args] [level throwable fmt & fmt-args])}
[level x & more]
(if (or (instance? String x) (nil? more)) ; optimize for common case
`(log ~level (format ~x ~@more))
`(let [log# (impl-get-log *log-factory* ~*ns*)]
(if (impl-enabled? log# ~level)
(if (instance? Throwable ~x) ; type check only when enabled
(log* log# ~level ~x (format ~(first more) ~@(next more)))
(log* log# ~level (format ~x ~@more)))))))
(defmacro enabled?
"Returns true if the specific logging level is enabled. Use of this function
should only be necessary if one needs to execute alternate code paths beyond
whether the log should be written to."
([level]
`(enabled? ~level ~*ns*))
([level log-ns]
`(impl-enabled? (impl-get-log *log-factory* ~log-ns) ~level)))
(defmacro spy
"Evaluates expr and writes the form and its result to the log. Returns the
result of expr. Defaults to debug log level."
([expr]
`(spy :debug ~expr))
([level expr]
`(let [a# ~expr]
(log ~level
(let [s# (with-out-str
(with-pprint-dispatch code-dispatch ; need a better way
(pprint '~expr)
(print "=> ")
(pprint a#)))]
(trim-newline s#)))
a#)))
(defn log-stream
"Creates a PrintStream that will output to the log at the specified level."
[level log-ns]
(let [log (impl-get-log *log-factory* log-ns)]
(java.io.PrintStream.
(proxy [java.io.ByteArrayOutputStream] []
(flush []
; deal with reflection in proxy-super
(let [^java.io.ByteArrayOutputStream this this]
(proxy-super flush)
(let [message (.trim (.toString this))]
(proxy-super reset)
(if (> (.length message) 0)
(log* log level nil message))))))
true)))
(let [orig (atom nil) ; holds original System.out and System.err
monitor (Object.)] ; sync monitor for calling setOut/setErr
(defn log-capture!
"Captures System.out and System.err, piping all writes of those streams to
the log. If unspecified, levels default to :info and :error, respectively.
The specified log-ns value will be used to namespace all log entries.
Note: use with-logs to redirect output of *out* or *err*.
Warning: if the logging implementation is configured to output to System.out
(as is the default with java.util.logging) then using this function will
result in StackOverflowException when writing to the log."
; Implementation Notes:
; - only set orig when nil to preserve original out/err
; - no enabled? check before making streams since that may change later
([log-ns]
(log-capture! log-ns :info :error))
([log-ns out-level err-level]
(locking monitor
(compare-and-set! orig nil [System/out System/err])
(System/setOut (log-stream out-level log-ns))
(System/setErr (log-stream err-level log-ns)))))
(defn log-uncapture!
"Restores System.out and System.err to their original values."
[]
(locking monitor
(when-let [[out err :as v] @orig]
(swap! orig (constantly nil))
(System/setOut out)
(System/setErr err)))))
(defmacro with-logs
"Evaluates exprs in a context in which *out* and *err* write to the log. The
specified log-ns value will be used to namespace all log entries.
By default *out* and *err* write to :info and :error, respectively."
{:arglists '([log-ns & body]
[[log-ns out-level err-level] & body])}
[arg & body]
; Implementation Notes:
; - no enabled? check before making writers since that may change later
(let [[log-ns out-level err-level] (if (vector? arg)
|