From 4dced3e6eb281dea20627146eaa4f172b5982402 Mon Sep 17 00:00:00 2001 From: "Determe, Sebastien (sd378r)" Date: Tue, 14 Feb 2017 04:50:44 -0800 Subject: Modification of the MsoLogger methods Method Signature changed to let new opensource based project define their own Log Enum Change-Id: I3e010695c6567fbbf8ea0efbea2c89c50938680d Signed-off-by: Determe, Sebastien (sd378r) --- .../java/org/openecomp/mso/logger/MsoLogger.java | 1025 +++++++++++--------- 1 file changed, 589 insertions(+), 436 deletions(-) (limited to 'common/src') diff --git a/common/src/main/java/org/openecomp/mso/logger/MsoLogger.java b/common/src/main/java/org/openecomp/mso/logger/MsoLogger.java index c530227..110cb40 100644 --- a/common/src/main/java/org/openecomp/mso/logger/MsoLogger.java +++ b/common/src/main/java/org/openecomp/mso/logger/MsoLogger.java @@ -39,14 +39,16 @@ import org.slf4j.MDC; import org.openecomp.mso.entity.MsoRequest; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import com.att.eelf.i18n.EELFResolvableErrorEnum; + import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** - * Simple wrapper around the EELF Logger class for MSO usage. - * This class supports all of the normal logging functions (debug, info, etc.), - * prepending a string of format "[||) value to it - String serName = MDC.get (MsoLogger.SERVICE_NAME); - - // Check if service name was already set as the method name by a previous call to this method. - String isMethodNameStr = MDC.get (MsoLogger.SERVICE_NAME_IS_METHOD_NAME); - boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals (Boolean.TRUE.toString ()); + // There's 3 ways to set the serviceName. The first method has the most + // priority to set the value. + // a) If the serviceName is set within the log method, this value will + // be used first + // b) If serviceName is not set within the log method, the value defined + // in the MDC will be used + // c) If nothing is set specifically, then MsoLogger will assign a + // default(MSO.) value to it + String serName = MDC.get(MsoLogger.SERVICE_NAME); + + // Check if service name was already set as the method name by a + // previous call to this method. + String isMethodNameStr = MDC.get(MsoLogger.SERVICE_NAME_IS_METHOD_NAME); + boolean isMethodName = isMethodNameStr != null && isMethodNameStr.equals(Boolean.TRUE.toString()); if (serviceNamep != null) { return serviceNamep; } else if (serName != null && !isMethodName) { return serName; } - MDC.put (MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString ()); + MDC.put(MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString()); int limit; - StackTraceElement[] classArr = new Exception ().getStackTrace (); - if (classArr.length >=6) { - limit = 7; + StackTraceElement[] classArr = new Exception().getStackTrace(); + if (classArr.length >= 6) { + limit = 7; } else { - limit = classArr.length; + limit = classArr.length; } - for (int i = 1; i < limit; i++) { - String className = classArr[i].getClassName (); - if (!className.equals (this.getClass ().getName ())) { - return classArr[i].getMethodName (); + for (int i = 1; i < limit; i++) { + String className = classArr[i].getClassName(); + if (!className.equals(this.getClass().getName())) { + return classArr[i].getMethodName(); } } - return classArr[0].getMethodName (); + return classArr[0].getMethodName(); } - // Based on the discussion with Adrian, instanceUUID is used to identifiy the mso instance, + // Based on the discussion with Adrian, instanceUUID is used to identifiy + // the mso instance, // it is generated during mso instance initialization period - // The same mso instnace will use the same instanceUUID value, even after restart - private static String getInstanceUUID () { + // The same mso instnace will use the same instanceUUID value, even after + // restart + private static String getInstanceUUID() { // Avoid creation during build and tests - if (System.getProperty ("jboss.server.name") == null) { + if (System.getProperty("jboss.server.name") == null) { return "Test UUID as JBoss not found"; } - File configFile = new File (CONFIG_FILE); + File configFile = new File(CONFIG_FILE); String uuid = ""; BufferedReader in = null; BufferedWriter bw = null; try { // Verify whether instanceUUID file exist, - // If yes, read the content; if not, generate the instanceUUID and write to the file - if (configFile.exists ()) { + // If yes, read the content; if not, generate the instanceUUID and + // write to the file + if (configFile.exists()) { // read the content of the file - in = new BufferedReader (new FileReader (CONFIG_FILE)); - if ((uuid = in.readLine ()) == null) { + in = new BufferedReader(new FileReader(CONFIG_FILE)); + if ((uuid = in.readLine()) == null) { // the file is empty, regenerate the file - uuid = UUID.randomUUID ().toString (); - FileWriter fw = new FileWriter (configFile.getAbsoluteFile ()); - bw = new BufferedWriter (fw); - bw.write (uuid); - bw.close (); + uuid = UUID.randomUUID().toString(); + FileWriter fw = new FileWriter(configFile.getAbsoluteFile()); + bw = new BufferedWriter(fw); + bw.write(uuid); + bw.close(); } - in.close (); + in.close(); } else { - // file doesn't exist yet -> create the file and generate the instanceUUID - uuid = UUID.randomUUID ().toString (); - configFile.getParentFile ().mkdirs (); - configFile.createNewFile (); - FileWriter fw = new FileWriter (configFile.getAbsoluteFile ()); - bw = new BufferedWriter (fw); - bw.write (uuid); - bw.close (); + // file doesn't exist yet -> create the file and generate the + // instanceUUID + uuid = UUID.randomUUID().toString(); + configFile.getParentFile().mkdirs(); + configFile.createNewFile(); + FileWriter fw = new FileWriter(configFile.getAbsoluteFile()); + bw = new BufferedWriter(fw); + bw.write(uuid); + bw.close(); } } catch (IOException e) { - LOGGER.log (Level.SEVERE, "Error trying to read UUID file", e); + LOGGER.log(Level.SEVERE, "Error trying to read UUID file", e); } finally { try { if (in != null) { - in.close (); + in.close(); } if (bw != null) { - bw.close (); + bw.close(); } } catch (IOException ex) { - LOGGER.log (Level.SEVERE, "Error trying to close UUID file", ex); + LOGGER.log(Level.SEVERE, "Error trying to close UUID file", ex); } } return uuid; @@ -853,121 +996,131 @@ public enum Catalog {APIH, BPEL, RA, ASDC, GENERAL}; /** * Set the requestId and serviceInstanceId - * @param reqId The requestId - * @param svcId The serviceInstanceId + * + * @param reqId + * The requestId + * @param svcId + * The serviceInstanceId */ - public static void setLogContext (String reqId, String svcId) { + public static void setLogContext(String reqId, String svcId) { if (null != reqId) { - MDC.put (REQUEST_ID, reqId); + MDC.put(REQUEST_ID, reqId); } if (null != svcId) { - MDC.put (SERVICE_INSTANCE_ID, svcId); + MDC.put(SERVICE_INSTANCE_ID, svcId); } - } + } /** * Set the remoteIp and the basic HTTP Authentication user - * @param remoteIpp The remote ip address - * @param userp The basic http authencitation user + * + * @param remoteIpp + * The remote ip address + * @param userp + * The basic http authencitation user */ - public static void setLoggerParameters (String remoteIpp, String userp) { + public static void setLoggerParameters(String remoteIpp, String userp) { if (null != remoteIpp) { - MDC.put (REMOTE_HOST, remoteIpp); + MDC.put(REMOTE_HOST, remoteIpp); } if (null != userp) { - MDC.put (USER, userp); + MDC.put(USER, userp); } - } + } /** * Set the serviceName - * @param serviceNamep The service name + * + * @param serviceNamep + * The service name */ - public static void setServiceName (String serviceNamep) { + public static void setServiceName(String serviceNamep) { if (null != serviceNamep) { - MDC.put (SERVICE_NAME, serviceNamep); - MDC.remove (SERVICE_NAME_IS_METHOD_NAME); + MDC.put(SERVICE_NAME, serviceNamep); + MDC.remove(SERVICE_NAME_IS_METHOD_NAME); } } /** * Get the serviceName + * * @return The service name */ - public static String getServiceName () { - return MDC.get (SERVICE_NAME); + public static String getServiceName() { + return MDC.get(SERVICE_NAME); } /** * Reset the serviceName */ - public static void resetServiceName () { - MDC.remove (SERVICE_NAME); - } + public static void resetServiceName() { + MDC.remove(SERVICE_NAME); + } /** * Set the requestId and serviceInstanceId based on the mso request - * @param msoRequest The mso request + * + * @param msoRequest + * The mso request */ - public static void setLogContext (MsoRequest msoRequest) { + public static void setLogContext(MsoRequest msoRequest) { if (msoRequest != null) { - MDC.put (REQUEST_ID, msoRequest.getRequestId()); - MDC.put (SERVICE_INSTANCE_ID, msoRequest.getServiceInstanceId()); - } - else { - MDC.put (REQUEST_ID, DUMMY_VALUE); - MDC.put (SERVICE_INSTANCE_ID, DUMMY_VALUE); + MDC.put(REQUEST_ID, msoRequest.getRequestId()); + MDC.put(SERVICE_INSTANCE_ID, msoRequest.getServiceInstanceId()); + } else { + MDC.put(REQUEST_ID, DUMMY_VALUE); + MDC.put(SERVICE_INSTANCE_ID, DUMMY_VALUE); } } - private String normalize (String input) { + private String normalize(String input) { if (input == null) { return null; } - String result = input.replace ('|', '!'); - result = result.replace ("\n", " - "); + String result = input.replace('|', '!'); + result = result.replace("\n", " - "); return result; } - private String getNormalizedStackTrace (Throwable t) { + private String getNormalizedStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); - return sw.toString().replace ('|', '!').replace ("\n", " - "); + return sw.toString().replace('|', '!').replace("\n", " - "); } - private void setDefaultLogCatalog (MsoLogger.Catalog cat) { + private void setDefaultLogCatalog(MsoLogger.Catalog cat) { if ("APIH".equals(cat.toString())) { - exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG; - defaultException = MessageEnum.APIH_GENERAL_EXCEPTION; - defaultWarning = MessageEnum.APIH_GENERAL_WARNING; - defaultAudit = MessageEnum.APIH_AUDIT_EXEC; - defaultMetrics = MessageEnum.APIH_GENERAL_METRICS; + exceptionArg = MessageEnum.APIH_GENERAL_EXCEPTION_ARG; + defaultException = MessageEnum.APIH_GENERAL_EXCEPTION; + defaultWarning = MessageEnum.APIH_GENERAL_WARNING; + defaultAudit = MessageEnum.APIH_AUDIT_EXEC; + defaultMetrics = MessageEnum.APIH_GENERAL_METRICS; } else if ("RA".equals(cat.toString())) { - exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG; - defaultException = MessageEnum.RA_GENERAL_EXCEPTION; - defaultWarning = MessageEnum.RA_GENERAL_WARNING; - defaultAudit = MessageEnum.RA_AUDIT_EXEC; - defaultMetrics = MessageEnum.RA_GENERAL_METRICS; + exceptionArg = MessageEnum.RA_GENERAL_EXCEPTION_ARG; + defaultException = MessageEnum.RA_GENERAL_EXCEPTION; + defaultWarning = MessageEnum.RA_GENERAL_WARNING; + defaultAudit = MessageEnum.RA_AUDIT_EXEC; + defaultMetrics = MessageEnum.RA_GENERAL_METRICS; } else if ("BPEL".equals(cat.toString())) { - exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG; - defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION; - defaultWarning = MessageEnum.BPMN_GENERAL_WARNING; - defaultAudit = MessageEnum.BPMN_AUDIT_EXEC; - defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS; + exceptionArg = MessageEnum.BPMN_GENERAL_EXCEPTION_ARG; + defaultException = MessageEnum.BPMN_GENERAL_EXCEPTION; + defaultWarning = MessageEnum.BPMN_GENERAL_WARNING; + defaultAudit = MessageEnum.BPMN_AUDIT_EXEC; + defaultMetrics = MessageEnum.BPMN_GENERAL_METRICS; } else if ("ASDC".equals(cat.toString())) { - exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG; - defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION; - defaultWarning = MessageEnum.ASDC_GENERAL_WARNING; - defaultAudit = MessageEnum.ASDC_AUDIT_EXEC; - defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS; + exceptionArg = MessageEnum.ASDC_GENERAL_EXCEPTION_ARG; + defaultException = MessageEnum.ASDC_GENERAL_EXCEPTION; + defaultWarning = MessageEnum.ASDC_GENERAL_WARNING; + defaultAudit = MessageEnum.ASDC_AUDIT_EXEC; + defaultMetrics = MessageEnum.ASDC_GENERAL_METRICS; } else { - exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG; - defaultException = MessageEnum.GENERAL_EXCEPTION; - defaultWarning = MessageEnum.GENERAL_WARNING; - defaultAudit = MessageEnum.AUDIT_EXEC; - defaultMetrics = MessageEnum.GENERAL_METRICS; + exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG; + defaultException = MessageEnum.GENERAL_EXCEPTION; + defaultWarning = MessageEnum.GENERAL_WARNING; + defaultAudit = MessageEnum.AUDIT_EXEC; + defaultMetrics = MessageEnum.GENERAL_METRICS; } } } -- cgit 1.2.3-korg