Skip to content

Commit

Permalink
imp: added a debug function to debug logging before logging is config…
Browse files Browse the repository at this point in the history
…ured
  • Loading branch information
testersen committed Apr 10, 2024
1 parent 3df3044 commit 1ffc0ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.tnboot.logging.configure.internal

internal fun debug(message: String) {
if (System.getProperty("io.tnboot.debug")?.contains("logging") == true) {
println("[DEBUG] (io.tnboot.logging) $message")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.tnboot.logging.configure.internal

import java.io.ByteArrayOutputStream
import java.io.PrintStream
import kotlin.test.Test
import kotlin.test.assertEquals

class DebugTest {
@Test
fun `should debug logging when logging is included in io-tnboot-logging`() = synchronized(this) {
val previousOut = System.out
val outputStream = ByteArrayOutputStream()
System.setOut(PrintStream(outputStream))

val beforeProperty = System.getProperty("io.tnboot.debug")
System.setProperty("io.tnboot.debug", "logging")

debug("Hello, World!")

System.setOut(previousOut)
beforeProperty?.let { System.setProperty("io.tnboot.debug", it) } ?: System.clearProperty("io.tnboot.debug")

assertEquals("[DEBUG] (io.tnboot.logging) Hello, World!\n", outputStream.toString())
}

@Test
fun `should not debug logging when logging is not included in io-tnboot-logging`() = synchronized(this) {
val previousOut = System.out
val outputStream = ByteArrayOutputStream()
System.setOut(PrintStream(outputStream))

val beforeProperty = System.getProperty("io.tnboot.debug")
System.setProperty("io.tnboot.debug", "something-else")

debug("Hello, World!")

System.setOut(previousOut)
beforeProperty?.let { System.setProperty("io.tnboot.debug", it) } ?: System.clearProperty("io.tnboot.debug")

assertEquals(0, outputStream.size())
}
}

0 comments on commit 1ffc0ab

Please sign in to comment.