diff options
Diffstat (limited to 'mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java')
-rwxr-xr-x | mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java | 286 |
1 files changed, 248 insertions, 38 deletions
diff --git a/mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java b/mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java index d8b5256..2bd26ff 100755 --- a/mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java +++ b/mdbc-server/src/main/java/org/onap/music/logging/EELFLoggerDelegate.java @@ -7,9 +7,9 @@ * 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 - * + * + * 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. @@ -19,34 +19,42 @@ */ package org.onap.music.logging; +import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY; +import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID; +import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; - +import java.io.IOException; +import java.io.InputStream; import java.net.InetAddress; import java.text.MessageFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; - import javax.servlet.http.HttpServletRequest; - import org.slf4j.MDC; - import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.att.eelf.configuration.SLF4jWrapper; public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { + public static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger(); public static final EELFLogger applicationLogger = EELFManager.getInstance().getApplicationLogger(); public static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); public static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); public static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger(); + // DateTime Format according to the ECOMP Application Logging Guidelines. + private static final SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + private String className; - private static ConcurrentMap<String, EELFLoggerDelegate> classMap = new ConcurrentHashMap<>(); + private static ConcurrentMap<String, EELFLoggerDelegate> classMap = new ConcurrentHashMap<String, EELFLoggerDelegate>(); public EELFLoggerDelegate(final String className) { super(className); @@ -92,7 +100,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void trace(EELFLogger logger, String msg) { if (logger.isTraceEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.trace(msg); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -105,7 +115,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void trace(EELFLogger logger, String msg, Object... arguments) { if (logger.isTraceEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.trace(msg, arguments); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -118,7 +130,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void trace(EELFLogger logger, String msg, Throwable th) { if (logger.isTraceEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.trace(msg, th); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -130,7 +144,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void debug(EELFLogger logger, String msg) { if (logger.isDebugEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.debug(msg); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -143,7 +159,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void debug(EELFLogger logger, String msg, Object... arguments) { if (logger.isDebugEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.debug(msg, arguments); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -156,7 +174,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ public void debug(EELFLogger logger, String msg, Throwable th) { if (logger.isDebugEnabled()) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.debug(msg, th); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } } @@ -167,7 +187,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param msg */ public void info(EELFLogger logger, String msg) { - logger.info(className + " - "+msg); + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + logger.info(msg); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -178,7 +200,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param arguments */ public void info(EELFLogger logger, String msg, Object... arguments) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.info(msg, arguments); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -189,7 +213,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param th */ public void info(EELFLogger logger, String msg, Throwable th) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.info(msg, th); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -199,7 +225,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param msg */ public void warn(EELFLogger logger, String msg) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.warn(msg); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -210,7 +238,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param arguments */ public void warn(EELFLogger logger, String msg, Object... arguments) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.warn(msg, arguments); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -221,7 +251,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param th */ public void warn(EELFLogger logger, String msg, Throwable th) { + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); logger.warn(msg, th); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -231,7 +263,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param msg */ public void error(EELFLogger logger, String msg) { - logger.error(className+ " - " + msg); + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + logger.error(msg); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -242,7 +276,9 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param arguments */ public void error(EELFLogger logger, String msg, Object... arguments) { - logger.error(msg, arguments); + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + logger.warn(msg, arguments); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } /** @@ -253,19 +289,11 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param th */ public void error(EELFLogger logger, String msg, Throwable th) { - logger.error(msg, th); + MDC.put(LoggerProperties.MDC_CLASS_NAME, className); + logger.warn(msg, th); + MDC.remove(LoggerProperties.MDC_CLASS_NAME); } - /** - * Logs a message with the associated alarm severity at error level. - * - * @param logger - * @param msg - * @param severtiy - */ - public void error(EELFLogger logger, String msg, Object /*AlarmSeverityEnum*/ severtiy) { - logger.error(msg); - } /** * Initializes the logger context. @@ -277,11 +305,30 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { info(applicationLogger, msg); error(errorLogger, msg); debug(debugLogger, msg); - info(auditLogger, msg); + // Audit and metrics logger must be told start AND stop times + final String currentDateTime = getCurrentDateTimeUTC(); + // Set the MDC with audit properties + MDC.put(LoggerProperties.AUDITLOG_BEGIN_TIMESTAMP, currentDateTime); + MDC.put(LoggerProperties.AUDITLOG_END_TIMESTAMP, currentDateTime); + info(auditLogger, msg); + MDC.remove(LoggerProperties.AUDITLOG_BEGIN_TIMESTAMP); + MDC.remove(LoggerProperties.AUDITLOG_END_TIMESTAMP); + // Set the MDC with metrics properties + MDC.put(LoggerProperties.METRICSLOG_BEGIN_TIMESTAMP, currentDateTime); + MDC.put(LoggerProperties.METRICSLOG_END_TIMESTAMP, currentDateTime); info(metricsLogger, msg); + MDC.remove(LoggerProperties.METRICSLOG_BEGIN_TIMESTAMP); + MDC.remove(LoggerProperties.METRICSLOG_END_TIMESTAMP); + } + + + public static String getCurrentDateTimeUTC() { + String currentDateTime = ecompLogDateFormat.format(new Date()); + return currentDateTime; } + /** * Builds a message using a template string and the arguments. * @@ -289,7 +336,6 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param args * @return */ - @SuppressWarnings("unused") private String formatMessage(String message, Object... args) { StringBuilder sbFormattedMessage = new StringBuilder(); if (args != null && args.length > 0 && message != null && message != "") { @@ -307,9 +353,11 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { */ private void setGlobalLoggingContext() { MDC.put(MDC_SERVICE_INSTANCE_ID, ""); + MDC.put(MDC_ALERT_SEVERITY, AlarmSeverityEnum.INFORMATIONAL.toString()); try { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); + MDC.put(MDC_INSTANCE_UUID, LoggerProperties.getProperty(LoggerProperties.INSTANCE_UUID)); } catch (Exception e) { errorLogger.error("setGlobalLoggingContext failed", e); } @@ -335,23 +383,185 @@ public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger { * @param req * @param appName */ - public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req, String appName) { + public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req, String appName,String reqId,String loginId) {// Load the default fields // Load the default fields - setGlobalLoggingContext(); + setGlobalLoggingContext(); + + // Load the request based fields + if (req != null) { + // Load the Request into MDC context. + + MDC.put(MDC_KEY_REQUEST_ID, reqId); + + // Load user agent into MDC context, if available. + String accessingClient = req.getHeader(LoggerProperties.USERAGENT_NAME); + if (accessingClient != null && !"".equals(accessingClient) && (accessingClient.contains("Mozilla") + || accessingClient.contains("Chrome") || accessingClient.contains("Safari"))) { + accessingClient = appName + "_FE"; + } + MDC.put(LoggerProperties.PARTNER_NAME, accessingClient); + + // Protocol, Rest URL & Rest Path + MDC.put(LoggerProperties.FULL_URL, LoggerProperties.UNKNOWN); + MDC.put(LoggerProperties.PROTOCOL, LoggerProperties.HTTP); + String restURL = getFullURL(req); + if (restURL != null && restURL != "") { + MDC.put(LoggerProperties.FULL_URL, restURL); + if (restURL.toLowerCase().contains("https")) { + MDC.put(LoggerProperties.PROTOCOL, LoggerProperties.HTTPS); + } + } + + // Rest Path + MDC.put(MDC_SERVICE_NAME, req.getServletPath()); + + // Client IPAddress i.e. IPAddress of the remote host who is making + // this request. + String clientIPAddress = req.getHeader("X-FORWARDED-FOR"); + if (clientIPAddress == null) { + clientIPAddress = req.getRemoteAddr(); + } + MDC.put(LoggerProperties.CLIENT_IP_ADDRESS, clientIPAddress); + + // Load loginId into MDC context. + MDC.put(LoggerProperties.MDC_LOGIN_ID, "Unknown"); + + + + if (loginId != null && loginId != "") { + MDC.put(LoggerProperties.MDC_LOGIN_ID, loginId); + } + } + } + + + + + public static String getFullURL(HttpServletRequest request) { + if (request != null) { + StringBuffer requestURL = request.getRequestURL(); + String queryString = request.getQueryString(); + + if (queryString == null) { + return requestURL.toString(); + } else { + return requestURL.append('?').append(queryString).toString(); + } + } + return ""; + } + + - // Load the request based fields - if (req != null) { +} +enum AlarmSeverityEnum { + CRITICAL("1"), + MAJOR("2"), + MINOR("3"), + INFORMATIONAL("4"), + NONE("0"); - // Rest Path - MDC.put(MDC_SERVICE_NAME, req.getServletPath()); + private final String severity; - // Client IPAddress i.e. IPAddress of the remote host who is making - // this request. - String clientIPAddress = req.getHeader("X-FORWARDED-FOR"); - if (clientIPAddress == null) { - clientIPAddress = req.getRemoteAddr(); - } - } - } + AlarmSeverityEnum(String severity) { + this.severity = severity; + } + + public String severity() { + return severity; + } +} + +class LoggerProperties { + + +public static final String MDC_APPNAME = "AppName"; +public static final String MDC_REST_PATH = "RestPath"; +public static final String MDC_REST_METHOD = "RestMethod"; +public static final String INSTANCE_UUID = "instance_uuid"; +public static final String MDC_CLASS_NAME = "class"; +public static final String MDC_LOGIN_ID = "LoginId"; +public static final String MDC_TIMER = "Timer"; +public static final String PARTNER_NAME = "PartnerName"; +public static final String FULL_URL = "Full-URL"; +public static final String AUDITLOG_BEGIN_TIMESTAMP = "AuditLogBeginTimestamp"; +public static final String AUDITLOG_END_TIMESTAMP = "AuditLogEndTimestamp"; +public static final String METRICSLOG_BEGIN_TIMESTAMP = "MetricsLogBeginTimestamp"; +public static final String METRICSLOG_END_TIMESTAMP = "MetricsLogEndTimestamp"; +public static final String CLIENT_IP_ADDRESS = "ClientIPAddress"; +public static final String STATUS_CODE = "StatusCode"; +public static final String RESPONSE_CODE = "ResponseCode"; + +public static final String HTTP = "HTTP"; +public static final String HTTPS = "HTTPS"; +public static final String UNKNOWN = "Unknown"; +public static final String PROTOCOL = "PROTOCOL"; +public static final String USERAGENT_NAME = "user-agent"; +public static final String USER_ATTRIBUTE_NAME = "user_attribute_name"; + + +private LoggerProperties(){} + +private static Properties properties; + +private static String propertyFileName = "logger.properties"; + +private static final Object lockObject = new Object(); + +//private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoggerProperties.class); + +/** + * Gets the property value for the specified key. If a value is found, leading + * and trailing space is trimmed. + * + * @param property + * Property key + * @return Value for the named property; null if the property file was not + * loaded or the key was not found. + */ +public static String getProperty(String property) { + if (properties == null) { + synchronized (lockObject) { + try { + if (!initialize()) { +// logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + propertyFileName); + return null; + } + } catch (IOException e) { +// logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + propertyFileName ,e); + return null; + } + } + } + String value = properties.getProperty(property); + if (value != null) + value = value.trim(); + return value; +} + +/** + * Reads properties from a portal.properties file on the classpath. + * + * Clients do NOT need to call this method. Clients MAY call this method to test + * whether the properties file can be loaded successfully. + * + * @return True if properties were successfully loaded, else false. + * @throws IOException + * On failure + */ +private static boolean initialize() throws IOException { + if (properties != null) + return true; + InputStream in = LoggerProperties.class.getClassLoader().getResourceAsStream(propertyFileName); + if (in == null) + return false; + properties = new Properties(); + try { + properties.load(in); + } finally { + in.close(); + } + return true; } +}
\ No newline at end of file |