From 7b5d3d08876124b713b12a0b5983752af4b5921b Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Mon, 24 Feb 2020 18:33:27 +0000 Subject: minor logging updates create error logger, integrate with record node and improve slf4jrecorder Issue-ID: CCSDK-2115 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I857ce0fac070278a195204195eaae0c53d2d0d52 --- artifacts/pom.xml | 5 -- .../org/onap/ccsdk/sli/core/sli/ErrorLogger.java | 100 +++++++++++++++++++++ .../onap/ccsdk/sli/core/sli/ErrorLoggerTest.java | 56 ++++++++++++ sli/installer/pom.xml | 5 -- .../sli/core/sli/recording/Slf4jRecorder.java | 54 ++++++++--- 5 files changed, 198 insertions(+), 22 deletions(-) create mode 100644 sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/ErrorLogger.java create mode 100644 sli/common/src/test/java/org/onap/ccsdk/sli/core/sli/ErrorLoggerTest.java diff --git a/artifacts/pom.xml b/artifacts/pom.xml index f6c883c13..a5ed37993 100755 --- a/artifacts/pom.xml +++ b/artifacts/pom.xml @@ -146,11 +146,6 @@ utils-installer ${project.version} - - org.onap.ccsdk.sli.core - sli-recording - ${project.version} - diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/ErrorLogger.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/ErrorLogger.java new file mode 100644 index 000000000..47f6526e9 --- /dev/null +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/ErrorLogger.java @@ -0,0 +1,100 @@ +package org.onap.ccsdk.sli.core.sli; + +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +public class ErrorLogger { + public static final int ERROR_CODE_100 = 100; + public static final int ERROR_CODE_200 = 200; + public static final int ERROR_CODE_300 = 300; + public static final int ERROR_CODE_400 = 400; + public static final int ERROR_CODE_500 = 500; + public static final int ERROR_CODE_900 = 900; + + private static final String DEFAULT_100_DESCRIPTION = "Permission Error"; + private static final String DEFAULT_200_DESCRIPTION = "Availability Error or Timeout"; + private static final String DEFAULT_300_DESCRIPTION = "Data Error"; + private static final String DEFAULT_400_DESCRIPTION = "Schema Error"; + private static final String DEFAULT_500_DESCRIPTION = "Business Process Error"; + private static final String DEFAULT_900_DESCRIPTION = "Unknown Error"; + private Logger log; + + public ErrorLogger() { + this.log = LoggerFactory.getLogger(ErrorLogger.class); + } + + public ErrorLogger(Logger log) { + this.log = log; + } + + public void logError(String message, int errorCode) { + createLogEntry(message, errorCode, null, null); + } + + public void logError(String message, int errorCode, Exception e) { + createLogEntry(message, errorCode, null, e); + } + + public void logError(String message, int errorCode, String description) { + createLogEntry(message, errorCode, description, null); + } + + public void logError(String message, int errorCode, String description, Exception e) { + createLogEntry(message, errorCode, description, e); + } + + public void createLogEntry(String message, int errorCode, String description, Exception e) { + //If the error code isn't valid default it to unknown error code + if(!isValidCode(errorCode)) { + errorCode = 900; + } + + MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(errorCode)); + + if (description == null || description.isEmpty()) { + description = getDefaultDescription(errorCode); + } + + MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, description); + if (e != null) { + log.error(message, e); + } else { + log.error(message); + } + clearKeys(); + } + + public boolean isValidCode(int errorCode) { + if (errorCode == ERROR_CODE_100 || errorCode == ERROR_CODE_200 || errorCode == ERROR_CODE_300 || errorCode == ERROR_CODE_400 || errorCode == ERROR_CODE_500 + || errorCode == ERROR_CODE_900) { + return true; + } + return false; + } + + public String getDefaultDescription(int errorCode) { + if (errorCode == ERROR_CODE_100) { + return DEFAULT_100_DESCRIPTION; + } + if (errorCode == ERROR_CODE_200) { + return DEFAULT_200_DESCRIPTION; + } + if (errorCode == ERROR_CODE_300) { + return DEFAULT_300_DESCRIPTION; + } + if (errorCode == ERROR_CODE_400) { + return DEFAULT_400_DESCRIPTION; + } + if (errorCode == ERROR_CODE_500) { + return DEFAULT_500_DESCRIPTION; + } + return DEFAULT_900_DESCRIPTION; + } + + public void clearKeys() { + MDC.remove(ONAPLogConstants.MDCs.ERROR_CODE); + MDC.remove(ONAPLogConstants.MDCs.ERROR_DESC); + } +} diff --git a/sli/common/src/test/java/org/onap/ccsdk/sli/core/sli/ErrorLoggerTest.java b/sli/common/src/test/java/org/onap/ccsdk/sli/core/sli/ErrorLoggerTest.java new file mode 100644 index 000000000..d95ff3075 --- /dev/null +++ b/sli/common/src/test/java/org/onap/ccsdk/sli/core/sli/ErrorLoggerTest.java @@ -0,0 +1,56 @@ +package org.onap.ccsdk.sli.core.sli; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ErrorLoggerTest { + private Logger log = LoggerFactory.getLogger(ErrorLoggerTest.class); + + @Test + public void testOverloads() throws Exception { + ErrorLogger e = new ErrorLogger(); + Exception exc = new Exception(); + e.logError("failure", 200); + e.logError("failure", 200, exc); + e.logError("failure", 200, "Timeout during HTTP operation"); + e.logError("failure", 200, "Timeout during HTTP operation", exc); + } + + @Test + public void testInvalidErrorCode() throws Exception { + ErrorLogger e = new ErrorLogger(); + e.logError("failure", 0); + } + + @Test + public void testDescriptionMapping() throws Exception { + ErrorLogger e = new ErrorLogger(); + e.logError("failure", 100); + e.logError("failure", 200); + e.logError("failure", 300); + e.logError("failure", 400); + e.logError("failure", 500); + e.logError("failure", 900); + } + + @Test + public void testIsValidCode() throws Exception { + ErrorLogger e = new ErrorLogger(log); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_100)); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_200)); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_300)); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_400)); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_500)); + assertTrue(e.isValidCode(ErrorLogger.ERROR_CODE_900)); + + assertFalse(e.isValidCode(0)); + assertFalse(e.isValidCode(204)); + assertFalse(e.isValidCode(404)); + assertFalse(e.isValidCode(501)); + } + +} diff --git a/sli/installer/pom.xml b/sli/installer/pom.xml index 38905caee..dcbe6b8d2 100755 --- a/sli/installer/pom.xml +++ b/sli/installer/pom.xml @@ -47,11 +47,6 @@ ${project.version} - - org.onap.ccsdk.sli.core - sli-recording - ${project.version} - diff --git a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/recording/Slf4jRecorder.java b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/recording/Slf4jRecorder.java index e3f4f1b7e..4f67c5d9a 100644 --- a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/recording/Slf4jRecorder.java +++ b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/recording/Slf4jRecorder.java @@ -28,14 +28,16 @@ import java.util.Map; import java.util.TimeZone; import org.onap.ccsdk.sli.core.sli.ConfigurationException; +import org.onap.ccsdk.sli.core.sli.ErrorLogger; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Slf4jRecorder implements SvcLogicRecorder { - - + protected DateFormat dateFmt; + protected static final String messageLogName = "message-log"; + public enum Level { ERROR, WARN, @@ -44,13 +46,17 @@ public class Slf4jRecorder implements SvcLogicRecorder { TRACE } + protected Logger defaultLogger = LoggerFactory.getLogger(Slf4jRecorder.class); + protected Logger messageLogger = LoggerFactory.getLogger(messageLogName); + + public Slf4jRecorder() { + TimeZone tz = TimeZone.getTimeZone("UTC"); + dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'"); + dateFmt.setTimeZone(tz); + } + @Override public void record(Map parmMap) throws SvcLogicException { - String loggerName = parmMap.get("logger"); - if (loggerName == null) { - loggerName = "Log4jRecorder"; - } - String lvl = parmMap.get("level"); if (lvl == null) { lvl = "INFO"; @@ -98,12 +104,20 @@ public class Slf4jRecorder implements SvcLogicRecorder { throw new ConfigurationException("No record/fields passed in record node"); } - Logger logger = LoggerFactory.getLogger(loggerName); + String loggerName = parmMap.get("logger"); + Logger logger = null; + if (loggerName == null) { + logger = defaultLogger; + }else { + if(loggerName.equals(messageLogName)){ + logger = messageLogger; + }else { + logger = LoggerFactory.getLogger(loggerName); + } + } Date now = new Date(); - TimeZone tz = TimeZone.getTimeZone("UTC"); - DateFormat dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'"); - dateFmt.setTimeZone(tz); + if (record.indexOf("__TIMESTAMP__") != -1) { record = record.replaceFirst("__TIMESTAMP__", dateFmt.format(now)); @@ -111,7 +125,23 @@ public class Slf4jRecorder implements SvcLogicRecorder { switch (level) { case ERROR: - logger.error(record); + String errorCode = parmMap.get("errorCode"); + String errorDescription = parmMap.get("errorDescription"); + + if ((errorCode != null && !errorCode.isEmpty()) + || (errorDescription != null && !errorDescription.isEmpty())) { + ErrorLogger e = new ErrorLogger(logger); + + Integer integerCode = 0; + try { + integerCode = Integer.valueOf(errorCode); + } catch (NumberFormatException nfe) { + // do nothing + } + e.createLogEntry(record, integerCode, errorDescription, null); + } else { + logger.error(record); + } break; case WARN: logger.warn(record); -- cgit 1.2.3-korg