|
19 | 19 | Apache commons-logging, log4j, and finally java.util.logging.
|
20 | 20 |
|
21 | 21 | Logging levels are specified by clojure keywords corresponding to the
|
22 |
| - values used in log4j/commons-logging: |
| 22 | + values used in log4j and commons-logging: |
23 | 23 | :trace, :debug, :info, :warn, :error, :fatal
|
24 | 24 |
|
25 | 25 | Logging occurs with the log macro, or the level-specific convenience macros,
|
|
28 | 28 | logging is invoked within a transaction it will always use an agent.
|
29 | 29 |
|
30 | 30 | The log macros will not evaluate their 'message' unless the specific logging
|
31 |
| - level is in effect. |
32 |
| - |
33 |
| - Alternately, you can use the spy macro when you have code that needs to be |
34 |
| - evaluated, and also want to output the code and its result to the debug log. |
| 31 | + level is in effect. Alternately, you can use the spy macro when you have code |
| 32 | + that needs to be evaluated, and also want to output the code and its result to |
| 33 | + the debug log. |
35 | 34 |
|
36 | 35 | Unless otherwise specified, the current namespace (as identified by *ns*) will
|
37 | 36 | be used as the log-ns (similar to how the java class name is usually used).
|
| 37 | + Note: your log configuration should display the name that was passed to the |
| 38 | + logging implementation, and not perform stack-inspection, otherwise you'll see |
| 39 | + something like \"clojure.contrib.logging$fn__72$write__39__auto____81 invoke\" |
| 40 | + in your logs. |
38 | 41 |
|
39 | 42 | Use the enabled? function to write conditional code against the logging level
|
40 | 43 | (beyond simply whether or not to call log, which is handled automatically).
|
|
64 | 67 | (try
|
65 | 68 | (import (org.apache.commons.logging LogFactory Log))
|
66 | 69 | `(letfn [(get-log# [log-ns#]
|
67 |
| - (LogFactory/getLog log-ns#)) |
68 |
| - (enabled?# [log# level#] |
| 70 | + (LogFactory/getLog #^String log-ns#)) |
| 71 | + (enabled?# [#^org.apache.commons.logging.Log log# level#] |
69 | 72 | (condp = level#
|
70 | 73 | :trace (.isTraceEnabled log#)
|
71 | 74 | :debug (.isDebugEnabled log#)
|
72 | 75 | :info (.isInfoEnabled log#)
|
73 | 76 | :warn (.isWarnEnabled log#)
|
74 | 77 | :error (.isErrorEnabled log#)
|
75 | 78 | :fatal (.isFatalEnabled log#)))
|
76 |
| - (write# [log# level# msg# e#] |
| 79 | + (write# [#^org.apache.commons.logging.Log log# level# msg# e#] |
77 | 80 | (condp = level#
|
78 | 81 | :trace (.trace log# msg# e#)
|
79 | 82 | :debug (.debug log# msg# e#)
|
|
98 | 101 | :error Level/ERROR
|
99 | 102 | :fatal Level/FATAL}]
|
100 | 103 | (letfn [(get-log# [log-ns#]
|
101 |
| - (Logger/getLogger log-ns#)) |
102 |
| - (enabled?# [log# level#] |
| 104 | + (org.apache.log4j.Logger/getLogger #^String log-ns#)) |
| 105 | + (enabled?# [#^org.apache.log4j.Logger log# level#] |
103 | 106 | (.isEnabledFor log# (levels# level#)))
|
104 |
| - (write# [log# level# msg# e#] |
| 107 | + (write# [#^org.apache.log4j.Logger log# level# msg# e#] |
105 | 108 | (if-not e#
|
106 | 109 | (.log log# (levels# level#) msg#)
|
107 | 110 | (.log log# (levels# level#) msg# e#)))]
|
|
122 | 125 | :error Level/SEVERE
|
123 | 126 | :fatal Level/SEVERE}]
|
124 | 127 | (letfn [(get-log# [log-ns#]
|
125 |
| - (Logger/getLogger log-ns#)) |
126 |
| - (enabled?# [log# level#] |
| 128 | + (java.util.logging.Logger/getLogger log-ns#)) |
| 129 | + (enabled?# [#^java.util.logging.Logger log# level#] |
127 | 130 | (.isLoggable log# (levels# level#)))
|
128 |
| - (write# [log# level# msg# e#] |
| 131 | + (write# [#^java.util.logging.Logger log# level# msg# e#] |
129 | 132 | (if-not e#
|
130 |
| - (.log log# (levels# level#) (str msg#)) |
131 |
| - (.log log# (levels# level#) (str msg#) e#)))] |
| 133 | + (.log log# #^java.util.logging.Level (levels# level#) |
| 134 | + #^String (str msg#)) |
| 135 | + (.log log# #^java.util.logging.Level (levels# level#) |
| 136 | + #^String (str msg#) #^Throwable e#)))] |
132 | 137 | (struct log-system "java-logging" get-log# enabled?# write#)))
|
133 | 138 | (catch Exception e nil)))
|
134 | 139 |
|
|
208 | 213 | (proxy [java.io.ByteArrayOutputStream] []
|
209 | 214 | (flush []
|
210 | 215 | (proxy-super flush)
|
211 |
| - (let [s (.trim (.toString this))] |
| 216 | + (let [s (.trim (.toString #^java.io.ByteArrayOutputStream this))] |
212 | 217 | (proxy-super reset)
|
213 | 218 | (if (> (.length s) 0)
|
214 | 219 | (log level s nil log-ns)))))
|
|
0 commit comments