summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>2020-02-24 18:33:27 +0000
committerSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>2020-02-24 18:33:27 +0000
commit7b5d3d08876124b713b12a0b5983752af4b5921b (patch)
tree42856e73ba7cfbba43b2560bce39647fe8c534ec
parent3a325d8192addf27423494d8ed8b621dedcf7888 (diff)
minor logging updates
create error logger, integrate with record node and improve slf4jrecorder Issue-ID: CCSDK-2115 Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com> Change-Id: I857ce0fac070278a195204195eaae0c53d2d0d52
-rwxr-xr-xartifacts/pom.xml5
-rw-r--r--sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/ErrorLogger.java100
-rw-r--r--sli/common/src/test/java/org/onap/ccsdk/sli/core/sli/ErrorLoggerTest.java56
-rwxr-xr-xsli/installer/pom.xml5
-rw-r--r--sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/recording/Slf4jRecorder.java54
5 files changed, 198 insertions, 22 deletions
diff --git a/artifacts/pom.xml b/artifacts/pom.xml
index f6c883c1..a5ed3799 100755
--- a/artifacts/pom.xml
+++ b/artifacts/pom.xml
@@ -146,11 +146,6 @@
<artifactId>utils-installer</artifactId>
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.onap.ccsdk.sli.core</groupId>
- <artifactId>sli-recording</artifactId>
- <version>${project.version}</version>
- </dependency>
</dependencies>
</dependencyManagement>
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 00000000..47f6526e
--- /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 00000000..d95ff307
--- /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 38905cae..dcbe6b8d 100755
--- a/sli/installer/pom.xml
+++ b/sli/installer/pom.xml
@@ -47,11 +47,6 @@
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.onap.ccsdk.sli.core</groupId>
- <artifactId>sli-recording</artifactId>
- <version>${project.version}</version>
- </dependency>
</dependencies>
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 e3f4f1b7..4f67c5d9 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<String, String> 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);