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 * ====================================================================
22 package org.onap.music.eelf.logging;
24 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
25 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
26 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID;
27 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
28 import java.net.InetAddress;
29 import java.text.MessageFormat;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32 import javax.servlet.http.HttpServletRequest;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.att.eelf.configuration.SLF4jWrapper;
38 public class EELFLoggerDelegate extends SLF4jWrapper implements EELFLogger {
40 public static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
41 public static final EELFLogger applicationLogger =
42 EELFManager.getInstance().getApplicationLogger();
43 public static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
44 public static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
45 public static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
47 private String className;
48 private static ConcurrentMap<String, EELFLoggerDelegate> classMap = new ConcurrentHashMap<>();
50 public EELFLoggerDelegate(final String className) {
52 this.className = className;
56 * Convenience method that gets a logger for the specified class.
58 * @see #getLogger(String)
61 * @return Instance of EELFLoggerDelegate
63 public static EELFLoggerDelegate getLogger(Class<?> clazz) {
64 return getLogger(clazz.getName());
68 * Gets a logger for the specified class name. If the logger does not already exist in the map,
69 * this creates a new logger.
71 * @param className If null or empty, uses EELFLoggerDelegate as the class name.
72 * @return Instance of EELFLoggerDelegate
74 public static EELFLoggerDelegate getLogger(final String className) {
75 String classNameNeverNull = className == null || "".equals(className)
76 ? EELFLoggerDelegate.class.getName()
78 EELFLoggerDelegate delegate = classMap.get(classNameNeverNull);
79 if (delegate == null) {
80 delegate = new EELFLoggerDelegate(className);
81 classMap.put(className, delegate);
87 * Logs a message at the lowest level: trace.
92 public void trace(EELFLogger logger, String msg) {
93 if (logger.isTraceEnabled()) {
99 * Logs a message with parameters at the lowest level: trace.
105 public void trace(EELFLogger logger, String msg, Object... arguments) {
106 if (logger.isTraceEnabled()) {
107 logger.trace(msg, arguments);
112 * Logs a message and throwable at the lowest level: trace.
118 public void trace(EELFLogger logger, String msg, Throwable th) {
119 if (logger.isTraceEnabled()) {
120 logger.trace(msg, th);
125 * Logs a message at the second-lowest level: debug.
130 public void debug(EELFLogger logger, String msg) {
131 if (logger.isDebugEnabled()) {
137 * Logs a message with parameters at the second-lowest level: debug.
143 public void debug(EELFLogger logger, String msg, Object... arguments) {
144 if (logger.isDebugEnabled()) {
145 logger.debug(msg, arguments);
150 * Logs a message and throwable at the second-lowest level: debug.
156 public void debug(EELFLogger logger, String msg, Throwable th) {
157 if (logger.isDebugEnabled()) {
158 logger.debug(msg, th);
163 * Logs a message at info level.
168 public void info(EELFLogger logger, String msg) {
169 logger.info(className + " - "+msg);
173 * Logs a message with parameters at info level.
179 public void info(EELFLogger logger, String msg, Object... arguments) {
180 logger.info(msg, arguments);
184 * Logs a message and throwable at info level.
190 public void info(EELFLogger logger, String msg, Throwable th) {
191 logger.info(msg, th);
195 * Logs a message at warn level.
200 public void warn(EELFLogger logger, String msg) {
205 * Logs a message with parameters at warn level.
211 public void warn(EELFLogger logger, String msg, Object... arguments) {
212 logger.warn(msg, arguments);
216 * Logs a message and throwable at warn level.
222 public void warn(EELFLogger logger, String msg, Throwable th) {
223 logger.warn(msg, th);
227 * Logs a message at error level.
232 public void error(EELFLogger logger, String msg) {
233 logger.error(className+ " - " + msg);
237 * Logs a message with parameters at error level.
243 public void error(EELFLogger logger, String msg, Object... arguments) {
244 logger.warn(msg, arguments);
248 * Logs a message and throwable at error level.
254 public void error(EELFLogger logger, String msg, Throwable th) {
255 logger.warn(msg, th);
259 * Logs a message with the associated alarm severity at error level.
265 public void error(EELFLogger logger, String msg, Object /* AlarmSeverityEnum */ severtiy) {
270 * Initializes the logger context.
273 setGlobalLoggingContext();
275 "############################ Logging is started. ############################";
276 // These loggers emit the current date-time without being told.
277 info(applicationLogger, msg);
278 error(errorLogger, msg);
279 debug(debugLogger, msg);
280 info(auditLogger, msg);
281 info(metricsLogger, msg);
285 * Builds a message using a template string and the arguments.
291 private String formatMessage(String message, Object... args) {
292 StringBuilder sbFormattedMessage = new StringBuilder();
293 if (args != null && args.length > 0 && message != null && message != "") {
294 MessageFormat mf = new MessageFormat(message);
295 sbFormattedMessage.append(mf.format(args));
297 sbFormattedMessage.append(message);
300 return sbFormattedMessage.toString();
304 * Loads all the default logging fields into the MDC context.
306 private void setGlobalLoggingContext() {
307 MDC.put(MDC_SERVICE_INSTANCE_ID, "");
309 MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
310 MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
311 } catch (Exception e) {
312 errorLogger.error("setGlobalLoggingContext failed", e);
316 public static void mdcPut(String key, String value) {
320 public static String mdcGet(String key) {
324 public static void mdcRemove(String key) {
329 * Loads the RequestId/TransactionId into the MDC which it should be receiving with an each
330 * incoming REST API request. Also, configures few other request based logging fields into the
336 public void setRequestBasedDefaultsIntoGlobalLoggingContext(HttpServletRequest req,
338 // Load the default fields
339 setGlobalLoggingContext();
341 // Load the request based fields
344 MDC.put(MDC_SERVICE_NAME, req.getServletPath());
346 // Client IPAddress i.e. IPAddress of the remote host who is making
348 String clientIPAddress = req.getHeader("X-FORWARDED-FOR");
349 if (clientIPAddress == null) {
350 clientIPAddress = req.getRemoteAddr();