Add stack_trace field in json logging 61/140961/1
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>
Mon, 26 May 2025 16:14:37 +0000 (17:14 +0100)
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>
Tue, 27 May 2025 09:38:06 +0000 (10:38 +0100)
Issue-ID: POLICY-5375
Change-Id: I0417c07375912637bd02946e369837e4fd7a9c43
Signed-off-by: rameshiyer27 <ramesh.murugan.iyer@est.tech>
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayout.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayoutTest.java

index d80ff02..f3041c6 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2024 Nordix Foundation.
+ *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -94,12 +94,13 @@ public class LoggingConsoleLayout extends LayoutBase<ILoggingEvent> {
         map.put("timestamp", getTimestamp(event.getInstant()));
         map.put("severity", event.getLevel().toString());
         map.put("message", event.getFormattedMessage());
-        Map<String, String> extraDatamap = new HashMap<>();
+        Map<String, Object> extraDatamap = new HashMap<>();
         extraDatamap.put("logger", event.getLoggerName());
         extraDatamap.put("thread", event.getThreadName());
         var throwableProxy = event.getThrowableProxy();
         if (throwableProxy != null) {
-            extraDatamap.put("exception", ThrowableProxyUtil.asString(throwableProxy));
+            var m = Map.of("stack_trace", ThrowableProxyUtil.asString(throwableProxy));
+            extraDatamap.put("exception", m);
         }
         map.put("extra_data", extraDatamap);
         map.putAll(staticParameterMap);
index d4f4c3e..7253049 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2024 Nordix Foundation.
+ *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,11 +22,13 @@ package org.onap.policy.clamp.acm.runtime.config;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.classic.spi.IThrowableProxy;
 import ch.qos.logback.classic.spi.LoggerContextVO;
+import ch.qos.logback.classic.spi.ThrowableProxy;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.List;
@@ -137,6 +139,15 @@ class LoggingConsoleLayoutTest {
         layout.setTimestampFormatTimezoneId("");
         layout.start();
         testingResult(layout, event);
+
+        //event with exception
+        layout.setTimestampFormat(FORMAT);
+        layout.setTimestampFormatTimezoneId("UTC");
+        layout.setStaticParameters("service_id=policy-acm|application_id=policy-acm");
+        layout.start();
+        var throwable = new Throwable("PSQL Exception: Unable to modify object");
+        event.setThrowableProxy(new ThrowableProxy(throwable));
+        testingResult(layout, event);
     }
 
     private void testingResult(LoggingConsoleLayout layout, DummyEvent event) throws CoderException {
@@ -146,8 +157,11 @@ class LoggingConsoleLayoutTest {
         assertEquals(event.level.toString(), map.get("severity"));
         assertEquals(event.message, map.get("message"));
         @SuppressWarnings("unchecked")
-        var extraData = (Map<String, String>) map.get("extra_data");
+        var extraData = (Map<String, Object>) map.get("extra_data");
         assertEquals(event.loggerName, extraData.get("logger"));
         assertEquals(event.threadName, extraData.get("thread"));
+        if (event.getThrowableProxy() != null) {
+            assertTrue(((Map<?, ?>) extraData.get("exception")).containsKey("stack_trace"));
+        }
     }
 }