From a8618399833e9e2c2cfdd214058234a1395f8361 Mon Sep 17 00:00:00 2001 From: Kishore Reddy Dwaram Date: Fri, 25 Sep 2020 16:45:02 -0400 Subject: changes done to support logs format Issue-ID: PORTAL-1008 Signed-off-by: Kishore Reddy Dwaram Change-Id: I20f110c0fd48d7258f86f08501bad81244d3db20 --- .../core/logging/logic/EELFLoggerDelegate.java | 85 ++++++++++++++++------ 1 file changed, 64 insertions(+), 21 deletions(-) (limited to 'ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java') diff --git a/ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java b/ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java index adad5caa..45eae6d4 100644 --- a/ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java +++ b/ecomp-sdk/epsdk-logger/src/main/java/org/onap/portalsdk/core/logging/logic/EELFLoggerDelegate.java @@ -38,7 +38,6 @@ package org.onap.portalsdk.core.logging.logic; import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY; -import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID; import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; @@ -60,7 +59,6 @@ import org.onap.portalsdk.core.logging.format.AppMessagesEnum; import org.onap.portalsdk.core.logging.format.ErrorSeverityEnum; import org.slf4j.MDC; -import com.att.eelf.configuration.Configuration; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.att.eelf.configuration.SLF4jWrapper; @@ -115,17 +113,39 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { public void logWrite(EELFLogger logger, Level level, String msg, Throwable th, Object... arguments) { MDC.put(LoggerProperties.MDC_CLASS_NAME, className); setGlobalLoggingContext(); + String requestId = null; + if (MDC.get(MDC_KEY_REQUEST_ID) == null||MDC.get(MDC_KEY_REQUEST_ID).isEmpty()){ + requestId = UUID.randomUUID().toString(); + MDC.put(MDC_KEY_REQUEST_ID, requestId); + } + if ( logger == errorLogger) { - MDC.put(LoggerProperties.MDC_CLASS_NAME, className); - MDC.put("ErrorCategory", ErrorSeverityEnum.ERROR.name()); - MDC.put("ErrorCode", "900"); - MDC.put("ErrorDescription", "Exception"); + if(MDC.get(LoggerProperties.MDC_CLASS_NAME) == null) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + } + MDC.put(MDC_ALERT_SEVERITY, AlarmSeverityEnum.ERROR.name()); + if(MDC.get(LoggerProperties.ERROR_CODE) == null) { + MDC.put(LoggerProperties.ERROR_CODE, "900"); + } + if(MDC.get(LoggerProperties.ERROR_DESCRIPTION) == null) { + MDC.put(LoggerProperties.ERROR_DESCRIPTION, "Exception"); + } } - if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null||MDC.get(Configuration.MDC_KEY_REQUEST_ID).isEmpty()){ - String requestId = UUID.randomUUID().toString(); - MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId); + if(MDC.get(LoggerProperties.PARTNER_NAME) == null) { + MDC.put(LoggerProperties.PARTNER_NAME, "UNKNOWN"); + } + + if(MDC.get(LoggerProperties.STATUS_CODE) == null) { + MDC.put(LoggerProperties.STATUS_CODE, "COMPLETE"); + } + + if ( logger == auditLogger) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + this.calculateDateTimeDifference(MDC.get(LoggerProperties.AUDITLOG_BEGIN_TIMESTAMP), MDC.get(LoggerProperties.AUDITLOG_END_TIMESTAMP)); + MDC.put(LoggerProperties.CUSTOM_FIELD1, "EXIT"); + MDC.put(LoggerProperties.CUSTOM_FIELD2, "InvocationID=" + requestId); } if (logger.isErrorEnabled()) { @@ -135,12 +155,18 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { } if ( logger == errorLogger) { - MDC.remove(MDC_SERVICE_NAME); MDC.remove(LoggerProperties.MDC_CLASS_NAME); - MDC.remove("ErrorCode"); - MDC.remove("ErrorDescription"); - MDC.remove("ErrorCategory"); + MDC.remove(LoggerProperties.ERROR_CATAGORY); + MDC.remove(LoggerProperties.ERROR_CODE); + MDC.remove(LoggerProperties.ERROR_DESCRIPTION); } + + if(logger == auditLogger) { + MDC.remove(LoggerProperties.MDC_CLASS_NAME); + MDC.remove(LoggerProperties.CUSTOM_FIELD1); + MDC.remove(LoggerProperties.CUSTOM_FIELD2); + } + MDC.remove(MDC_KEY_REQUEST_ID); } @@ -322,9 +348,8 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { /** * Initializes the logger context. */ - public void init(String appName) { + public void init() { setGlobalLoggingContext(); - MDC.put(MDC_SERVICE_NAME, appName); } public static String getCurrentDateTimeUTC() { @@ -345,9 +370,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity(); ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity(); - MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name()); - MDC.put("ErrorCode", epMessageEnum.getErrorCode()); - MDC.put("ErrorDescription", epMessageEnum.getErrorDescription()); + MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.severity()); + MDC.put(LoggerProperties.ERROR_CODE, epMessageEnum.getErrorCode()); + MDC.put(LoggerProperties.ERROR_DESCRIPTION, epMessageEnum.getErrorDescription()); String resolution = this.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param); @@ -361,8 +386,8 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { } catch (Exception e) { errorLogger.error("logEcompError failed", e); } finally { - MDC.remove("ErrorCode"); - MDC.remove("ErrorDescription"); + MDC.remove(LoggerProperties.ERROR_CODE); + MDC.remove(LoggerProperties.ERROR_DESCRIPTION); MDC.remove(MDC_ALERT_SEVERITY); } } @@ -395,7 +420,7 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { try { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); - MDC.put(MDC_INSTANCE_UUID, LoggerProperties.getProperty(LoggerProperties.INSTANCE_UUID)); + MDC.put(MDC_SERVICE_NAME, LoggerProperties.getPropertyValue(LoggerProperties.APP_NAME)); } catch (Exception e) { errorLogger.error("setGlobalLoggingContext failed", e); } @@ -484,4 +509,22 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { } return ""; } + + /** + * + * @param beginDateTime + * @param endDateTime + */ + private void calculateDateTimeDifference(String beginDateTime, String endDateTime) { + if (beginDateTime != null && endDateTime != null) { + try { + Date beginDate = ecompLogDateFormat.parse(beginDateTime); + Date endDate = ecompLogDateFormat.parse(endDateTime); + String timeDifference = String.format("%d", endDate.getTime() - beginDate.getTime()); + MDC.put(LoggerProperties.MDC_TIMER, timeDifference); + } catch (Exception e) { + errorLogger.error("calculateDateTimeDifference failed", e); + } + } + } } -- cgit 1.2.3-korg