Merge branch 'named-logger'
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 87ee9a8..d4ee57c 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
@@ -23,15 +23,22 @@
/**
* Logger instantiation by function. Use: `val log = logger()`. The logger will be named according to the
* receiver of the function, which can be a class or object. An alternative for explicitly named loggers is
- * the `namedLogger` function.
+ * the [logger(String)] function.
*/
@Suppress("unused")
inline fun <reified T : Any> T.logger() = loggerOf(T::class.java)
/**
- * Named logger instantiation by function. Use: `val log = namedLogger('MyLoggerName')`. Generally one should
- * prefer the `logger` function to create automatically named loggers.
+ * Named logger instantiation by function. Use: `val log = logger('MyLoggerName')`. Generally one should
+ * prefer the `logger` function to create automatically named loggers, but this is useful outside of objects,
+ * such as in top-level functions.
*/
+fun logger(name: String): KotlinLogger = KotlinLogger(LogManager.getContext(false).getLogger(name))
+
+/**
+ * @see [logger]
+ */
+@Deprecated("Replaced with logger(name)", replaceWith = ReplaceWith("logger"), level = DeprecationLevel.WARNING)
fun namedLogger(name: String): KotlinLogger = KotlinLogger(LogManager.getContext(false).getLogger(name))
fun loggerDelegateOf(ofClass: Class<*>): ExtendedLogger {
diff --git a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt
index 28c0cfb..8171ba7 100644
--- a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt
+++ b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt
@@ -28,7 +28,7 @@
class NamedLoggerTest {
@Rule @JvmField var init = LoggerContextRule("InfoLogger.xml")
- val log = namedLogger(loggerName)
+ val log = logger(loggerName)
@Test
fun `Logging from an explicitly named logger logs with the correct name`() {
diff --git a/src/site/asciidoc/usage.adoc b/src/site/asciidoc/usage.adoc
index 983936a..71829c8 100644
--- a/src/site/asciidoc/usage.adoc
+++ b/src/site/asciidoc/usage.adoc
@@ -106,15 +106,17 @@
==== Explicitly Named Loggers
-An explicitly-named logger may be obtained via the `namedLogger` function:
+An explicitly-named logger may be obtained via the `logger` function that takes a `name` parameter:
[source,kotlin]
----
import org.apache.logging.log4j.kotlin
class MyClass: BaseClass {
- val logger = namedLogger("MyCustomLoggerName")
+ val logger = logger("MyCustomLoggerName")
...
}
----
+
+This is also needed in scopes that do not have a `this` Object, such as top-level functions.