/*- * ============LICENSE_START======================================================= * OPENECOMP - MSO * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.mso.logger; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.slf4j.MDC; import org.openecomp.mso.entity.MsoRequest; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; 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 ()); if (serviceNamep != null) { return serviceNamep; } else if (serName != null && !isMethodName) { return serName; } MDC.put (MsoLogger.SERVICE_NAME_IS_METHOD_NAME, Boolean.TRUE.toString ()); int limit; StackTraceElement[] classArr = new Exception ().getStackTrace (); if (classArr.length >=6) { limit = 7; } else { 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 (); } } return classArr[0].getMethodName (); } // 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 () { // Avoid creation during build and tests if (System.getProperty ("jboss.server.name") == null) { return "Test UUID as JBoss not found"; } 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 ()) { // read the content of the file 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 (); } 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 (); } } catch (IOException e) { LOGGER.log (Level.SEVERE, "Error trying to read UUID file", e); } finally { try { if (in != null) { in.close (); } if (bw != null) { bw.close (); } } catch (IOException ex) { LOGGER.log (Level.SEVERE, "Error trying to close UUID file", ex); } } return uuid; } /** * Set the requestId and serviceInstanceId * @param reqId The requestId * @param svcId The serviceInstanceId */ public static void setLogContext (String reqId, String svcId) { if (null != reqId) { MDC.put (REQUEST_ID, reqId); } if (null != 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 */ public static void setLoggerParameters (String remoteIpp, String userp) { if (null != remoteIpp) { MDC.put (REMOTE_HOST, remoteIpp); } if (null != userp) { MDC.put (USER, userp); } } /** * Set the serviceName * @param serviceNamep The service name */ public static void setServiceName (String serviceNamep) { if (null != serviceNamep) { 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); } /** * Reset the serviceName */ public static void resetServiceName () { MDC.remove (SERVICE_NAME); } /** * Set the requestId and serviceInstanceId based on the mso request * @param msoRequest The mso request */ 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); } } private String normalize (String input) { if (input == null) { return null; } String result = input.replace ('|', '!'); result = result.replace ("\n", " - "); return result; } private String getNormalizedStackTrace (Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); return sw.toString().replace ('|', '!').replace ("\n", " - "); } 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; } 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; } 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; } 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; } else { exceptionArg = MessageEnum.GENERAL_EXCEPTION_ARG; defaultException = MessageEnum.GENERAL_EXCEPTION; defaultWarning = MessageEnum.GENERAL_WARNING; defaultAudit = MessageEnum.AUDIT_EXEC; defaultMetrics = MessageEnum.GENERAL_METRICS; } } }