Skip to content

Commit b64b0f2

Browse files
committed
Move logger property to extension package
The `logger` property conflicts with explicitly defined logger properties. Given the performance considerations of the `logger` property, move it to a separate package so imports of it are explicit and non-conflicting. Resolves #136
1 parent 95c7ef2 commit b64b0f2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package org.apache.logging.log4j.kotlin.sample
1818

1919
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
2020
import org.apache.logging.log4j.kotlin.ContextMap
21-
import org.apache.logging.log4j.kotlin.logger
21+
import org.apache.logging.log4j.kotlin.extension.logger
2222
import java.util.Random
2323

2424
@SuppressFBWarnings("PREDICTABLE_RANDOM", "DMI_RANDOM_USED_ONLY_ONCE")

log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inline fun <reified T : Any> T.logger() = loggerOf(T::class.java)
3434
*
3535
* @since 1.3.0
3636
*/
37+
@Deprecated(
38+
"Replace with autoprop.logger to avoid unintended consequences with explicitly declared logger properties. This will be removed in the next major release.",
39+
replaceWith = ReplaceWith("logger", "org.apache.logging.log4j.kotlin.autoprop.logger")
40+
)
3741
inline val <reified T> T.logger: KotlinLogger
3842
get() = cachedLoggerOf(T::class.java)
3943

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.logging.log4j.kotlin.extension
2+
3+
import org.apache.logging.log4j.kotlin.KotlinLogger
4+
import org.apache.logging.log4j.kotlin.cachedLoggerOf
5+
6+
/**
7+
* Provides a logger named after the receiver object's class.
8+
*
9+
* Simply import this property and use it.
10+
*
11+
* ```
12+
* import org.apache.logging.log4j.kotlin.autoprop.logger
13+
*
14+
* class MyClass {
15+
* // use `logger` as necessary
16+
* }
17+
* ```
18+
*
19+
* Note that this is significantly slower than creating a logger explicitly, as it requires a lookup of the
20+
* logger on each call via the property getter. We attempt to minimize the overhead of this by caching the
21+
* loggers, but according to microbenchmarks, it is still about 3.5 times slower than creating a logger once
22+
* and using it (about 4.2 nanoseconds per call instead of 1.2 nanoseconds).
23+
*
24+
* @since 1.3.0
25+
*/
26+
inline val <reified T> T.logger: KotlinLogger
27+
get() = cachedLoggerOf(T::class.java)

0 commit comments

Comments
 (0)