Skip to content

Commit 2472b6f

Browse files
committed
Decrease overhead of get-logger
The previously used static methods have baked-in setup overhead. Move that overhead to instantiation. The change in the mean time to call get-logger on my laptop: - slf4j-factory: from 10ns to 7ns - cl-factory: from 198ns to 13ns - log4j2-factory: from 2587ns to 14ns
1 parent 3f44502 commit 2472b6f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ 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+
### Changed
10+
- Decreased the per-call overhead when using SLF4J, Commons Logging, and Log4j2.
11+
Previously, their associated `logger-factory` implementations were calling
12+
library-specific static convenience methods for each call to `get-logger`.
13+
These methods have some baked-in setup overhead, which now only occurs during
14+
instantiation of the associated `logger-factory`.
915

1016
## [1.0.0] - 2020-02-27
1117
### Added

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
[log4j "1.2.16"]
1616
[org.apache.logging.log4j/log4j-api "2.8.2"]
1717
[org.apache.logging.log4j/log4j-core "2.8.2"]
18-
[commons-logging "1.1.1"]]}})
18+
[commons-logging "1.1.1"]
19+
[criterium "0.4.5"]]}})

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
[]
6262
(when (class-found? "org.slf4j.Logger")
6363
(eval
64-
`(do
64+
`(let [; Same as is done inside LoggerFactory/getLogger(String).
65+
factory# (org.slf4j.LoggerFactory/getILoggerFactory)]
6566
(extend org.slf4j.Logger
6667
Logger
6768
{:enabled?
@@ -98,15 +99,16 @@
9899
(name [_#]
99100
"org.slf4j")
100101
(get-logger [_# logger-ns#]
101-
(org.slf4j.LoggerFactory/getLogger ^String (str logger-ns#))))))))
102+
(.getLogger factory# ^String (str logger-ns#))))))))
102103

103104
(defn cl-factory
104105
"Returns a Commons Logging-based implementation of the LoggerFactory protocol, or
105106
nil if not available."
106107
[]
107108
(when (class-found? "org.apache.commons.logging.Log")
108109
(eval
109-
`(do
110+
`(let [; Same as is done inside LogFactory/getLog(String).
111+
factory# (org.apache.commons.logging.LogFactory/getFactory)]
110112
(extend org.apache.commons.logging.Log
111113
Logger
112114
{:enabled?
@@ -142,7 +144,7 @@
142144
(name [_#]
143145
"org.apache.commons.logging")
144146
(get-logger [_# logger-ns#]
145-
(org.apache.commons.logging.LogFactory/getLog (str logger-ns#))))))))
147+
(.getInstance factory# (str logger-ns#))))))))
146148

147149
(defn log4j-factory
148150
"Returns a Log4j-based implementation of the LoggerFactory protocol, or nil if
@@ -179,7 +181,9 @@
179181
[]
180182
(when (class-found? "org.apache.logging.log4j.Logger")
181183
(eval
182-
`(let [levels# {:trace org.apache.logging.log4j.Level/TRACE
184+
`(let [; Same as is done inside LogManager/getLogger(String).
185+
context# (org.apache.logging.log4j.LogManager/getContext false)
186+
levels# {:trace org.apache.logging.log4j.Level/TRACE
183187
:debug org.apache.logging.log4j.Level/DEBUG
184188
:info org.apache.logging.log4j.Level/INFO
185189
:warn org.apache.logging.log4j.Level/WARN
@@ -206,7 +210,7 @@
206210
(name [_#]
207211
"org.apache.logging.log4j")
208212
(get-logger [_# logger-ns#]
209-
(org.apache.logging.log4j.LogManager/getLogger ^String (str logger-ns#))))))))
213+
(.getLogger context# ^String (str logger-ns#))))))))
210214

211215
(defn jul-factory
212216
"Returns a java.util.logging-based implementation of the LoggerFactory protocol,

0 commit comments

Comments
 (0)