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/03/27 17:55:23 UTC
[03/25] logging-log4j-kotlin git commit: Instantiate logger via =,
not by, trace, build updates
Instantiate logger via =, not by, trace, build updates
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/5c64cf41
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/tree/5c64cf41
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/diff/5c64cf41
Branch: refs/heads/master
Commit: 5c64cf417955e4b084cf90cbea4aaedf460d4c23
Parents: 3a2eb84
Author: Raman Gupta <ro...@gmail.com>
Authored: Thu Dec 8 11:43:00 2016 -0500
Committer: Raman Gupta <ro...@gmail.com>
Committed: Thu Dec 8 11:43:00 2016 -0500
----------------------------------------------------------------------
.../logging/log4j/kotlin/sample/LoggingApp.kt | 34 ++++++++++----
.../org/apache/logging/log4j/kotlin/Logger.kt | 44 ++++++++++++++----
.../LoggerCompanionTest.kt | 30 ++++++++++++
.../LoggerTest.kt | 9 +++-
.../src/test/resources/log4j2-test.xml | 31 +++++++++++++
pom.xml | 48 ++++++++++++++++++++
6 files changed, 176 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingApp.kt
----------------------------------------------------------------------
diff --git a/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingApp.kt b/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingApp.kt
index 33f9d0e..43890ea 100644
--- a/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingApp.kt
+++ b/log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingApp.kt
@@ -18,9 +18,10 @@ package org.apache.logging.log4j.kotlin.sample
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.kotlin.logger
+import java.util.*
object LoggingApp {
- val log by logger()
+ val log = logger()
@JvmStatic
fun main(args: Array<String>) {
@@ -30,15 +31,30 @@ object LoggingApp {
log.info { "Hello, world: $s1 $s2" }
- log.traceEntry()
- log.traceEntry(s1, s2)
- val entryMessage = log.traceEntry("foobar", "")
+ log.trace("Regular trace")
- log.traceExit()
- log.traceExit(s2)
- log.traceExit(entryMessage)
- log.traceExit(entryMessage, s2)
- log.traceExit("bonsai", s2)
+ log.trace {
+ log.info("Inside trace extension!")
+ }
+
+ log.trace({ "Trace extension with entry message." }) {
+ log.info("Inside trace extension with supplier!")
+ }
+
+ fun getKey(): Int = log.trace {
+ Random().nextInt(10)
+ }
+
+ fun getKeyError(): Int = log.trace {
+ throw Exception("Oops!")
+ }
+
+ log.info { "Key was ${getKey()}" }
+ try {
+ log.info { "Key was ${getKeyError()}" }
+ } catch(e: Exception) {
+ Unit
+ }
log.throwing(t)
log.throwing(Level.INFO, t)
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logger.kt
----------------------------------------------------------------------
diff --git a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logger.kt b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logger.kt
index 8e32d54..6e4920c 100644
--- a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logger.kt
+++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/Logger.kt
@@ -16,7 +16,6 @@
*/
package org.apache.logging.log4j.kotlin
-import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.apache.logging.log4j.Marker
@@ -51,20 +50,15 @@ import kotlin.reflect.companionObject
* ```
* log.error(exc) { "Unexpected exception evaluating $whatever." }
* ```
+ *
+ * One known limitation of the Kotlin logging API is that location aware logging does not work
*/
-class FunctionalLogger(val log: ExtendedLogger): ReadOnlyProperty<Any?, FunctionalLogger>, Logger by log {
+class FunctionalLogger(val log: ExtendedLogger): Logger by log {
companion object {
@Suppress("NOTHING_TO_INLINE")
inline fun <T: Any?> (() -> T).asLog4jSupplier(): Supplier<T> = Supplier { invoke() }
}
- // allows access to FunctionalLogger as a property delegate
- override operator fun getValue(thisRef: Any?, property: KProperty<*>) = this
-
- inline fun trace(crossinline supplier: () -> Any?) {
- log.trace(supplier.asLog4jSupplier())
- }
-
inline fun trace(t: Throwable, crossinline supplier: () -> Any?) {
log.trace(supplier.asLog4jSupplier(), t)
}
@@ -156,10 +150,40 @@ class FunctionalLogger(val log: ExtendedLogger): ReadOnlyProperty<Any?, Function
inline fun fatal(marker: Marker?, t: Throwable?, crossinline supplier: () -> Any?) {
log.fatal(marker, supplier.asLog4jSupplier(), t)
}
+
+ inline fun <R : Any?> trace(block: () -> R): R {
+ val entry = traceEntry()
+ try {
+ val result = block()
+ when(result) {
+ is Unit -> traceExit(entry)
+ else -> traceExit(entry, result)
+ }
+ return result
+ } catch (e: Throwable) {
+ catching(e)
+ throw e
+ }
+ }
+
+ inline fun <R : Any?> trace(crossinline supplier: () -> Any?, block: () -> R): R {
+ val entry = traceEntry(supplier.asLog4jSupplier())
+ try {
+ val result = block()
+ when(result) {
+ is Unit -> traceExit(entry)
+ else -> traceExit(entry, result)
+ }
+ return result
+ } catch (e: Throwable) {
+ catching(e)
+ throw e
+ }
+ }
}
/**
- * A delegate-based logger instantiation. Use: `val log by logger()`.
+ * Logger instantiation. Use: `val log = logger()`.
*/
@Suppress("unused")
inline fun <reified T : Any> T.logger(): FunctionalLogger =
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerCompanionTest.kt
----------------------------------------------------------------------
diff --git a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerCompanionTest.kt b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerCompanionTest.kt
new file mode 100644
index 0000000..c7c9ba0
--- /dev/null
+++ b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerCompanionTest.kt
@@ -0,0 +1,30 @@
+/*
+ * 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.junit.Test
+
+class LoggerCompanionTest {
+ companion object {
+ val log = logger()
+ }
+
+ @Test
+ fun `Logging from companion logger works!`() {
+ log.debug("This is a debug log.")
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerTest.kt
----------------------------------------------------------------------
diff --git a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerTest.kt b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerTest.kt
index 5abf847..8f6ada6 100644
--- a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerTest.kt
+++ b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/LoggerTest.kt
@@ -16,6 +16,13 @@
*/
package org.apache.logging.log4j.kotlin
+import org.junit.Test
+
class LoggerTest {
- // TODO
+ val log = logger()
+
+ @Test
+ fun `Logging works!`() {
+ log.debug("This is a debug log.")
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/log4j-api-kotlin/src/test/resources/log4j2-test.xml
----------------------------------------------------------------------
diff --git a/log4j-api-kotlin/src/test/resources/log4j2-test.xml b/log4j-api-kotlin/src/test/resources/log4j2-test.xml
new file mode 100644
index 0000000..4113137
--- /dev/null
+++ b/log4j-api-kotlin/src/test/resources/log4j2-test.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<Configuration name="KotlinTests" status="error">
+ <Appenders>
+ <Console name="Console">
+ <PatternLayout>
+ <Pattern>%d %5p %c{1} %X %F:%L - %m%n</Pattern>
+ </PatternLayout>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="TRACE">
+ <AppenderRef ref="Console"/>
+ </Root>
+ </Loggers>
+</Configuration>
http://git-wip-us.apache.org/repos/asf/logging-log4j-kotlin/blob/5c64cf41/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 422428c..0453ad0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,6 +34,12 @@
<url>https://git-wip-us.apache.org/repos/asf?p=logging-log4j-kotlin.git;a=summary</url>
<tag>log4j-${Log4jReleaseVersion}</tag>
</scm>
+
+ <properties>
+ <kotlin.version>1.0.5-2</kotlin.version>
+ </properties>
+
+
<dependencyManagement>
<dependencies>
<dependency>
@@ -43,6 +49,48 @@
</dependency>
</dependencies>
</dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-stdlib</artifactId>
+ <version>${kotlin.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-test</artifactId>
+ <version>${kotlin.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-maven-plugin</artifactId>
+ <version>${kotlin.version}</version>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>test-compile</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>test-compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+
<modules>
<module>log4j-api-kotlin</module>
<module>log4j-api-kotlin-sample</module>