Skip to content

Commit cd96ea8

Browse files
authored
Merge pull request oshai#7 from morj/master
implement logging with Throwable payload
2 parents 7d09fbb + b1dd99b commit cd96ea8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/main/kotlin/mu/KLogger.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,39 @@ class KLogger(jLogger: Logger): Logger by jLogger{
4343
inline fun error(msg: () -> Any?) {
4444
if (isErrorEnabled) error(msg.invoke().toString())
4545
}
46+
47+
/**
48+
* Lazy add a log message with throwable payload if isTraceEnabled is true
49+
*/
50+
inline fun trace(t: Throwable, msg: () -> Any?) {
51+
if (isTraceEnabled) trace(msg.invoke().toString(), t)
52+
}
53+
54+
/**
55+
* Lazy add a log message with throwable payload if isDebugEnabled is true
56+
*/
57+
inline fun debug(t: Throwable, msg: () -> Any?) {
58+
if (isDebugEnabled) debug(msg.invoke().toString(), t)
59+
}
60+
61+
/**
62+
* Lazy add a log message with throwable payload if isInfoEnabled is true
63+
*/
64+
inline fun info(t: Throwable, msg: () -> Any?) {
65+
if (isInfoEnabled) info(msg.invoke().toString(), t)
66+
}
67+
68+
/**
69+
* Lazy add a log message with throwable payload if isWarnEnabled is true
70+
*/
71+
inline fun warn(t: Throwable, msg: () -> Any?) {
72+
if (isWarnEnabled) warn(msg.invoke().toString(), t)
73+
}
74+
75+
/**
76+
* Lazy add a log message with throwable payload if isErrorEnabled is true
77+
*/
78+
inline fun error(t: Throwable, msg: () -> Any?) {
79+
if (isErrorEnabled) error(msg.invoke().toString(), t)
80+
}
4681
}

src/test/kotlin/mu/LoggingTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class ClassWithLogging {
88
fun test() {
99
logger.info{"test ClassWithLogging"}
1010
}
11+
fun testThrowable() {
12+
val ex = Throwable()
13+
logger.trace(ex){"test ChildClassWithLogging"}
14+
}
1115
}
1216
open class ClassHasLogging: KLoggable {
1317
override val logger = logger()
@@ -57,7 +61,10 @@ class LoggingTest {
5761

5862
@Test
5963
fun testMessages() {
60-
ClassWithLogging().test()
64+
ClassWithLogging().apply {
65+
test()
66+
testThrowable()
67+
}
6168
ClassInheritLogging().test()
6269
ChildClassWithLogging().test()
6370
ClassWithNamedLogging().test()

0 commit comments

Comments
 (0)