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();
48 private String className;
49 private static ConcurrentMap<String, EELFLoggerDelegate> classMap = new ConcurrentHashMap<>();
51 public EELFLoggerDelegate(final String className) {
53 this.className = className;
57 * Convenience method that gets a logger for the specified class.
59 * @see #getLogger(String)
62 * @return Instance of EELFLoggerDelegate
64 public static EELFLoggerDelegate getLogger(Class<?> clazz) {
65 return getLogger(clazz.getName());
69 * Gets a logger for the specified class name. If the logger does not already exist in the map,
70 * this creates a new logger.
72 * @param className If null or empty, uses EELFLoggerDelegate as the class name.
73 * @return Instance of EELFLoggerDelegate
75 public static EELFLoggerDelegate getLogger(final String className) {
76 String classNameNeverNull = className == null || "".equals(className)
77 ? EELFLoggerDelegate.class.getName()
79 EELFLoggerDelegate delegate = classMap.get(classNameNeverNull);
80 if (delegate == null) {
81 delegate = new EELFLoggerDelegate(className);
82 classMap.put(className, delegate);
88 * Logs a message at the lowest level: trace.
93 public void trace(EELFLogger logger, String msg) {
94 if (logger.isTraceEnabled()) {
100 * Logs a message with parameters at the lowest level: trace.
106 public void trace(EELFLogger logger, String msg, Object... arguments) {
107 if (logger.isTraceEnabled()) {
108 logger.trace(msg, arguments);
113 * Logs a message and throwable at the lowest level: trace.
119 public void trace(EELFLogger logger, String msg, Throwable th) {
120 if (logger.isTraceEnabled()) {
121 logger.trace(msg, th);
126 * Logs a message at the second-lowest level: debug.
131 public void debug(EELFLogger logger, String msg) {
132 if (logger.isDebugEnabled()) {
138 * Logs a message with parameters at the second-lowest level: debug.
144 public void debug(EELFLogger logger, String msg, Object... arguments) {
145 if (logger.isDebugEnabled()) {
146 logger.debug(msg, arguments);
151 * Logs a message and throwable at the second-lowest level: debug.
157 public void debug(EELFLogger logger, String msg, Throwable th) {
158 if (logger.isDebugEnabled()) {
159 logger.debug(msg, th);
164 * Logs a message at info level.
169 public void info(EELFLogger logger, String msg) {
170 logger.info(className + " - "+msg);
174 * Logs a message with parameters at info level.
180 public void info(EELFLogger logger, String msg, Object... arguments) {
181 logger.info(msg, arguments);
185 * Logs a message and throwable at info level.
191 public void info(EELFLogger logger, String msg, Throwable th) {
192 logger.info(msg, th);
196 * Logs a message at warn level.
201 public void warn(EELFLogger logger, String msg) {
206 * Logs a message with parameters at warn level.
212 public void warn(EELFLogger logger, String msg, Object... arguments) {
213 logger.warn(msg, arguments);
217 * Logs a message and throwable at warn level.
223 public void warn(EELFLogger logger, String msg, Throwable th) {
224 logger.warn(msg, th);
228 * Logs a message at error level.
233 * @deprecated use {@link #error(EELF, Exception)} instead
236 public void error(EELFLogger logger, String msg) {
237 logger.error(className+ " - " + msg);
241 * Logs a message at error level.
246 public void error(EELFLogger logger, Exception e) {
247 logger.error(className+ " - ", e);
251 * Logs a message with parameters at error level.
257 * @deprecated use {@link #error(EELF, Exception, Object...)} instead
260 public void error(EELFLogger logger, String msg, Object... arguments) {
261 logger.error(msg, arguments);
265 * Logs a message with parameters at error level.
271 public void error(EELFLogger logger, Exception e, Object... arguments) {
272 logger.error("Exception", e, arguments);
276 * Logs a message and throwable at error level.
282 public void error(EELFLogger logger, String msg, Throwable th) {
283 logger.error(msg, th);
287 * Logs a message with the associated alarm severity at error level.
292 * @deprecated use {@link #error(EELF, Exception)} instead
295 public void error(EELFLogger logger, String msg, Object /* AlarmSeverityEnum */ severtiy) {
300 * Initializes the logger context.
303 setGlobalLoggingContext();
305 "############################ Logging is started. ############################";
306 // These loggers emit the current date-time without being told.
307 info(applicationLogger, msg);
308 error(errorLogger, msg);
309 debug(debugLogger, msg);
310 info(auditLogger, msg);
311 info(metricsLogger, msg);
315 * Builds a message using a template string and the arguments.
321 private String formatMessage(String message, Object... args) {
322 StringBuilder sbFormattedMessage = new StringBuilder();
323 if (args != null && args.length > 0 && message != null && message != "") {
324 MessageFormat mf = new MessageFormat(message);
325 sbFormattedMessage.append(mf.format(args));
327 sbFormattedMessage.append(message);
330 return sbFormattedMessage.toString();
334 * Loads all the default logging fields into the MDC context.
336 private void setGlobalLoggingContext() {
337 MDC.put(MDC_SERVICE_INSTANCE_ID, "");
339 MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
340 MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
341 } catch (Exception e) {
342 errorLogger.error("setGlobalLoggingContext failed", e);
346 public static void mdcPut(String key, String value) {
350 public static String mdcGet(String key) {
354 public static void mdcRemove(String key) {
359 * Loads the RequestId/TransactionId into the MDC which it should be receiving with an each
360 * incoming REST API request. Also, configures few other request based logging fields into the
366 public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req,
368 // Load the default fields
369 setGlobalLoggingContext();
371 // Load the request based fields
374 MDC.put(MDC_SERVICE_NAME, req.getServletPath());
376 // Client IPAddress i.e. IPAddress of the remote host who is making
378 String clientIPAddress = req.getHeader("X-FORWARDED-FOR");
379 if (clientIPAddress == null) {
380 clientIPAddress = req.getRemoteAddr();