Skip to content

Commit 961a526

Browse files
committed
Merge branch 'log-testing'
2 parents dec1854 + bb3afa6 commit 961a526

8 files changed

Lines changed: 663 additions & 171 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ 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 support for testing logs in `clojure.tools.logging.test`
11+
12+
### Changed
13+
- Now passes the context classloader of current thread to `Class/forName` when
14+
determining whether logging implementation classes are available on the
15+
classpath. This was done to allow testing of the various `impl/*-factory`
16+
functions, and seems consistent with clojure internals.
917

1018
## [0.4.1] - 2018-05-07
1119
### Fixed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The following short example should give you what you need to get started:
2727
(defn divide [x y]
2828
(log/info "dividing" x "by" y)
2929
(try
30-
(log/spyf "result: %s" (/ x y)) ; yields the result
30+
(log/spyf "result is %s" (/ x y)) ; yields the result
3131
(catch Exception ex
3232
(log/error ex "There was an error in calculation"))))
3333
```

project.clj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
:source-paths ["src/main/clojure"]
77
:test-paths ["src/test/clojure"]
88
:dependencies [[org.clojure/clojure "1.3.0"]]
9-
:profiles {:test {:dependencies [[org.slf4j/slf4j-api "1.6.2"]
10-
[org.slf4j/slf4j-log4j12 "1.6.2"]
11-
[log4j "1.2.16"]
12-
[org.apache.logging.log4j/log4j-api "2.8.2"]
13-
[org.apache.logging.log4j/log4j-core "2.8.2"]
14-
[commons-logging "1.1.1"]]}})
9+
:aliases {"cloverage" ["with-profile" "dev,cloverage" "cloverage" "--fail-threshold" "100"]}
10+
:profiles {:cloverage {:plugins [[lein-cloverage "1.0.9"]]}
11+
:dev {:dependencies [[org.clojure/clojure "1.8.0"]
12+
[org.clojure/test.check "0.9.0"]
13+
[org.slf4j/slf4j-api "1.6.2"]
14+
[org.slf4j/slf4j-log4j12 "1.6.2"]
15+
[log4j "1.2.16"]
16+
[org.apache.logging.log4j/log4j-api "2.8.2"]
17+
[org.apache.logging.log4j/log4j-core "2.8.2"]
18+
[commons-logging "1.1.1"]]}})

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@
4444
(name [_] "disabled")
4545
(get-logger [_ _] disabled-logger)))
4646

47+
(defn class-found?
48+
"Returns true if the Class associated with the given classname can be found
49+
using the context ClassLoader for the current thread."
50+
[name]
51+
(try
52+
(Class/forName name true (.. Thread currentThread getContextClassLoader))
53+
true
54+
(catch ClassNotFoundException _
55+
false)))
56+
57+
4758
(defn slf4j-factory
4859
"Returns a SLF4J-based implementation of the LoggerFactory protocol, or nil if
4960
not available."
5061
[]
51-
(try
52-
(Class/forName "org.slf4j.Logger")
62+
(when (class-found? "org.slf4j.Logger")
5363
(eval
5464
`(do
5565
(extend org.slf4j.Logger
@@ -88,15 +98,13 @@
8898
(name [_#]
8999
"org.slf4j")
90100
(get-logger [_# logger-ns#]
91-
(org.slf4j.LoggerFactory/getLogger ^String (str logger-ns#))))))
92-
(catch Exception e nil)))
101+
(org.slf4j.LoggerFactory/getLogger ^String (str logger-ns#))))))))
93102

94103
(defn cl-factory
95104
"Returns a Commons Logging-based implementation of the LoggerFactory protocol, or
96105
nil if not available."
97106
[]
98-
(try
99-
(Class/forName "org.apache.commons.logging.Log")
107+
(when (class-found? "org.apache.commons.logging.Log")
100108
(eval
101109
`(do
102110
(extend org.apache.commons.logging.Log
@@ -134,15 +142,13 @@
134142
(name [_#]
135143
"org.apache.commons.logging")
136144
(get-logger [_# logger-ns#]
137-
(org.apache.commons.logging.LogFactory/getLog (str logger-ns#))))))
138-
(catch Exception e nil)))
145+
(org.apache.commons.logging.LogFactory/getLog (str logger-ns#))))))))
139146

140147
(defn log4j-factory
141148
"Returns a Log4j-based implementation of the LoggerFactory protocol, or nil if
142149
not available."
143150
[]
144-
(try
145-
(Class/forName "org.apache.log4j.Logger")
151+
(when (class-found? "org.apache.log4j.Logger")
146152
(eval
147153
`(let [levels# {:trace org.apache.log4j.Level/TRACE
148154
:debug org.apache.log4j.Level/DEBUG
@@ -165,15 +171,13 @@
165171
(name [_#]
166172
"org.apache.log4j")
167173
(get-logger [_# logger-ns#]
168-
(org.apache.log4j.Logger/getLogger ^String (str logger-ns#))))))
169-
(catch Exception e nil)))
174+
(org.apache.log4j.Logger/getLogger ^String (str logger-ns#))))))))
170175

171176
(defn log4j2-factory
172177
"Returns a Log4j2-based implementation of the LoggerFactory protocol, or nil if
173178
not available."
174179
[]
175-
(try
176-
(Class/forName "org.apache.logging.log4j.Logger")
180+
(when (class-found? "org.apache.logging.log4j.Logger")
177181
(eval
178182
`(let [levels# {:trace org.apache.logging.log4j.Level/TRACE
179183
:debug org.apache.logging.log4j.Level/DEBUG
@@ -202,15 +206,13 @@
202206
(name [_#]
203207
"org.apache.logging.log4j")
204208
(get-logger [_# logger-ns#]
205-
(org.apache.logging.log4j.LogManager/getLogger ^String (str logger-ns#))))))
206-
(catch Exception e nil)))
209+
(org.apache.logging.log4j.LogManager/getLogger ^String (str logger-ns#))))))))
207210

208211
(defn jul-factory
209212
"Returns a java.util.logging-based implementation of the LoggerFactory protocol,
210213
or nil if not available."
211214
[]
212-
(try
213-
(Class/forName "java.util.logging.Logger")
215+
(when (class-found? "java.util.logging.Logger")
214216
(eval
215217
`(let [levels# {:trace java.util.logging.Level/FINEST
216218
:debug java.util.logging.Level/FINE
@@ -234,8 +236,7 @@
234236
(name [_#]
235237
"java.util.logging")
236238
(get-logger [_# logger-ns#]
237-
(java.util.logging.Logger/getLogger (str logger-ns#))))))
238-
(catch Exception e nil)))
239+
(java.util.logging.Logger/getLogger (str logger-ns#))))))))
239240

240241
(defn find-factory
241242
"Returns the first non-nil value from slf4j-factory, cl-factory,

0 commit comments

Comments
 (0)