Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement logging with Throwable payload #7

Merged
merged 1 commit into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
9 changes: 8 additions & 1 deletion src/test/kotlin/mu/LoggingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -57,7 +61,10 @@ class LoggingTest {

@Test
fun testMessages() {
ClassWithLogging().test()
ClassWithLogging().apply {
test()
testThrowable()
}
ClassInheritLogging().test()
ChildClassWithLogging().test()
ClassWithNamedLogging().test()
Expand Down