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 public void error(EELFLogger logger, String msg) {
234 logger.error(className+ " - " + msg);
238 * Logs a message with parameters at error level.
244 public void error(EELFLogger logger, String msg, Object... arguments) {
245 logger.warn(msg, arguments);
249 * Logs a message and throwable at error level.
255 public void error(EELFLogger logger, String msg, Throwable th) {
256 logger.warn(msg, th);
260 * Logs a message with the associated alarm severity at error level.
266 public void error(EELFLogger logger, String msg, Object /* AlarmSeverityEnum */ severtiy) {
271 * Initializes the logger context.
274 setGlobalLoggingContext();
276 "############################ Logging is started. ############################";
277 // These loggers emit the current date-time without being told.
278 info(applicationLogger, msg);
279 error(errorLogger, msg);
280 debug(debugLogger, msg);
281 info(auditLogger, msg);
282 info(metricsLogger, msg);
286 * Builds a message using a template string and the arguments.
292 private String formatMessage(String message, Object... args) {
293 StringBuilder sbFormattedMessage = new StringBuilder();
294 if (args != null && args.length > 0 && message != null && message != "") {
295 MessageFormat mf = new MessageFormat(message);
296 sbFormattedMessage.append(mf.format(args));
298 sbFormattedMessage.append(message);
301 return sbFormattedMessage.toString();
305 * Loads all the default logging fields into the MDC context.
307 private void setGlobalLoggingContext() {
308 MDC.put(MDC_SERVICE_INSTANCE_ID, "");
310 MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
311 MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
312 } catch (Exception e) {
313 errorLogger.error("setGlobalLoggingContext failed", e);
317 public static void mdcPut(String key, String value) {
321 public static String mdcGet(String key) {
325 public static void mdcRemove(String key) {
330 * Loads the RequestId/TransactionId into the MDC which it should be receiving with an each
331 * incoming REST API request. Also, configures few other request based logging fields into the
337 public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req,
339 // Load the default fields
340 setGlobalLoggingContext();
342 // Load the request based fields
345 MDC.put(MDC_SERVICE_NAME, req.getServletPath());
347 // Client IPAddress i.e. IPAddress of the remote host who is making
349 String clientIPAddress = req.getHeader("X-FORWARDED-FOR");
350 if (clientIPAddress == null) {
351 clientIPAddress = req.getRemoteAddr();