From 1ffc0ab240a6edf5d17cd265182529850acb40d5 Mon Sep 17 00:00:00 2001 From: James Bradlee Date: Wed, 10 Apr 2024 03:57:20 +0200 Subject: [PATCH] imp: added a debug function to debug logging before logging is configured --- .../logging/configure/internal/Debug.kt | 7 ++++ .../logging/configure/internal/DebugTest.kt | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 projects/logging/logging-configure/src/main/kotlin/io/tnboot/logging/configure/internal/Debug.kt create mode 100644 projects/logging/logging-configure/src/test/kotlin/io/tnboot/logging/configure/internal/DebugTest.kt diff --git a/projects/logging/logging-configure/src/main/kotlin/io/tnboot/logging/configure/internal/Debug.kt b/projects/logging/logging-configure/src/main/kotlin/io/tnboot/logging/configure/internal/Debug.kt new file mode 100644 index 0000000..8935ee7 --- /dev/null +++ b/projects/logging/logging-configure/src/main/kotlin/io/tnboot/logging/configure/internal/Debug.kt @@ -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") + } +} diff --git a/projects/logging/logging-configure/src/test/kotlin/io/tnboot/logging/configure/internal/DebugTest.kt b/projects/logging/logging-configure/src/test/kotlin/io/tnboot/logging/configure/internal/DebugTest.kt new file mode 100644 index 0000000..94af1bc --- /dev/null +++ b/projects/logging/logging-configure/src/test/kotlin/io/tnboot/logging/configure/internal/DebugTest.kt @@ -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()) + } +}