From a785376167df58fa0651d489e45c88ceb8f1427a Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Fri, 27 Oct 2023 20:29:47 -0400 Subject: [PATCH] Logging fix --- .../onthegomap/planetiler/util/LogUtil.java | 3 ++- .../planetiler/util/LogUtilTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 planetiler-core/src/test/java/com/onthegomap/planetiler/util/LogUtilTest.java diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/LogUtil.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/LogUtil.java index 9c62403b64..11669dd877 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/LogUtil.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/LogUtil.java @@ -24,7 +24,8 @@ public static void clearStage() { /** Returns the current {@code [stage]} value prepended to log for this thread. */ public static String getStage() { - return MDC.get(STAGE_KEY); + // strip out the "[stage] " wrapper + return MDC.get(STAGE_KEY) instanceof String s ? s.substring(1, s.length() - 2) : null; } /** Prepends {@code [parent:child]} to all subsequent logs from this thread. */ diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/LogUtilTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/LogUtilTest.java new file mode 100644 index 0000000000..e668841198 --- /dev/null +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/LogUtilTest.java @@ -0,0 +1,19 @@ +package com.onthegomap.planetiler.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class LogUtilTest { + @Test + void testStageHandling() { + assertNull(LogUtil.getStage()); + LogUtil.setStage("test"); + assertEquals("test", LogUtil.getStage()); + LogUtil.setStage(LogUtil.getStage(), "child"); + assertEquals("test:child", LogUtil.getStage()); + LogUtil.clearStage(); + assertNull(LogUtil.getStage()); + } +}