diff options
Diffstat (limited to 'common-logging/src/main/java/org/onap/policy/common/logging/flexlogger')
8 files changed, 278 insertions, 790 deletions
diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java index dc740440..425c62a4 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/DisplayUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 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. @@ -20,22 +20,28 @@ package org.onap.policy.common.logging.flexlogger; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + /** * Utilities to display messages. These are generally used while logging is being * configured, or when logging being directed to System.out. As a result, it directly * writes to System.out rather than to a logger. */ + +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class DisplayUtils { - private DisplayUtils() { - // do nothing - } + /* + * As the comment above says, these purposely write to System.out rather than a + * logger, thus sonar is disabled. + */ public static void displayMessage(Object message) { - System.out.println(message); + System.out.println(message); // NOSONAR } public static void displayErrorMessage(Object msg) { - System.err.println(msg); + System.err.println(msg); // NOSONAR } } 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..0e0ef2bf 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-2021 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. @@ -21,10 +21,9 @@ package org.onap.policy.common.logging.flexlogger; import com.att.eelf.configuration.EELFLogger.Level; - import java.io.Serializable; import java.util.UUID; - +import lombok.Getter; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -36,6 +35,7 @@ public class EelfLogger implements Logger, Serializable { private static final long serialVersionUID = 5385586713941277192L; private String className = ""; + @Getter private String transId = UUID.randomUUID().toString(); /** @@ -137,14 +137,6 @@ public class EelfLogger implements Logger, Serializable { } /** - * Returns transaction Id for logging. - */ - @Override - public String getTransId() { - return transId; - } - - /** * Records a message. * * @param message the message @@ -166,6 +158,17 @@ public class EelfLogger implements Logger, Serializable { } /** + * 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. * * @param message the message @@ -210,6 +213,17 @@ public class EelfLogger implements Logger, Serializable { } /** + * 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. * * @param message the message @@ -234,6 +248,17 @@ public class EelfLogger implements Logger, Serializable { * 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. + * + * @param message the message */ @Override public void warn(Object message) { @@ -278,6 +303,17 @@ public class EelfLogger implements Logger, Serializable { * 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. + * + * @param message the message */ @Override public void trace(Object message) { @@ -387,6 +423,17 @@ public class EelfLogger implements Logger, Serializable { } /** + * 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. * * @param eventId the event ID @@ -485,6 +532,17 @@ public class EelfLogger implements Logger, Serializable { } /** + * 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. * * @param transId the transaction ID diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java index 030363dc..6c301712 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +30,6 @@ import java.util.Date; import java.util.Properties; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; - import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.PropertyUtil.Listener; @@ -40,13 +40,12 @@ public class FlexLogger extends SecurityManager { private static final String GET_LOGGER_PREFIX = "FlexLogger:getLogger : loggerType = "; private static LoggerType loggerType = LoggerType.EELF; - private static ConcurrentHashMap<String, Logger4J> logger4JMap = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, EelfLogger> eelfLoggerMap = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, SystemOutLogger> systemOutMap = new ConcurrentHashMap<>(); // --- init logger first static { - loggerType = initlogger(); + loggerType = initLogger(); } /** @@ -55,103 +54,41 @@ public class FlexLogger extends SecurityManager { * @param clazz the class */ public static Logger getLogger(Class<?> clazz) { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(clazz, false); - break; - case LOG4J: - logger = getLog4JLogger(); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; - + return getLogger(clazz, false); } /** * Returns an instance of Logger. - * - * @param name the name of the logger */ - public static Logger getLogger(String name) { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(null, false); - break; - case LOG4J: - logger = getLog4JLogger(name); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; - + public static Logger getLogger() { + return getLogger(null); } /** * Returns an instance of Logger. * - * @param clazz the class + * @param clazz the class * @param isNewTransaction is a new transaction */ public static Logger getLogger(Class<?> clazz, boolean isNewTransaction) { - Logger logger = null; + Logger logger; displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(clazz, isNewTransaction); - break; - case LOG4J: - logger = getLog4JLogger(); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; + if (loggerType == LoggerType.EELF) { + logger = getEelfLogger(clazz, isNewTransaction); + } else { + logger = getSystemOutLogger(); } return logger; - } /** * Returns an instance of Logger. * - * @param name the name of the logger * @param isNewTransaction is a new transaction */ - public static Logger getLogger(String name, boolean isNewTransaction) { - Logger logger = null; - displayMessage(GET_LOGGER_PREFIX + loggerType); - switch (loggerType) { - - case EELF: - logger = getEelfLogger(null, isNewTransaction); - break; - case LOG4J: - logger = getLog4JLogger(name); - break; - case SYSTEMOUT: - default: - logger = getSystemOutLogger(); - break; - } - - return logger; + public static Logger getLogger(boolean isNewTransaction) { + return getLogger(null, isNewTransaction); } /** @@ -163,40 +100,9 @@ public class FlexLogger extends SecurityManager { } /** - * Returns an instance of Logger4J. - */ - private static Logger4J getLog4JLogger() { - String className = new FlexLogger().getClassName(); - - if (!logger4JMap.containsKey(className)) { - // for 1610 release use the default debug.log for log4j - Logger4J logger = new Logger4J("debugLogger", className); - logger4JMap.put(className, logger); - } - - return logger4JMap.get(className); - } - - /** - * Returns an instance of Logger4J. - * - * @param name the name of the logger - */ - private static Logger4J getLog4JLogger(String name) { - String className = new FlexLogger().getClassName(); - - if (!logger4JMap.containsKey(className)) { - Logger4J logger = new Logger4J(name, className); - logger4JMap.put(className, logger); - } - - return logger4JMap.get(className); - } - - /** * Returns an instance of EelfLogger. * - * @param clazz the class + * @param clazz the class * @param isNewTransaction is a new transaction */ private static EelfLogger getEelfLogger(Class<?> clazz, boolean isNewTransaction) { @@ -209,21 +115,13 @@ public class FlexLogger extends SecurityManager { className = new FlexLogger().getClassName(); } - if (!eelfLoggerMap.containsKey(className)) { - logger = new EelfLogger(clazz, isNewTransaction); - eelfLoggerMap.put(className, logger); - } else { - logger = eelfLoggerMap.get(className); - if (logger == null) { - logger = new EelfLogger(clazz, isNewTransaction); - eelfLoggerMap.put(className, logger); - } - // installl already created but it is new transaction - if (isNewTransaction) { - String transId = PolicyLogger.postMdcInfoForEvent(null); - logger.setTransId(transId); - } + logger = eelfLoggerMap.computeIfAbsent(className, key -> new EelfLogger(clazz, isNewTransaction)); + + if (isNewTransaction) { + String transId = PolicyLogger.postMdcInfoForEvent(null); + logger.setTransId(transId); } + displayMessage("eelfLoggerMap size : " + eelfLoggerMap.size() + " class name: " + className); return logger; } @@ -235,37 +133,30 @@ public class FlexLogger extends SecurityManager { String className = new FlexLogger().getClassName(); - if (!systemOutMap.containsKey(className)) { - SystemOutLogger logger = new SystemOutLogger(className); - systemOutMap.put(className, logger); - } - - return systemOutMap.get(className); + return systemOutMap.computeIfAbsent(className, SystemOutLogger::new); } /** * loads the logger properties. */ - private static LoggerType initlogger() { - LoggerType loggerType = LoggerType.EELF; + private static LoggerType initLogger() { + var loggerType = LoggerType.EELF; Properties properties = null; try { properties = PropertyUtil.getProperties("config/policyLogger.properties"); displayMessage("FlexLogger:properties => " + properties); - if (properties != null) { - String overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); - displayMessage("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); - String loggerTypeString = properties.getProperty("logger.type"); - if ("EELF".equalsIgnoreCase(loggerTypeString) && "TRUE".equalsIgnoreCase(overrideLogbackLevel)) { - displayMessage("FlexLogger: start listener."); - properties = PropertyUtil.getProperties("config/policyLogger.properties", - new PropertiesCallBack("FlexLogger-CallBack")); - } + String overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); + displayMessage("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); + var loggerTypeString = properties.getProperty("logger.type"); + if ("EELF".equalsIgnoreCase(loggerTypeString) && "TRUE".equalsIgnoreCase(overrideLogbackLevel)) { + displayMessage("FlexLogger: start listener."); + properties = PropertyUtil.getProperties("config/policyLogger.properties", + new PropertiesCallBack("FlexLogger-CallBack")); } } catch (IOException e1) { - displayMessage("initlogger" + e1); + displayMessage("initLogger" + e1); } finally { // OK to pass no properties (null) loggerType = PolicyLogger.init(properties); @@ -295,10 +186,10 @@ public class FlexLogger extends SecurityManager { String auditLevel = properties.getProperty("audit.level"); String errorLevel = properties.getProperty("error.level"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); - Instant startTime = Instant.now(); - String formatedTime = sdf.format(Date.from(startTime)); - displayMessage("FlexLogger.propertiesChanged : called at time : " + formatedTime); + var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); + var startTime = Instant.now(); + String formattedTime = sdf.format(Date.from(startTime)); + displayMessage("FlexLogger.propertiesChanged : called at time : " + formattedTime); displayMessage("FlexLogger.propertiesChanged : debugLevel : " + debugLevel); if (changedKeys != null) { 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..ad155a4c 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. @@ -21,7 +21,6 @@ package org.onap.policy.common.logging.flexlogger; import java.util.UUID; - import org.onap.policy.common.logging.eelf.MessageCodes; /** @@ -41,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 */ public void error(Object message); @@ -61,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 */ public void info(Object message); @@ -71,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 */ public void warn(Object message); @@ -91,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 */ public void trace(Object message); @@ -111,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 */ public void recordAuditEventStart(String eventId); @@ -157,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. */ public boolean isDebugEnabled(); 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 deleted file mode 100644 index 8802d17e..00000000 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java +++ /dev/null @@ -1,514 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP-Logging - * ================================================================================ - * Copyright (C) 2017-2019 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.logging.flexlogger; - -import static org.onap.policy.common.logging.flexlogger.DisplayUtils.displayMessage; - -import com.att.eelf.configuration.EELFLogger.Level; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.Arrays; -import java.util.UUID; - -import org.apache.log4j.Logger; -import org.apache.log4j.Priority; -import org.onap.policy.common.logging.eelf.MessageCodes; -import org.onap.policy.common.logging.eelf.PolicyLogger; - -/** - * Logger4J implements all the methods of interface Logger by calling org.apache.log4j.Logger - */ -public class Logger4J implements org.onap.policy.common.logging.flexlogger.Logger, Serializable { - - private static final long serialVersionUID = 3183729429888828471L; - private Logger log = null; - private String methodName = ""; - private String className = ""; - private String transId = UUID.randomUUID().toString(); - - /** - * Constructor. - * - * @param clazz the class - */ - public Logger4J(Class<?> clazz) { - displayMessage("create instance of Logger4J"); - if (clazz != null) { - log = Logger.getLogger(clazz); - className = clazz.getName(); - } - } - - /** - * Constructor. - * - * @param name the name of the logger - * @param className the name of the class - */ - public Logger4J(String name, String className) { - displayMessage("create instance of Logger4J"); - if (name != null) { - log = Logger.getLogger(name); - } - this.className = className; - } - - /** - * Sets transaction Id. - */ - @Override - public void setTransId(String transId) { - log.info(transId); - this.transId = transId; - } - - /** - * Returns transaction Id. - */ - @Override - public String getTransId() { - return transId; - } - - /** - * Records a message. - * - * @param message the message - */ - @Override - public void debug(Object message) { - if (isDebugEnabled()) { - log.debug(transId + "|" + message); - } - } - - /** - * Records a message. - * - * @param message the message - * @param throwable the throwable - */ - @Override - public void debug(Object message, Throwable throwable) { - log.debug(message, throwable); - } - - /** - * Records an error message. - * - * @param message the message - */ - @Override - public void error(Object message) { - log.error(transId + "|" + className + "|" + message); - } - - /** - * Records an error message. - * - * @param message the message - * @param throwable the throwable - */ - @Override - public void error(Object message, Throwable throwable) { - log.error(message, throwable); - } - - /** - * Records an error message. - * - * @param msg the message code - * @param throwable the throwable - * @param arguments the messages - */ - @Override - public void error(MessageCodes msg, Throwable throwable, String... arguments) { - log.error(transId + "|" + className + "|" + "MessageCodes :" + msg + Arrays.asList(arguments)); - - } - - /** - * Records an error message. - * - * @param msg the message code - * @param arguments the messages - */ - @Override - public void error(MessageCodes msg, String... arguments) { - log.error(transId + "|" + className + "|" + "MessageCode:" + msg + Arrays.asList(arguments)); - } - - /** - * Records a message. - * - * @param message the message - */ - @Override - public void info(Object message) { - log.info(transId + "|" + className + "|" + message); - } - - /** - * Records a message. - * - * @param message the message - * @param throwable the throwable - */ - @Override - public void info(Object message, Throwable throwable) { - log.info(message, throwable); - } - - /** - * Records a message. - * - * @param message the message - */ - @Override - public void warn(Object message) { - log.warn(transId + "|" + className + "|" + message); - } - - /** - * Records a message. - * - * @param message the message - * @param throwable the throwable - */ - @Override - public void warn(Object message, Throwable throwable) { - log.warn(message, throwable); - } - - /** - * Records a message. - * - * @param msg the message code - * @param arguments the messages - */ - @Override - public void warn(MessageCodes msg, String... arguments) { - log.warn(className + "|" + "MessageCodes:" + msg + Arrays.asList(arguments)); - } - - /** - * Records a message. - * - * @param msg the message code - * @param throwable the throwable - * @param arguments the messages - */ - @Override - public void warn(MessageCodes msg, Throwable throwable, String... arguments) { - log.warn(className + "|" + "MessageCodes:" + msg + Arrays.asList(arguments)); - } - - /** - * Records a message. - * - * @param message the message - */ - @Override - public void trace(Object message) { - log.trace(transId + "|" + className + "|" + message); - } - - /** - * Records a message. - * - * @param message the message - * @param throwable the throwable - */ - @Override - public void trace(Object message, Throwable throwable) { - log.trace(message, throwable); - } - - /** - * Returns true for debug enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isDebugEnabled() { - return log.isDebugEnabled(); - } - - /** - * Returns true for error enabled, or false for not. - * - * @return boolean - */ - @SuppressWarnings("deprecation") - @Override - public boolean isErrorEnabled() { - return log.isEnabledFor(Priority.ERROR); - } - - /** - * Returns true for info enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isInfoEnabled() { - return log.isInfoEnabled(); - } - - /** - * Returns true for warn enabled, or false for not. - * - * @return boolean - */ - @SuppressWarnings("deprecation") - @Override - public boolean isWarnEnabled() { - // return log4j value - return log.isEnabledFor(Priority.WARN); - } - - /** - * Returns true for audit enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isAuditEnabled() { - return (PolicyLogger.getAuditLevel() != Level.OFF); - } - - /** - * Returns true for metrics enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isMetricsEnabled() { - return (PolicyLogger.getMetricsLevel() != Level.OFF); - } - - /** - * Records an audit message. - * - * @param message the message - */ - @Override - public void audit(Object message) { - log.info(className + "|" + message); - } - - /** - * Records an audit message. - * - * @param message the message - * @param throwable the throwable - */ - - @Override - public void audit(Object message, Throwable throwable) { - log.info(message, throwable); - } - - /** - * Records an audit message. - * - * @param eventId the event ID - */ - @Override - public void recordAuditEventStart(String eventId) { - log.info(className + "|recordAuditEventStart with eventId " + eventId); - } - - /** - * Records an audit message. - * - * @param eventId the event ID - */ - @Override - public void recordAuditEventStart(UUID eventId) { - if (eventId != null) { - recordAuditEventStart(eventId.toString()); - } - } - - /** - * Records an audit message. - * - * @param eventId the event ID - * @param rule the rule - * @param policyVersion the policy version - */ - @Override - public void recordAuditEventEnd(String eventId, String rule, String policyVersion) { - log.info(className + "|" + eventId + ":" + rule); - } - - /** - * Records an audit message. - * - * @param eventId the event ID - * @param rule the rule - * @param policyVersion the policy version - */ - @Override - public void recordAuditEventEnd(UUID eventId, String rule, String policyVersion) { - if (eventId != null) { - recordAuditEventEnd(eventId.toString(), rule, policyVersion); - } else { - recordAuditEventEnd(eventId, rule, policyVersion); - } - } - - /** - * Records an audit message. - * - * @param eventId the event ID - * @param rule the rule - */ - @Override - public void recordAuditEventEnd(String eventId, String rule) { - log.info(className + "|" + eventId + ":" + rule); - } - - /** - * Records an audit message. - * - * @param eventId the event ID - * @param rule the rule - */ - @Override - public void recordAuditEventEnd(UUID eventId, String rule) { - if (eventId != null) { - recordAuditEventEnd(eventId.toString(), rule); - } else { - recordAuditEventEnd(eventId, rule); - } - } - - /** - * Records a metrics message. - * - * @param eventId the event ID - * @param message the message - */ - @Override - public void recordMetricEvent(String eventId, String message) { - log.info(className + "|" + eventId + ":" + message); - - } - - /** - * Records a metrics message. - * - * @param eventId the event ID - * @param message the message - */ - @Override - public void recordMetricEvent(UUID eventId, String message) { - if (eventId != null) { - recordMetricEvent(eventId.toString(), message); - } else { - recordMetricEvent(eventId, message); - } - } - - /** - * Records a metrics message. - * - * @param message the message - */ - @Override - public void metrics(Object message) { - log.info(message); - } - - /** - * Returns transaction Id. - * - * @param transId the transaction ID - */ - @Override - public String postMdcInfoForEvent(String transId) { - String transactionId = transId; - if (transactionId == null || transactionId.isEmpty()) { - transactionId = UUID.randomUUID().toString(); - } - - return transactionId; - } - - /** - * Records transaction Id. - * - * @param message the message - */ - @Override - public void postMdcInfoForEvent(Object message) { - log.info(message); - } - - /** - * Returns true for trace enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isTraceEnabled() { - return log.isTraceEnabled(); - } - - /** - * Records transaction Id. - * - * @param transId the transaction ID - */ - @Override - public void postMdcInfoForTriggeredRule(String transId) { - log.info(transId); - } - - /* ============================================================ */ - - /* - * Support for 'Serializable' -- the default rules don't work for the 'log' field - */ - - private void writeObject(ObjectOutputStream out) throws IOException { - // write out 'methodName', 'className', 'transId' strings - out.writeObject(methodName); - out.writeObject(className); - out.writeObject(transId); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - - // read in 'methodName', 'className', 'transId' strings - methodName = (String) (in.readObject()); - className = (String) (in.readObject()); - transId = (String) (in.readObject()); - - // look up associated logger - log = Logger.getLogger(className); - } -} diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/LoggerType.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/LoggerType.java index 8bf0dd78..f73bc4bc 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/LoggerType.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/LoggerType.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2018 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,5 +24,5 @@ package org.onap.policy.common.logging.flexlogger; * Logger types. */ public enum LoggerType { - EELF, LOG4J, SYSTEMOUT + EELF, SYSTEMOUT } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java index 38759bc2..ef6c2e95 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -32,13 +32,17 @@ import java.util.Properties; import java.util.Set; import java.util.Timer; import java.util.TimerTask; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; /** * This class provides utilities to read properties from a properties file, and optionally get * notifications of future changes. */ -public class PropertyUtil { +public final class PropertyUtil { + + @NoArgsConstructor(access = AccessLevel.PRIVATE) protected static class LazyHolder { /** * Timer thread. Will not be allocated by the JVM until it is first referenced. @@ -60,17 +64,13 @@ public class PropertyUtil { */ public static Properties getProperties(File file) throws IOException { // create an InputStream (may throw a FileNotFoundException) - FileInputStream fis = new FileInputStream(file); - try { + try (var fis = new FileInputStream(file)) { // create the properties instance - Properties rval = new Properties(); + var rval = new Properties(); // load properties (may throw an IOException) rval.load(fis); return rval; - } finally { - // close input stream - fis.close(); } } @@ -202,7 +202,7 @@ public class PropertyUtil { lastModified = timestamp; // Save old set, and initial set of changed properties. - Properties oldProperties = properties; + var oldProperties = properties; HashSet<String> changedProperties = new HashSet<>(oldProperties.stringPropertyNames()); // Fetch the list of listeners that we will potentially notify, @@ -238,7 +238,7 @@ public class PropertyUtil { for (final Listener notify : listeners) { // Copy 'properties' and 'changedProperties', so it doesn't // cause problems if the recipient makes changes. - final Properties tmpProperties = (Properties) (properties.clone()); + final var tmpProperties = (Properties) (properties.clone()); final HashSet<String> tmpChangedProperties = new HashSet<>(changedProperties); // Do the notification in a separate thread, so blocking @@ -276,7 +276,7 @@ public class PropertyUtil { // Convert the file to a canonical form in order to avoid the situation // where different names refer to the same file. - File tempFile = file.getCanonicalFile(); + var tempFile = file.getCanonicalFile(); // See if there is an existing registration. The 'synchronized' block // is needed to handle the case where a new listener is added at about 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..d16a1d7a 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-2021 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. @@ -23,27 +23,28 @@ package org.onap.policy.common.logging.flexlogger; import static org.onap.policy.common.logging.flexlogger.DisplayUtils.displayMessage; import com.att.eelf.configuration.EELFLogger.Level; - import java.io.Serializable; import java.util.Arrays; import java.util.UUID; - +import lombok.Getter; +import org.onap.policy.common.logging.OnapLoggingUtils; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; /** * SystemOutLogger implements all the methods of interface Logger by calling System.out.println */ +@Getter public class SystemOutLogger implements Logger, Serializable { private static final long serialVersionUID = 4956408061058933929L; private String className = ""; - private boolean isDebugEnabled = true; - private boolean isInfoEnabled = true; - private boolean isWarnEnabled = true; - private boolean isErrorEnabled = true; - private boolean isAuditEnabled = true; - private boolean isMetricsEnabled = true; + private boolean debugEnabled = true; + private boolean infoEnabled = true; + private boolean warnEnabled = true; + private boolean errorEnabled = true; + private boolean auditEnabled = true; + private boolean metricsEnabled = true; private String transId = UUID.randomUUID().toString(); /** @@ -78,35 +79,35 @@ public class SystemOutLogger implements Logger, Serializable { private void initLevel() { if (PolicyLogger.getDebugLevel() == Level.DEBUG) { - isDebugEnabled = true; - isInfoEnabled = true; - isWarnEnabled = true; + debugEnabled = true; + infoEnabled = true; + warnEnabled = true; } else { - isDebugEnabled = false; + debugEnabled = false; } if (PolicyLogger.getDebugLevel() == Level.INFO) { - isInfoEnabled = true; - isWarnEnabled = true; - isDebugEnabled = false; + infoEnabled = true; + warnEnabled = true; + debugEnabled = false; } if (PolicyLogger.getDebugLevel() == Level.OFF) { - isInfoEnabled = false; - isWarnEnabled = false; - isDebugEnabled = false; + infoEnabled = false; + warnEnabled = false; + debugEnabled = false; } if (PolicyLogger.getErrorLevel() == Level.OFF) { - isErrorEnabled = false; + errorEnabled = false; } if (PolicyLogger.getAuditLevel() == Level.OFF) { - isAuditEnabled = false; + auditEnabled = false; } if (PolicyLogger.getMetricsLevel() == Level.OFF) { - isMetricsEnabled = false; + metricsEnabled = false; } } @@ -121,15 +122,6 @@ public class SystemOutLogger implements Logger, Serializable { } /** - * Returns transaction Id. - */ - @Override - public String getTransId() { - - return transId; - } - - /** * Records a message. * * @param message the message @@ -152,6 +144,21 @@ public class SystemOutLogger implements Logger, Serializable { } /** + * 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. * * @param message the message @@ -193,181 +200,177 @@ public class SystemOutLogger implements Logger, Serializable { */ @Override public void error(MessageCodes msg, String... arguments) { - displayMessage(transId + "|" + className + " : " + "MessageCode:" + msg + Arrays.asList(arguments)); } /** - * Records a message. + * Records a error message. * * @param message the message + * @param arguments variable number of arguments */ @Override - public void info(Object message) { - displayMessage(transId + "|" + className + " : " + message); + 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. * * @param message the message - * @param throwable the throwable */ @Override - public void info(Object message, Throwable throwable) { - displayMessage(transId + "|" + className + " : " + message + ":" + throwable); + public void info(Object message) { + displayMessage(transId + "|" + className + " : " + message); } /** * Records a message. * * @param message the message + * @param throwable the throwable */ @Override - public void warn(Object message) { - displayMessage(transId + "|" + className + " : " + message); + public void info(Object message, Throwable throwable) { + displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } /** * Records a message. * * @param message the message - * @param throwable the throwable + * @param arguments variable number of arguments */ @Override - public void warn(Object message, Throwable throwable) { - displayMessage(transId + "|" + className + " : " + message + ":" + throwable); + 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 msg the message code - * @param arguments the messages + * @param message the message */ @Override - public void warn(MessageCodes msg, String... arguments) { - - displayMessage(transId + "|" + className + " : " + "MessageCodes:" + msg + Arrays.asList(arguments)); + public void warn(Object message) { + displayMessage(transId + "|" + className + " : " + message); } /** * Records a message. * * @param msg the message code - * @param throwable the throwable * @param arguments the messages */ @Override - public void warn(MessageCodes msg, Throwable throwable, String... arguments) { + public void warn(MessageCodes msg, String... arguments) { displayMessage(transId + "|" + className + " : " + "MessageCodes:" + msg + Arrays.asList(arguments)); - } /** * Records a message. * * @param message the message + * @param throwable the throwable */ @Override - public void trace(Object message) { - displayMessage(transId + "|" + className + " : " + message); + public void warn(Object message, Throwable throwable) { + displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } /** * Records a message. * - * @param message the message + * @param msg the message code * @param throwable the throwable + * @param arguments the messages */ @Override - public void trace(Object message, Throwable throwable) { - displayMessage(transId + "|" + className + " : " + message + ":" + throwable); - } + public void warn(MessageCodes msg, Throwable throwable, String... arguments) { - /** - * Returns true for debug enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isDebugEnabled() { - return isDebugEnabled; - } + displayMessage(transId + "|" + className + " : " + "MessageCodes:" + msg + Arrays.asList(arguments)); - /** - * Returns true for warn enabled, or false for not. - * - * @return boolean - */ - @Override - public boolean isWarnEnabled() { - return isWarnEnabled; } /** - * Returns true for info enabled, or false for not. + * Records a message. * - * @return boolean + * @param message the message + * @param arguments variable number of arguments */ @Override - public boolean isInfoEnabled() { - return isInfoEnabled; + 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)); + } } /** - * Returns true for error enabled, or false for not. + * Records a message. * - * @return boolean + * @param message the message */ @Override - public boolean isErrorEnabled() { - return isErrorEnabled; + public void trace(Object message) { + displayMessage(transId + "|" + className + " : " + message); } /** - * Returns true for audit enabled, or false for not. + * Records a message. * - * @return boolean + * @param message the message + * @param throwable the throwable */ @Override - public boolean isAuditEnabled() { - - return isAuditEnabled; + public void trace(Object message, Throwable throwable) { + displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } /** - * Returns true for metrics enabled, or false for not. + * Records an audit message. * - * @return boolean + * @param message the message */ @Override - public boolean isMetricsEnabled() { + public void audit(Object message) { - return isMetricsEnabled; + displayMessage(transId + "|" + className + " : " + message); } /** * Records an audit message. * * @param message the message + * @param throwable the throwable */ @Override - public void audit(Object message) { - - displayMessage(transId + "|" + className + " : " + message); + public void audit(Object message, Throwable throwable) { + displayMessage(transId + "|" + className + " : " + message + ":" + throwable); } /** * Records an audit message. * * @param message the message - * @param throwable the throwable */ @Override - public void audit(Object message, Throwable throwable) { - displayMessage(transId + "|" + className + " : " + message + ":" + throwable); + public void audit(String message, Object... arguments) { + if (arguments.length == 1) { + displayMessage(transId + "|" + className + " : " + message + ":" + arguments[0]); + } else { + displayMessage(OnapLoggingUtils.formatMessage(message, arguments)); + } } /** @@ -480,6 +483,21 @@ public class SystemOutLogger implements Logger, Serializable { } /** + * 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. * * @param transId the transaction ID |