You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2018/04/03 21:29:05 UTC

[1/2] logging-log4j-kotlin git commit: Add a function to create an explicitly-named logger

Repository: logging-log4j-kotlin
Updated Branches:
  refs/heads/master bdd72d3b2 -> 96b470ece


Add a function to create an explicitly-named logger


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/commit/dca80be9
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/tree/dca80be9
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/diff/dca80be9

Branch: refs/heads/master
Commit: dca80be9d6fb5012daa5f18fad6073b6e2b0c7e1
Parents: bdd72d3
Author: Raman Gupta <ro...@gmail.com>
Authored: Tue Apr 3 12:11:28 2018 -0400
Committer: Raman Gupta <ro...@gmail.com>
Committed: Tue Apr 3 14:35:35 2018 -0400

----------------------------------------------------------------------
 .../logging/log4j/kotlin/LoggingFactory.kt      | 10 +++-
 .../NamedLoggerTest.kt                          | 48 ++++++++++++++++++++
 src/main/asciidoc/usage.adoc                    | 17 ++++++-
 3 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/dca80be9/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt
----------------------------------------------------------------------
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 7a4126c..87ee9a8 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
@@ -21,11 +21,19 @@ import org.apache.logging.log4j.spi.ExtendedLogger
 import kotlin.reflect.full.companionObject
 
 /**
- * Logger instantiation by function. Use: `val log = logger()`.
+ * 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.
  */
 @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.
+ */
+fun namedLogger(name: String): KotlinLogger = KotlinLogger(LogManager.getContext(false).getLogger(name))
+
 fun loggerDelegateOf(ofClass: Class<*>): ExtendedLogger {
   return LogManager.getContext(ofClass.classLoader, false).getLogger(unwrapCompanionClass(ofClass).name)
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/dca80be9/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..28c0cfb
--- /dev/null
+++ b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/NamedLoggerTest.kt
@@ -0,0 +1,48 @@
+/*
+ * 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
+
+import org.apache.logging.log4j.Level
+import org.apache.logging.log4j.junit.LoggerContextRule
+import org.apache.logging.log4j.kotlin.support.withListAppender
+import org.junit.Rule
+import org.junit.Test
+import kotlin.test.assertEquals
+
+const val loggerName = "Foo"
+
+class NamedLoggerTest {
+  @Rule @JvmField var init = LoggerContextRule("InfoLogger.xml")
+
+  val log = namedLogger(loggerName)
+
+  @Test
+  fun `Logging from an explicitly named logger logs with the correct name`() {
+    val msg = "This is an error log."
+    val msgs = withListAppender { _, _ ->
+      log.error(msg)
+    }
+
+    assertEquals(1, msgs.size.toLong())
+
+    msgs.first().also {
+      assertEquals(Level.ERROR, it.level)
+      assertEquals(msg, it.message.format)
+      assertEquals(loggerName, it.loggerName)
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/dca80be9/src/main/asciidoc/usage.adoc
----------------------------------------------------------------------
diff --git a/src/main/asciidoc/usage.adoc b/src/main/asciidoc/usage.adoc
index e49e9e2..983936a 100644
--- a/src/main/asciidoc/usage.adoc
+++ b/src/main/asciidoc/usage.adoc
@@ -32,7 +32,7 @@ class MyClass: BaseClass, Logging {
 }
 ----
 
-The `Logging` interface can also be mixed into `object` declarations, including companions.
+The `Logging` interface can also be mixed into `object` declarations, including companions. This is generally preferable over the previous approach as there is a single logger created for every instance of the class.
 
 [source,kotlin]
 ----
@@ -103,3 +103,18 @@ class MyClass: BaseClass {
   ...
 }
 ----
+
+==== Explicitly Named Loggers
+
+An explicitly-named logger may be obtained via the `namedLogger` function:
+
+[source,kotlin]
+----
+import org.apache.logging.log4j.kotlin
+
+class MyClass: BaseClass {
+  val logger = namedLogger("MyCustomLoggerName")
+
+  ...
+}
+----


[2/2] logging-log4j-kotlin git commit: Exclude travis config from rat check

Posted by ma...@apache.org.
Exclude travis config from rat check


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/commit/96b470ec
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/tree/96b470ec
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/diff/96b470ec

Branch: refs/heads/master
Commit: 96b470ece6a12c336ace30ab71150146c9bdc017
Parents: dca80be
Author: Raman Gupta <ro...@gmail.com>
Authored: Tue Apr 3 17:19:32 2018 -0400
Committer: Raman Gupta <ro...@gmail.com>
Committed: Tue Apr 3 17:19:32 2018 -0400

----------------------------------------------------------------------
 pom.xml | 5 +++++
 1 file changed, 5 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/96b470ec/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 63a16e4..ebe7329 100644
--- a/pom.xml
+++ b/pom.xml
@@ -105,6 +105,11 @@
             </goals>
           </execution>
         </executions>
+        <configuration>
+          <excludes>
+            <exclude>.travis.yml</exclude>
+          </excludes>
+        </configuration>
       </plugin>
       <plugin>
         <groupId>org.jetbrains.kotlin</groupId>