This is an automated email from the ASF dual-hosted git repository. rgupta pushed a commit to branch rg/136-logger-property-package-change in repository https://gitbox.apache.org/repos/asf/logging-log4j-kotlin.git
commit 65985ad58da66736207411b86087363eeb112fac Author: Raman Gupta <[email protected]> AuthorDate: Fri Sep 26 13:37:36 2025 -0400 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 --- .../kotlin/sample/LoggingAppExtensionProperty.kt | 2 +- .../apache/logging/log4j/kotlin/LoggingFactory.kt | 4 ++ .../kotlin/extension/LoggingFactoryExtension.kt | 43 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) 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 <reified T : Any> 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 <reified T> 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..3932fe8 --- /dev/null +++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/extension/LoggingFactoryExtension.kt @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +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 <reified T> T.logger: KotlinLogger + get() = cachedLoggerOf(T::class.java)
