diff --git a/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt b/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt index f3f95ff..c48edc1 100644 --- a/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt +++ b/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt @@ -18,7 +18,7 @@ package org.apache.logging.log4j.kotlin.sample import edu.umd.cs.findbugs.annotations.SuppressFBWarnings import org.apache.logging.log4j.kotlin.ContextMap -import org.apache.logging.log4j.kotlin.logger +import org.apache.logging.log4j.kotlin.extension.logger import java.util.Random @SuppressFBWarnings("PREDICTABLE_RANDOM", "DMI_RANDOM_USED_ONLY_ONCE") diff --git a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt index d7dd316..859968b 100644 --- a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt +++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt @@ -34,6 +34,10 @@ inline fun T.logger() = loggerOf(T::class.java) * * @since 1.3.0 */ +@Deprecated( + "Replace with extension.logger to avoid unintended consequences with explicitly declared logger properties. This will be removed in the next major release.", + replaceWith = ReplaceWith("logger", "org.apache.logging.log4j.kotlin.extension.logger") +) inline val T.logger: KotlinLogger get() = cachedLoggerOf(T::class.java) diff --git a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/extension/LoggingFactoryExtension.kt b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/extension/LoggingFactoryExtension.kt new file mode 100644 index 0000000..deda765 --- /dev/null +++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/extension/LoggingFactoryExtension.kt @@ -0,0 +1,27 @@ +package org.apache.logging.log4j.kotlin.extension + +import org.apache.logging.log4j.kotlin.KotlinLogger +import org.apache.logging.log4j.kotlin.cachedLoggerOf + +/** + * Provides a logger named after the receiver object's class. + * + * Simply import this property and use it. + * + * ``` + * import org.apache.logging.log4j.kotlin.extension.logger + * + * class MyClass { + * // use `logger` as necessary + * } + * ``` + * + * Note that this is significantly slower than creating a logger explicitly, as it requires a lookup of the + * logger on each call via the property getter. We attempt to minimize the overhead of this by caching the + * loggers, but according to microbenchmarks, it is still about 3.5 times slower than creating a logger once + * and using it (about 4.2 nanoseconds per call instead of 1.2 nanoseconds). + * + * @since 1.3.0 + */ +inline val T.logger: KotlinLogger + get() = cachedLoggerOf(T::class.java)