Skip to content

Commit

Permalink
Fix #160 - throw error instead of show in log
Browse files Browse the repository at this point in the history
in case system property is defined: kotlin-logging.throwOnMessageError
  • Loading branch information
oshai committed Dec 6, 2020
1 parent 4037ec8 commit 40beff5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/commonMain/kotlin/mu/internal/MessageInvoker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ internal inline fun (() -> Any?).toStringSafe(): String {
return try {
invoke().toString()
} catch (e: Exception) {
"Log message invocation failed: $e"
ErrorMessageProducer.getErrorLog(e)
}
}

public expect object ErrorMessageProducer {
public fun getErrorLog(e: Exception): String
}
5 changes: 5 additions & 0 deletions src/jsMain/kotlin/mu/internal/ErrorMessageProducer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mu.internal

public actual object ErrorMessageProducer {
public actual fun getErrorLog(e: Exception): String = "Log message invocation failed: $e"
}
12 changes: 12 additions & 0 deletions src/jvmMain/kotlin/mu/internal/ErrorMessageProducer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mu.internal

public actual object ErrorMessageProducer {
public actual fun getErrorLog(e: Exception): String {
if (System.getProperties().containsKey("kotlin-logging.throwOnMessageError")) {
throw e
} else {
return "Log message invocation failed: $e"
}
}

}
27 changes: 27 additions & 0 deletions src/jvmTest/kotlin/mu/internal/MessageInvokerJavaTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mu.internal

import org.junit.Test
import kotlin.test.assertEquals

class MessageInvokerJavaTest {

@Test
fun toStringSafeChecks() {
assertEquals("hi", { "hi" }.toStringSafe())
}

@Test
fun toStringSafeChecksThrowException() {
assertEquals("Log message invocation failed: java.lang.Exception: hi", { throw Exception("hi") }.toStringSafe())
}

@Test(expected = Exception::class)
fun toStringSafeChecksThrowExceptionWithSystemProperty() {
System.setProperty("kotlin-logging.throwOnMessageError", "")
try {
{ throw Exception("hi") }.toStringSafe()
} finally {
System.clearProperty("kotlin-logging.throwOnMessageError")
}
}
}
5 changes: 5 additions & 0 deletions src/nativeMain/kotlin/mu/internal/ErrorMessageProducer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mu.internal

public actual object ErrorMessageProducer {
public actual fun getErrorLog(e: Exception): String = "Log message invocation failed: $e"
}

0 comments on commit 40beff5

Please sign in to comment.