From 7968b32c3f18cf8e98d87229f100c622323cd78e Mon Sep 17 00:00:00 2001 From: Joseph Chou Date: Wed, 22 Apr 2020 14:06:23 -0400 Subject: Logging enhancement work Update log tool to provide placeholder feature Change-Id: I3f2b10d009a1d51f30a6080c07459eb0ac07189f Issue-ID: POLICY-2478 Signed-off-by: Joseph Chou --- .../policy/common/logging/OnapLoggingUtils.java | 39 ++++- .../policy/common/logging/eelf/PolicyLogger.java | 192 +++++++++++++++------ .../common/logging/flexlogger/EelfLogger.java | 68 +++++++- .../policy/common/logging/flexlogger/Logger.java | 32 +++- .../policy/common/logging/flexlogger/Logger4J.java | 93 ++++++++++ .../common/logging/flexlogger/SystemOutLogger.java | 103 ++++++++++- .../common/logging/eelf/PolicyLoggerTest.java | 20 +++ .../common/logging/flexlogger/EelfLoggerTest.java | 20 ++- .../logging/flexlogger/SystemOutLoggerTest.java | 10 +- 9 files changed, 507 insertions(+), 70 deletions(-) diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java index 6c0879ea..9bfbe68d 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,4 +60,41 @@ public class OnapLoggingUtils { return requestContext; } + /** + * Create message text replace {} place holder with data + * if last argument is throwable/exception, pass it as argument to logger. + * @param format message format can contains text and {} + * @param arguments output arguments + * @return + */ + public static String formatMessage(String format, Object ...arguments) { + if (arguments.length <= 0 || arguments[0] == null) { + return format; + } + int index; + StringBuilder builder = new StringBuilder(); + String[] token = format.split("[{][}]"); + for (index = 0; index < arguments.length; index++) { + if (index < token.length) { + builder.append(token[index]); + builder.append(arguments[index]); + } else { + break; + } + } + for (int index2 = index; index2 < token.length; index2++) { + builder.append(token[index2]); + } + + return builder.toString(); + } + + /** + * Check object is throwable. + * @param obj to verify + * @return true if object is throwable or false otherwise + */ + public static boolean isThrowable(Object obj) { + return (obj instanceof Throwable); + } } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java index 9e5fd5a6..8de057f9 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java @@ -54,6 +54,7 @@ import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.Instant; +import java.util.Arrays; import java.util.Date; import java.util.Iterator; import java.util.Map; @@ -63,6 +64,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentMap; import java.util.function.Consumer; import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.flexlogger.LoggerType; import org.slf4j.MDC; @@ -331,7 +333,6 @@ public class PolicyLogger { * Set Timestamps for start, end and duration of logging a transaction. */ private static void seTimeStamps() { - MDC.put(MDC_INSTANCE_UUID, ""); MDC.put(MDC_ALERT_SEVERITY, ""); @@ -432,18 +433,6 @@ public class PolicyLogger { debugLogger.info(msg, arguments); } - /** - * Records only one String message with its class name. - * - * @param className the class name - * @param arg0 the message - */ - public static void info(String className, String arg0) { - MDC.put(classNameProp, className); - debugLogger.info(MessageCodes.GENERAL_INFO, arg0); - } - - /** * Records only one String message. * @@ -483,14 +472,29 @@ public class PolicyLogger { } /** - * Records only one String message with its class name. + * Records a message with passed in message text and variable number of arguments. * - * @param arg0 log message - * @param className class name + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments */ - public static void warn(String className, String arg0) { - MDC.put(classNameProp, className); - debugLogger.warn(MessageCodes.GENERAL_INFO, arg0); + public static void info(String message, Object... arguments) { + if (!debugLogger.isInfoEnabled()) { + return; + } + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + debugLogger.info(MessageCodes.GENERAL_INFO, + arguments[0] == null ? "" : arguments[0].toString()); + return; + } + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + String arguments2 = getNormalizedStackTrace((Throwable)arguments[0], ""); + debugLogger.info(MessageCodes.GENERAL_INFO, message + arguments2); + return; + } + + MDC.put(classNameProp, ""); + debugLogger.info(message, arguments); } /** @@ -553,15 +557,28 @@ public class PolicyLogger { } /** - * Records only one String message with its class name. + * Records a message with passed in message text and variable number of arguments. * - * @param className class name - * @param arg0 log message + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments */ - public static void error(String className, String arg0) { - MDC.put(classNameProp, className); - setErrorCode(MessageCodes.GENERAL_ERROR); - errorLogger.error(MessageCodes.GENERAL_ERROR, arg0); + public static void warn(String message, Object... arguments) { + if (!debugLogger.isWarnEnabled()) { + return; + } + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + debugLogger.warn(MessageCodes.GENERAL_INFO, + arguments[0] == null ? "" : arguments[0].toString()); + return; + } + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + String arguments2 = getNormalizedStackTrace((Throwable)arguments[0], ""); + debugLogger.warn(MessageCodes.GENERAL_INFO, message + arguments2); + return; + } + MDC.put(classNameProp, ""); + debugLogger.warn(message, arguments); } /** @@ -629,25 +646,41 @@ public class PolicyLogger { } /** - * Records a message with passed in message code and a list of string values. + * Records a message with passed in message text and variable number of arguments. * - * @param msg the message code - * @param arguments the messages + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments */ - public static void debug(MessageCodes msg, String... arguments) { + public static void error(String message, Object... arguments) { + if (!errorLogger.isErrorEnabled()) { + return; + } + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + setErrorCode(MessageCodes.GENERAL_ERROR); + errorLogger.error(MessageCodes.GENERAL_ERROR, + arguments[0] == null ? "" : arguments[0].toString()); + return; + } + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + String arguments2 = getNormalizedStackTrace((Throwable)arguments[0], ""); + errorLogger.error(MessageCodes.GENERAL_ERROR, message + arguments2); + return; + } MDC.put(classNameProp, ""); - debugLogger.debug(msg, arguments); + setErrorCode(MessageCodes.GENERAL_ERROR); + errorLogger.error(message, arguments); } /** - * Records only one String message with its class name. + * Records a message with passed in message code and a list of string values. * - * @param className the class name - * @param arg0 the message + * @param msg the message code + * @param arguments the messages */ - public static void debug(String className, String arg0) { - MDC.put(classNameProp, className); - debugLogger.debug(MessageCodes.GENERAL_INFO, arg0); + public static void debug(MessageCodes msg, String... arguments) { + MDC.put(classNameProp, ""); + debugLogger.debug(msg, arguments); } /** @@ -700,17 +733,28 @@ public class PolicyLogger { } /** - * Records only one String message with its class name. + * Records a message with passed in message text and variable number of arguments. * - * @param className the class name - * @param arg0 the message + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments */ - public static void audit(String className, Object arg0) { - MDC.put(INVOCATION_ID, MDC.get(MDC_KEY_REQUEST_ID)); - MDC.put(STATUS_CODE, COMPLETE_STATUS); - MDC.put(RESPONSE_CODE, "0"); - MDC.put(classNameProp, className); - auditLogger.info("" + arg0); + public static void debug(String message, Object... arguments) { + if (!debugLogger.isDebugEnabled()) { + return; + } + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + debugLogger.debug(MessageCodes.GENERAL_INFO, + arguments[0] == null ? "" : arguments[0].toString()); + return; + } + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + String arguments2 = getNormalizedStackTrace((Throwable)arguments[0], ""); + debugLogger.debug(MessageCodes.GENERAL_INFO, message + arguments2); + return; + } + MDC.put(classNameProp, ""); + debugLogger.debug(message, arguments); } /** @@ -726,6 +770,29 @@ public class PolicyLogger { auditLogger.info("" + arg0); } + /** + * Records a message with passed in message text and variable number of arguments. + * + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments + */ + public static void audit(String message, Object... arguments) { + if (!auditLogger.isInfoEnabled()) { + return; + } + MDC.put(INVOCATION_ID, postMdcInfoForEvent(null)); + MDC.put(STATUS_CODE, COMPLETE_STATUS); + MDC.put(RESPONSE_CODE, "0"); + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + auditLogger.info(arguments[0] == null ? "" : arguments[0].toString()); + return; + } + + MDC.put(classNameProp, ""); + auditLogger.info(message, arguments); + } + /** * returns true for enabled, false for not enabled. */ @@ -1093,6 +1160,7 @@ public class PolicyLogger { * @param arg0 the message */ public static void metrics(String arg0) { + seTimeStamps(); MDC.put(INVOCATION_ID, MDC.get(MDC_KEY_REQUEST_ID)); MDC.put(RESPONSE_CODE, "0"); String serviceName = MDC.get(MDC_SERVICE_NAME); @@ -1100,31 +1168,47 @@ public class PolicyLogger { } /** - * Records the metrics event with a class name and a String message. + * Records the metrics event with a String message. * * @param arg0 the message */ - public static void metrics(String className, Object arg0) { + public static void metrics(Object arg0) { seTimeStamps(); MDC.put(INVOCATION_ID, MDC.get(MDC_KEY_REQUEST_ID)); MDC.put(RESPONSE_CODE, "0"); - MDC.put(classNameProp, className); + MDC.put(classNameProp, ""); String serviceName = MDC.get(MDC_SERVICE_NAME); metricsLogger.info(MessageCodes.RULE_METRICS_INFO, serviceName, "" + arg0); } /** - * Records the metrics event with a String message. + * Records a message with passed in message text and variable number of arguments. * - * @param arg0 the message + * @param message class name if one argument, otherwise message text + * @param arguments variable number of arguments */ - public static void metrics(Object arg0) { + public static void metrics(String message, Object... arguments) { + if (!metricsLogger.isInfoEnabled()) { + return; + } seTimeStamps(); MDC.put(INVOCATION_ID, MDC.get(MDC_KEY_REQUEST_ID)); MDC.put(RESPONSE_CODE, "0"); + if (arguments.length == 1 && !OnapLoggingUtils.isThrowable(arguments[0])) { + MDC.put(classNameProp, message); + String serviceName = MDC.get(MDC_SERVICE_NAME); + metricsLogger.info(MessageCodes.RULE_METRICS_INFO, serviceName, + arguments[0] == null ? "" : arguments[0].toString()); + return; + } + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + String arguments2 = getNormalizedStackTrace((Throwable)arguments[0], ""); + metricsLogger.info(MessageCodes.RULE_METRICS_INFO, message + arguments2); + return; + } + MDC.put(classNameProp, ""); - String serviceName = MDC.get(MDC_SERVICE_NAME); - metricsLogger.info(MessageCodes.RULE_METRICS_INFO, serviceName, "" + arg0); + metricsLogger.info(message, arguments); } /** diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java index a3e5cc8f..df60fa9c 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/EelfLogger.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -165,6 +165,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.debug(MessageCodes.GENERAL_INFO, throwable, message.toString()); } + /** + * Records a message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void debug(String message, Object... arguments) { + PolicyLogger.debug(message, arguments); + } + /** * Records an error message. * @@ -209,6 +220,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.error(msg, arguments); } + /** + * Records an error message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void error(String message, Object... arguments) { + PolicyLogger.error(message, arguments); + } + /** * Records a message. * @@ -230,6 +252,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.info(MessageCodes.GENERAL_INFO, throwable, message.toString()); } + /** + * Records a message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void info(String message, Object... arguments) { + PolicyLogger.info(message, arguments); + } + /** * Records a message. * @@ -274,6 +307,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.warn(msg, className, throwable, arguments); } + /** + * Records a message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void warn(String message, Object... arguments) { + PolicyLogger.warn(message, arguments); + } + /** * Records a message. * @@ -386,6 +430,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.audit(message); } + /** + * Records a message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void audit(String message, Object... arguments) { + PolicyLogger.audit(message, arguments); + } + /** * Records an audit message. * @@ -484,6 +539,17 @@ public class EelfLogger implements Logger, Serializable { PolicyLogger.metrics(className, message); } + /** + * Records a message. + * + * @param message the message + * @param arguments the arguments for message + */ + @Override + public void metrics(String message, Object... arguments) { + PolicyLogger.metrics(message, arguments); + } + /** * Populates MDC Info. * diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger.java index d6f020e0..315cd935 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,11 @@ public interface Logger { */ public void debug(Object message, Throwable throwable); + /** + * Prints messages with the level.DEBUG + */ + public void debug(String message, Object... arguments); + /** * Prints messages with the level.ERROR */ @@ -60,6 +65,11 @@ public interface Logger { */ public void error(MessageCodes msg, Throwable arg0, String... arguments); + /** + * Prints messages with the level.ERROR + */ + public void error(String message, Object... arguments); + /** * Prints messages with the level.INFO */ @@ -70,6 +80,11 @@ public interface Logger { */ public void info(Object message, Throwable throwable); + /** + * Prints messages with the level.INFO + */ + public void info(String message, Object... arguments); + /** * Prints messages with the level.WARN */ @@ -90,6 +105,11 @@ public interface Logger { */ public void warn(MessageCodes msg, Throwable arg0, String... arguments); + /** + * Prints messages with the level.WARN + */ + public void warn(String message, Object... arguments); + /** * Prints messages with the level.TRACE */ @@ -110,6 +130,11 @@ public interface Logger { */ public void audit(Object arg0, Throwable throwable); + /** + * Prints messages in audit log with the level.INFO + */ + public void audit(String message, Object... arguments); + /** * Records event Id in audit log with the level.INFO */ @@ -156,6 +181,11 @@ public interface Logger { */ public void metrics(Object arg0); + /** + * Records the Metrics log message. + */ + public void metrics(String message, Object... arguments); + /** * Returns a boolean value, true for debug logging enabled, false for not enabled. */ diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java index 5af40b09..bb5e1142 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java @@ -33,6 +33,7 @@ import java.util.UUID; import org.apache.log4j.Logger; import org.apache.log4j.Priority; +import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -114,6 +115,21 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.debug(message, throwable); } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void debug(String message, Object... arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + log.debug(message, (Throwable)arguments[0]); + } else { + log.debug(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records an error message. * @@ -159,6 +175,21 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.error(transId + "|" + className + "|" + "MessageCode:" + msg + Arrays.asList(arguments)); } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void error(String message, Object... arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + log.error(message, (Throwable)arguments[0]); + } else { + log.error(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records a message. * @@ -180,6 +211,21 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.info(message, throwable); } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void info(String message, Object... arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + log.info(message, (Throwable)arguments[0]); + } else { + log.info(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records a message. * @@ -224,6 +270,21 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.warn(className + "|" + "MessageCodes:" + msg + Arrays.asList(arguments)); } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void warn(String message, Object... arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + log.warn(message, (Throwable)arguments[0]); + } else { + log.warn(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records a message. * @@ -330,6 +391,21 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.info(message, throwable); } + /** + * Records an audit message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void audit(String message, Object... arguments) { + if (arguments.length == 1) { + log.info(message, (Throwable)arguments[0]); + } else { + log.info(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records an audit message. * @@ -443,6 +519,22 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge log.info(message); } + /** + * Records a metrics message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void metrics(String message, Object... arguments) { + if (arguments.length > 0 && OnapLoggingUtils.isThrowable(arguments[arguments.length - 1])) { + log.info(OnapLoggingUtils.formatMessage(message, arguments), + (Throwable)arguments[arguments.length - 1]); + } else { + log.info(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Returns transaction Id. * @@ -511,4 +603,5 @@ public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logge // look up associated logger log = Logger.getLogger(className); } + } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java index f7a68a3d..bc7633da 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/SystemOutLogger.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import java.io.Serializable; import java.util.Arrays; import java.util.UUID; +import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -151,6 +152,21 @@ public class SystemOutLogger implements Logger, Serializable { displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void debug(String message, Object...arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records an error message. * @@ -193,10 +209,24 @@ public class SystemOutLogger implements Logger, Serializable { */ @Override public void error(MessageCodes msg, String... arguments) { - displayMessage(transId + "|" + className + " : " + "MessageCode:" + msg + Arrays.asList(arguments)); } + /** + * Records a error message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void error(String message, Object...arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records a message. * @@ -222,21 +252,25 @@ public class SystemOutLogger implements Logger, Serializable { * Records a message. * * @param message the message + * @param arguments variable number of arguments */ @Override - public void warn(Object message) { - displayMessage(transId + "|" + className + " : " + message); + public void info(String message, Object...arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } } /** * Records a message. * * @param message the message - * @param throwable the throwable */ @Override - public void warn(Object message, Throwable throwable) { - displayMessage(transId + "|" + className + " : " + message + ":" + throwable); + public void warn(Object message) { + displayMessage(transId + "|" + className + " : " + message); } /** @@ -251,6 +285,17 @@ public class SystemOutLogger implements Logger, Serializable { displayMessage(transId + "|" + className + " : " + "MessageCodes:" + msg + Arrays.asList(arguments)); } + /** + * Records a message. + * + * @param message the message + * @param throwable the throwable + */ + @Override + public void warn(Object message, Throwable throwable) { + displayMessage(transId + "|" + className + " : " + message + ":" + throwable); + } + /** * Records a message. * @@ -265,6 +310,21 @@ public class SystemOutLogger implements Logger, Serializable { } + /** + * Records a message. + * + * @param message the message + * @param arguments variable number of arguments + */ + @Override + public void warn(String message, Object...arguments) { + if (arguments.length == 1 && OnapLoggingUtils.isThrowable(arguments[0])) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records a message. * @@ -370,6 +430,20 @@ public class SystemOutLogger implements Logger, Serializable { displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } + /** + * Records an audit message. + * + * @param message the message + */ + @Override + public void audit(String message, Object... arguments) { + if (arguments.length == 1) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Records an audit message. * @@ -479,6 +553,21 @@ public class SystemOutLogger implements Logger, Serializable { displayMessage(className + " : " + message); } + /** + * Records a metrics message. + * + * @param message the message + * @param arguments the arguments + */ + @Override + public void metrics(String message, Object... arguments) { + if (arguments.length == 1) { + displayMessage(className + " : " + message + " : " + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } + } + /** * Returns transaction Id. * diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java index b318c18d..adb7d366 100644 --- a/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java +++ b/common-logging/src/test/java/org/onap/policy/common/logging/eelf/PolicyLoggerTest.java @@ -36,6 +36,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.onap.policy.common.logging.eelf.OnapConfigProperties.PARTNER_NAME; import static org.onap.policy.common.logging.eelf.OnapConfigProperties.RESPONSE_CODE; import static org.onap.policy.common.logging.eelf.OnapConfigProperties.RESPONSE_DESCRIPTION; @@ -200,6 +202,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.info("str1", "str2"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); + PolicyLogger.info("str1", "str2"); Mockito.verify(mockLogger).info(MessageCodes.GENERAL_INFO, "str2"); } @@ -243,6 +248,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.warn("str1", "str2"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isWarnEnabled()).thenReturn(true); + PolicyLogger.warn("str1", "str2"); Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_INFO, "str2"); } @@ -286,6 +294,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); PolicyLogger.error("str1", "str2"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isErrorEnabled()).thenReturn(true); + PolicyLogger.error("str1", "str2"); Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "str2"); assertEquals("500", MDC.get("ErrorCode")); assertEquals("This is a general error message during the process. Please check the error message for detail " @@ -356,6 +367,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); PolicyLogger.debug("str1", "str2"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isDebugEnabled()).thenReturn(true); + PolicyLogger.debug("str1", "str2"); Mockito.verify(mockLogger).debug(MessageCodes.GENERAL_INFO, "str2"); } @@ -380,6 +394,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "auditLogger", mockLogger); PolicyLogger.audit("PolicyLoggerTest", 1); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); + PolicyLogger.audit("PolicyLoggerTest", 1); assertEquals("PolicyLoggerTest", MDC.get("ClassName")); assertEquals("COMPLETE", MDC.get("StatusCode")); Mockito.verify(mockLogger).info("1"); @@ -591,6 +608,9 @@ public class PolicyLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); PolicyLogger.metrics("PolicyLoggerTest", 1); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); + PolicyLogger.metrics("PolicyLoggerTest", 1); Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("1")); } diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java index d3c09ee6..1245e164 100644 --- a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/EelfLoggerTest.java @@ -3,7 +3,7 @@ * ONAP-Logging * ================================================================================ * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.never; import com.att.eelf.configuration.EELFLogger; import java.util.UUID; @@ -93,6 +94,9 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.debug("message"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isDebugEnabled()).thenReturn(true); + eelfLogger.debug("message"); Mockito.verify(mockLogger).debug(MessageCodes.GENERAL_INFO, "message"); } @@ -101,6 +105,9 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "errorLogger", mockLogger); eelfLogger.error("message"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isErrorEnabled()).thenReturn(true); + eelfLogger.error("message"); Mockito.verify(mockLogger).error(MessageCodes.GENERAL_ERROR, "message"); } @@ -109,6 +116,9 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.info("message"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); + eelfLogger.info("message"); Mockito.verify(mockLogger).info(MessageCodes.GENERAL_INFO, "message"); } @@ -117,6 +127,9 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.warn("message"); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isWarnEnabled()).thenReturn(true); + eelfLogger.warn("message"); Mockito.verify(mockLogger).warn(MessageCodes.GENERAL_INFO, "message"); } @@ -219,6 +232,8 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "debugLogger", mockLogger); eelfLogger.info("message", new NullPointerException()); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); Mockito.verify(mockLogger).info((MessageCodes) Mockito.any(), Mockito.startsWith("message:java.lang.NullPointerException")); } @@ -317,6 +332,9 @@ public class EelfLoggerTest { EELFLogger mockLogger = Mockito.mock(EELFLogger.class); Whitebox.setInternalState(PolicyLogger.class, "metricsLogger", mockLogger); eelfLogger.metrics(1); + Mockito.verify(mockLogger, never()).info(Mockito.anyString(), Mockito.anyString()); + Mockito.when(mockLogger.isInfoEnabled()).thenReturn(true); + eelfLogger.metrics(1); Mockito.verify(mockLogger).info(Mockito.eq(MessageCodes.RULE_METRICS_INFO), Mockito.anyString(), Mockito.eq("1")); } diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java index ca73d1cb..cde423f5 100644 --- a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/SystemOutLoggerTest.java @@ -427,7 +427,7 @@ public class SystemOutLoggerTest { try { System.setOut(ps); systemOutLogger.setTransId("transactionId"); - systemOutLogger.debug(1, new NullPointerException()); + systemOutLogger.debug("1", new NullPointerException()); assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); } finally { @@ -444,7 +444,7 @@ public class SystemOutLoggerTest { try { System.setOut(ps); systemOutLogger.setTransId("transactionId"); - systemOutLogger.error(1, new NullPointerException()); + systemOutLogger.error("1", new NullPointerException()); assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); } finally { @@ -461,7 +461,7 @@ public class SystemOutLoggerTest { try { System.setOut(ps); systemOutLogger.setTransId("transactionId"); - systemOutLogger.info(1, new NullPointerException()); + systemOutLogger.info("1", new NullPointerException()); assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); } finally { @@ -478,7 +478,7 @@ public class SystemOutLoggerTest { try { System.setOut(ps); systemOutLogger.setTransId("transactionId"); - systemOutLogger.warn(1, new NullPointerException()); + systemOutLogger.warn("1", new NullPointerException()); assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); } finally { @@ -512,7 +512,7 @@ public class SystemOutLoggerTest { try { System.setOut(ps); systemOutLogger.setTransId("transactionId"); - systemOutLogger.audit(1, new NullPointerException()); + systemOutLogger.audit("1", new NullPointerException()); assertTrue(baos.toString(), baos.toString().contains("transactionId|SystemOutLoggerTest : 1:java.lang.NullPointerException")); } finally { -- cgit 1.2.3-korg