From: rameshiyer27 Date: Mon, 26 May 2025 16:14:37 +0000 (+0100) Subject: Add stack_trace field in json logging X-Git-Tag: 8.2.0~5 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=dd757ac2242a3ebe817e19b57f5bdbcb29e70d10;p=policy%2Fclamp.git Add stack_trace field in json logging Issue-ID: POLICY-5375 Change-Id: I0417c07375912637bd02946e369837e4fd7a9c43 Signed-off-by: rameshiyer27 --- diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayout.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayout.java index d80ff0295..f3041c62d 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayout.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayout.java @@ -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 { map.put("timestamp", getTimestamp(event.getInstant())); map.put("severity", event.getLevel().toString()); map.put("message", event.getFormattedMessage()); - Map extraDatamap = new HashMap<>(); + Map 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); diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayoutTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayoutTest.java index d4f4c3ec0..72530491c 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayoutTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/config/LoggingConsoleLayoutTest.java @@ -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) map.get("extra_data"); + var extraData = (Map) 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")); + } } }