diff --git a/src/main/kotlin/mu/KLogger.kt b/src/main/kotlin/mu/KLogger.kt index 2bb325c1..61758258 100644 --- a/src/main/kotlin/mu/KLogger.kt +++ b/src/main/kotlin/mu/KLogger.kt @@ -43,4 +43,39 @@ class KLogger(jLogger: Logger): Logger by jLogger{ inline fun error(msg: () -> Any?) { if (isErrorEnabled) error(msg.invoke().toString()) } + + /** + * Lazy add a log message with throwable payload if isTraceEnabled is true + */ + inline fun trace(t: Throwable, msg: () -> Any?) { + if (isTraceEnabled) trace(msg.invoke().toString(), t) + } + + /** + * Lazy add a log message with throwable payload if isDebugEnabled is true + */ + inline fun debug(t: Throwable, msg: () -> Any?) { + if (isDebugEnabled) debug(msg.invoke().toString(), t) + } + + /** + * Lazy add a log message with throwable payload if isInfoEnabled is true + */ + inline fun info(t: Throwable, msg: () -> Any?) { + if (isInfoEnabled) info(msg.invoke().toString(), t) + } + + /** + * Lazy add a log message with throwable payload if isWarnEnabled is true + */ + inline fun warn(t: Throwable, msg: () -> Any?) { + if (isWarnEnabled) warn(msg.invoke().toString(), t) + } + + /** + * Lazy add a log message with throwable payload if isErrorEnabled is true + */ + inline fun error(t: Throwable, msg: () -> Any?) { + if (isErrorEnabled) error(msg.invoke().toString(), t) + } } diff --git a/src/test/kotlin/mu/LoggingTest.kt b/src/test/kotlin/mu/LoggingTest.kt index e48a37bf..48917e55 100644 --- a/src/test/kotlin/mu/LoggingTest.kt +++ b/src/test/kotlin/mu/LoggingTest.kt @@ -8,6 +8,10 @@ class ClassWithLogging { fun test() { logger.info{"test ClassWithLogging"} } + fun testThrowable() { + val ex = Throwable() + logger.trace(ex){"test ChildClassWithLogging"} + } } open class ClassHasLogging: KLoggable { override val logger = logger() @@ -57,7 +61,10 @@ class LoggingTest { @Test fun testMessages() { - ClassWithLogging().test() + ClassWithLogging().apply { + test() + testThrowable() + } ClassInheritLogging().test() ChildClassWithLogging().test() ClassWithNamedLogging().test()