Skip to content

Commit f0195c5

Browse files
committed
Move logger property to autoprop 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 f0195c5

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ 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
22-
import java.util.Random
21+
import org.apache.logging.log4j.kotlin.autoprop.logger
22+
import kotlin.random.Random
2323

2424
@SuppressFBWarnings("PREDICTABLE_RANDOM", "DMI_RANDOM_USED_ONLY_ONCE")
2525
object LoggingAppExtensionProperty {
@@ -45,7 +45,7 @@ object LoggingAppExtensionProperty {
4545
}
4646

4747
fun getKey(): Int = logger.runInTrace {
48-
Random().nextInt(10)
48+
Random.nextInt(10)
4949
}
5050

5151
@SuppressFBWarnings("NP_ALWAYS_NULL")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ 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+
level = DeprecationLevel.WARNING
41+
)
3742
inline val <reified T> T.logger: KotlinLogger
3843
get() = cachedLoggerOf(T::class.java)
3944

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.autoprop
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)