2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.eelf.logging;
25 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
26 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
27 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID;
28 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
29 import java.net.InetAddress;
30 import java.text.MessageFormat;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentMap;
33 import javax.servlet.http.HttpServletRequest;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.configuration.SLF4jWrapper;
39 public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
41 public static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
42 public static final EELFLogger applicationLogger =
43 EELFManager.getInstance().getApplicationLogger();
44 public static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
45 public static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
46 public static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
47 public static final EELFLogger securityLogger = EELFManager.getInstance().getSecurityLogger();
49 private String className;
50 private static ConcurrentMap<String, EELFLoggerDelegate> classMap = new ConcurrentHashMap<>();
52 public EELFLoggerDelegate(final String className) {
54 this.className = className;
58 * Convenience method that gets a logger for the specified class.
60 * @see #getLogger(String)
63 * @return Instance of EELFLoggerDelegate
65 public static EELFLoggerDelegate getLogger(Class<?> clazz) {
66 return getLogger(clazz.getName());
70 * Gets a logger for the specified class name. If the logger does not already exist in the map,
71 * this creates a new logger.
73 * @param className If null or empty, uses EELFLoggerDelegate as the class name.
74 * @return Instance of EELFLoggerDelegate
76 public static EELFLoggerDelegate getLogger(final String className) {
77 String classNameNeverNull = className == null || "".equals(className)
78 ? EELFLoggerDelegate.class.getName()
80 EELFLoggerDelegate delegate = classMap.get(classNameNeverNull);
81 if (delegate == null) {
82 delegate = new EELFLoggerDelegate(className);
83 classMap.put(className, delegate);
89 * Logs a message at the lowest level: trace.
94 public void trace(EELFLogger logger, String msg) {
95 if (logger.isTraceEnabled()) {
101 * Logs a message with parameters at the lowest level: trace.
107 public void trace(EELFLogger logger, String msg, Object... arguments) {
108 if (logger.isTraceEnabled()) {
109 logger.trace(msg, arguments);
114 * Logs a message and throwable at the lowest level: trace.
120 public void trace(EELFLogger logger, String msg, Throwable th) {
121 if (logger.isTraceEnabled()) {
122 logger.trace(msg, th);
127 * Logs a message at the second-lowest level: debug.
132 public void debug(EELFLogger logger, String msg) {
133 if (logger.isDebugEnabled()) {
139 * Logs a message with parameters at the second-lowest level: debug.
145 public void debug(EELFLogger logger, String msg, Object... arguments) {
146 if (logger.isDebugEnabled()) {
147 logger.debug(msg, arguments);
152 * Logs a message and throwable at the second-lowest level: debug.
158 public void debug(EELFLogger logger, String msg, Throwable th) {
159 if (logger.isDebugEnabled()) {
160 logger.debug(msg, th);
165 * Logs a message at info level.
170 public void info(EELFLogger logger, String msg) {
171 logger.info(className + " - "+msg);
175 * Logs a message with parameters at info level.
181 public void info(EELFLogger logger, String msg, Object... arguments) {
182 logger.info(msg, arguments);
186 * Logs a message and throwable at info level.
192 public void info(EELFLogger logger, String msg, Throwable th) {
193 logger.info(msg, th);
197 * Logs a message at warn level.
202 public void warn(EELFLogger logger, String msg) {
207 * Logs a message with parameters at warn level.
213 public void warn(EELFLogger logger, String msg, Object... arguments) {
214 logger.warn(msg, arguments);
218 * Logs a message and throwable at warn level.
224 public void warn(EELFLogger logger, String msg, Throwable th) {
225 logger.warn(msg, th);
229 * Logs a message at error level.
235 public void error(EELFLogger logger, String msg) {
236 logger.error(className+ " - " + msg);
240 * Logs a message at error level.
245 public void error(EELFLogger logger, Exception e) {
246 logger.error(className+ " - ", e);
250 * Logs a message with parameters at error level.
257 public void error(EELFLogger logger, String msg, Object... arguments) {
258 logger.error(msg, arguments);
262 * Logs a message with parameters at error level.
268 public void error(EELFLogger logger, Exception e, Object... arguments) {
269 logger.error("Exception", e, arguments);
273 * Logs a message and throwable at error level.
279 public void error(EELFLogger logger, String msg, Throwable th) {
280 logger.error(msg, th);
284 * Logs a message with the associated alarm severity at error level.
290 public void error(EELFLogger logger, String msg, Object /* AlarmSeverityEnum */ severtiy) {
295 * Initializes the logger context.
298 setGlobalLoggingContext();
300 "############################ Logging is started. ############################";
301 // These loggers emit the current date-time without being told.
302 info(applicationLogger, msg);
303 error(errorLogger, msg);
304 debug(debugLogger, msg);
305 info(auditLogger, msg);
306 info(metricsLogger, msg);
307 info(securityLogger, msg);
312 * Builds a message using a template string and the arguments.
318 private String formatMessage(String message, Object... args) {
319 StringBuilder sbFormattedMessage = new StringBuilder();
320 if (args != null && args.length > 0 && message != null && message != "") {
321 MessageFormat mf = new MessageFormat(message);
322 sbFormattedMessage.append(mf.format(args));
324 sbFormattedMessage.append(message);
327 return sbFormattedMessage.toString();
331 * Loads all the default logging fields into the MDC context.
333 private void setGlobalLoggingContext() {
334 MDC.put(MDC_SERVICE_INSTANCE_ID, "");
336 MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
337 MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
338 } catch (Exception e) {
339 errorLogger.error("setGlobalLoggingContext failed", e);
343 public static void mdcPut(String key, String value) {
347 public static String mdcGet(String key) {
351 public static void mdcRemove(String key) {
356 * Loads the RequestId/TransactionId into the MDC which it should be receiving with an each
357 * incoming REST API request. Also, configures few other request based logging fields into the
363 public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req,
365 // Load the default fields
366 setGlobalLoggingContext();
368 // Load the request based fields
371 MDC.put(MDC_SERVICE_NAME, req.getServletPath());
373 // Client IPAddress i.e. IPAddress of the remote host who is making
375 String clientIPAddress = req.getHeader("X-FORWARDED-FOR");
376 if (clientIPAddress == null) {
377 clientIPAddress = req.getRemoteAddr();