From fa28421064e8245c5bf0c2cde9e3f958a7bded26 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Thu, 28 May 2026 23:58:25 +0000 Subject: [PATCH 1/3] refactor: migrate Logstash logging to MDC and Google Cloud Logging appender while updating dependencies and test configuration --- run/logging-manual/pom.xml | 28 +++++++++++++++++-- .../main/java/com/example/cloudrun/App.java | 25 ++++++++--------- .../src/main/resources/logback.xml | 17 +++-------- .../java/com/example/cloudrun/AppTest.java | 14 ++++++---- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/run/logging-manual/pom.xml b/run/logging-manual/pom.xml index 913a536b45c..ab26a9d3886 100644 --- a/run/logging-manual/pom.xml +++ b/run/logging-manual/pom.xml @@ -26,8 +26,21 @@ limitations under the License. UTF-8 17 17 + true + + + + com.google.cloud + libraries-bom + 26.32.0 + pom + import + + + + com.sparkjava @@ -40,9 +53,13 @@ limitations under the License. 2.0.12 - net.logstash.logback - logstash-logback-encoder - 7.4 + com.google.cloud + google-cloud-logging-logback + + + ch.qos.logback + logback-core + 1.4.14 ch.qos.logback @@ -74,6 +91,11 @@ limitations under the License. + + org.jacoco + jacoco-maven-plugin + 0.8.12 + diff --git a/run/logging-manual/src/main/java/com/example/cloudrun/App.java b/run/logging-manual/src/main/java/com/example/cloudrun/App.java index df04d15df67..3f5372f2150 100644 --- a/run/logging-manual/src/main/java/com/example/cloudrun/App.java +++ b/run/logging-manual/src/main/java/com/example/cloudrun/App.java @@ -16,7 +16,6 @@ package com.example.cloudrun; -import static net.logstash.logback.argument.StructuredArguments.kv; import static spark.Spark.get; import static spark.Spark.port; @@ -27,6 +26,7 @@ import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; public class App { @@ -41,30 +41,27 @@ public static void main(String[] args) { "/", (req, res) -> { // [START cloudrun_manual_logging] - // Build structured log messages as an object. - Object globalLogFields = null; - // Add log correlation to nest all log messages beneath request log in Log Viewer. // TODO(developer): delete this code if you're creating a Cloud // Function and it is *NOT* triggered by HTTP. String traceHeader = req.headers("x-cloud-trace-context"); if (traceHeader != null && project != null) { String trace = traceHeader.split("/")[0]; - globalLogFields = - kv( - "logging.googleapis.com/trace", - String.format("projects/%s/traces/%s", project, trace)); + MDC.put( + "logging.googleapis.com/trace", + String.format("projects/%s/traces/%s", project, trace)); } // -- End log correlation code -- - // Create a structured log entry using key value pairs. + // Create a structured log entry using MDC (Mapped Diagnostic Context) keys. // For instantiating the "logger" variable, see // https://cloud.google.com/run/docs/logging#run_manual_logging-java - logger.error( - "This is the default display field.", - kv("component", "arbitrary-property"), - kv("severity", "NOTICE"), - globalLogFields); + MDC.put("component", "arbitrary-property"); + + logger.info("This is the default display field."); + + // Clear MDC at the end of the request to avoid resource leaks + MDC.clear(); // [END cloudrun_manual_logging] res.status(200); return "Hello Logger!"; diff --git a/run/logging-manual/src/main/resources/logback.xml b/run/logging-manual/src/main/resources/logback.xml index e7dcb0430ec..11499e3388b 100644 --- a/run/logging-manual/src/main/resources/logback.xml +++ b/run/logging-manual/src/main/resources/logback.xml @@ -1,21 +1,12 @@ - - - - - [ignore] - [ignore] - [ignore] - [ignore] - [ignore] - [ignore] - - + + + true - + diff --git a/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java b/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java index e56027b7184..9dcf118def6 100644 --- a/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java +++ b/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java @@ -34,11 +34,15 @@ public class AppTest { - private ByteArrayOutputStream bout; - private PrintStream out; + private static ByteArrayOutputStream bout; + private static PrintStream out; @BeforeClass public static void beforeClass() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + App app = new App(); app.main(new String[] {}); awaitInitialization(); @@ -51,9 +55,7 @@ public static void afterClass() { @Before public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); + bout.reset(); } @Test @@ -63,7 +65,7 @@ public void shouldSucceed() throws IOException { assertEquals("Hello Logger!", response.body); String output = bout.toString(); assertTrue(output.toString().contains("This is the default display field.")); - assertTrue(output.toString().contains("NOTICE")); + assertTrue(output.toString().contains("INFO")); assertTrue(output.toString().contains("arbitrary-property")); } From 8a50e14dc28870b2fc28130e48cf48740d40b528 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 29 May 2026 16:50:34 +0000 Subject: [PATCH 2/3] build: removed Jacoco library override and skip for local testing, not neccesary for cloud test --- run/logging-manual/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/run/logging-manual/pom.xml b/run/logging-manual/pom.xml index ab26a9d3886..3427f9c4a94 100644 --- a/run/logging-manual/pom.xml +++ b/run/logging-manual/pom.xml @@ -26,7 +26,6 @@ limitations under the License. UTF-8 17 17 - true @@ -91,11 +90,6 @@ limitations under the License. - - org.jacoco - jacoco-maven-plugin - 0.8.12 - From 77d6e3e34fb4b8525619dd859f58d5da93e028c6 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 29 May 2026 19:54:07 +0000 Subject: [PATCH 3/3] fix: restore original System.out in AppTest teardown to prevent side effects on other tests --- .../src/test/java/com/example/cloudrun/AppTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java b/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java index 9dcf118def6..c4fc70a66fa 100644 --- a/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java +++ b/run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java @@ -36,9 +36,11 @@ public class AppTest { private static ByteArrayOutputStream bout; private static PrintStream out; + private static PrintStream originalOut; @BeforeClass public static void beforeClass() { + originalOut = System.out; bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -50,6 +52,7 @@ public static void beforeClass() { @AfterClass public static void afterClass() { + System.setOut(originalOut); stop(); }