Platform hardening for common bundle
[appc.git] / appc-common / src / main / java / org / onap / appc / logging / LoggingUtils.java
index 1e4156e..5e0b2fa 100644 (file)
@@ -30,10 +30,8 @@ import com.att.eelf.configuration.EELFManager;
 import com.att.eelf.i18n.EELFResolvableErrorEnum;
 import com.att.eelf.i18n.EELFResourceManager;
 import org.slf4j.MDC;
-
 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
-
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.time.Instant;
@@ -46,76 +44,76 @@ import java.util.TimeZone;
  */
 public class LoggingUtils {
 
-    private final static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
-    private final static EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
-    private final static EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
+    private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
+    private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
+    private static final EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
 
     private LoggingUtils() {
         throw new IllegalAccessError("LoggingUtils");
     }
 
-    public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity,
-                                       String targetServiceName, String additionalMessage, String className) {
-        logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage, className);
+    public static void logErrorMessage(String errorCode, String errorDescription,
+            String targetEntity, String targetServiceName, String additionalMessage,
+            String className) {
+        logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
+                className);
     }
 
-    public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage,
-                                       String className) {
+    public static void logErrorMessage(String targetEntity, String targetServiceName,
+            String additionalMessage, String className) {
         logError("", "", targetEntity, targetServiceName, additionalMessage, className);
     }
 
-    public static void logErrorMessage(String targetServiceName, String additionalMessage, String className) {
-        logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName,
-                additionalMessage, className);
+    public static void logErrorMessage(String targetServiceName, String additionalMessage,
+            String className) {
+        logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
+                className);
     }
 
     private static void logError(String errorCode, String errorDescription, String targetEntity,
-                                 String targetServiceName, String additionalMessage, String className) {
-        populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName, className);
+            String targetServiceName, String additionalMessage, String className) {
+        populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
+                className);
         errorLogger.error(additionalMessage == null ? "" : additionalMessage);
         cleanErrorLogContext();
     }
 
     public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
-                                       String responseDescription, String className) {
+            String responseDescription, String className) {
         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
-        auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG,
-                MDC.get(MDC_SERVICE_NAME),
+        auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME),
                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
-                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
-                MDC.get(MDC_KEY_REQUEST_ID),
+                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
                 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
                 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
                 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
         cleanAuditErrorContext();
     }
 
-    public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription,
-                                 String className,EELFResolvableErrorEnum resourceId, String... arguments) {
+    public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code,
+            String responseDescription, String className, EELFResolvableErrorEnum resourceId,
+            String... arguments) {
         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
-        auditLogger.info(resourceId,arguments);
+        auditLogger.info(resourceId, arguments);
         cleanAuditErrorContext();
     }
 
-    public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription,
-                                 String className,EELFResolvableErrorEnum resourceId, String... arguments) {
+    public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code,
+            String responseDescription, String className, EELFResolvableErrorEnum resourceId,
+            String... arguments) {
         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
-        auditLogger.warn(resourceId,arguments);
+        auditLogger.warn(resourceId, arguments);
         cleanAuditErrorContext();
     }
 
-
-
-    public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity,
-                                         String targetServiceName, String statusCode, String responseCode,
-                                         String responseDescription, String className) {
-        populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode,
-                responseCode, responseDescription, className);
-        metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG,
-                MDC.get(MDC_SERVICE_NAME),
+    public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp,
+            String targetEntity, String targetServiceName, String statusCode, String responseCode,
+            String responseDescription, String className) {
+        populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName,
+                statusCode, responseCode, responseDescription, className);
+        metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME),
                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
-                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
-                MDC.get(MDC_KEY_REQUEST_ID),
+                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
                 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
                 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
                 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
@@ -123,15 +121,15 @@ public class LoggingUtils {
         cleanMetricContext();
     }
 
-    private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code,
-                                                String responseDescription, String className) {
+    private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
+            String code, String responseDescription, String className) {
         populateTimeContext(beginTimeStamp, endTimeStamp);
         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
-        MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code) ?
-                LoggingConstants.StatusCodes.COMPLETE :
-                LoggingConstants.StatusCodes.ERROR);
-        MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
-        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
+        MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code)
+                ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR);
+        MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
+                responseDescription != null ? responseDescription : "");
+        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
     }
 
     private static void cleanAuditErrorContext() {
@@ -142,11 +140,11 @@ public class LoggingUtils {
         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
     }
 
-    private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity,
-                                                String targetServiceName, String className) {
+    private static void populateErrorLogContext(String errorCode, String errorDescription,
+            String targetEntity, String targetServiceName, String className) {
         populateErrorContext(errorCode, errorDescription);
-        populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
-        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
+        populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
+        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
     }
 
     private static void cleanErrorLogContext() {
@@ -155,13 +153,13 @@ public class LoggingUtils {
         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
     }
 
-    private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity,
-                                                 String targetServiceName, String statusCode, String responseCode,
-                                                 String responseDescription, String className) {
+    private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
+            String targetEntity, String targetServiceName, String statusCode, String responseCode,
+            String responseDescription, String className) {
         populateTimeContext(beginTimeStamp, endTimeStamp);
         populateTargetContext(targetEntity, targetServiceName);
         populateResponseContext(statusCode, responseCode, responseDescription);
-        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
+        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
     }
 
     private static void cleanMetricContext() {
@@ -172,8 +170,9 @@ public class LoggingUtils {
     }
 
     private static void populateTargetContext(String targetEntity, String targetServiceName) {
-        MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
-        MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
+        MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity != null ? targetEntity : "");
+        MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME,
+                targetServiceName != null ? targetServiceName : "");
     }
 
     private static void cleanTargetContext() {
@@ -187,7 +186,7 @@ public class LoggingUtils {
         String elapsedTime = "";
 
         if (beginTimeStamp != null && endTimeStamp != null) {
-            elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp,  endTimeStamp));
+            elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
             beginTime = generateTimestampStr(beginTimeStamp);
             endTime = generateTimestampStr(endTimeStamp);
         }
@@ -210,10 +209,12 @@ public class LoggingUtils {
         MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
     }
 
-    private static void populateResponseContext(String statusCode, String responseCode, String responseDescription) {
-        MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode!=null?statusCode:"");
+    private static void populateResponseContext(String statusCode, String responseCode,
+            String responseDescription) {
+        MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
-        MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
+        MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
+                responseDescription != null ? responseDescription : "");
     }
 
     private static void cleanResponseContext() {