diff options
author | uj426b <uj426b@att.com> | 2020-06-11 13:32:53 -0400 |
---|---|---|
committer | uj426b <uj426b@att.com> | 2020-06-11 15:55:28 -0400 |
commit | 679ce33d39364e13489832852ddcda67d95daa20 (patch) | |
tree | ff51ac42bd03a812c767b39ff70d79d828a7558d /common-logging/src | |
parent | b3b8c72936adbc82f6ae4377f6c18dcaa30e4dc0 (diff) |
Removing log4j from policy/common
Change-Id: Ib662556533f72c99180b611d0a1933f20e231b2c
Issue-ID: POLICY-2623
Signed-off-by: uj426b <uj426b@att.com>
Diffstat (limited to 'common-logging/src')
6 files changed, 4 insertions, 1038 deletions
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 8de057f9..7bc2c1dc 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 @@ -1389,10 +1389,6 @@ public class PolicyLogger { switch (loggerTypeProp.toUpperCase()) { case "EELF": return LoggerType.EELF; - - case "LOG4J": - return LoggerType.LOG4J; - case "SYSTEMOUT": return LoggerType.SYSTEMOUT; 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 4217421e..62459bcc 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 @@ -40,7 +40,6 @@ 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<>(); @@ -62,9 +61,6 @@ public class FlexLogger extends SecurityManager { case EELF: logger = getEelfLogger(clazz, false); break; - case LOG4J: - logger = getLog4JLogger(); - break; case SYSTEMOUT: default: logger = getSystemOutLogger(); @@ -88,9 +84,6 @@ public class FlexLogger extends SecurityManager { case EELF: logger = getEelfLogger(null, false); break; - case LOG4J: - logger = getLog4JLogger(name); - break; case SYSTEMOUT: default: logger = getSystemOutLogger(); @@ -115,9 +108,6 @@ public class FlexLogger extends SecurityManager { case EELF: logger = getEelfLogger(clazz, isNewTransaction); break; - case LOG4J: - logger = getLog4JLogger(); - break; case SYSTEMOUT: default: logger = getSystemOutLogger(); @@ -142,9 +132,6 @@ public class FlexLogger extends SecurityManager { case EELF: logger = getEelfLogger(null, isNewTransaction); break; - case LOG4J: - logger = getLog4JLogger(name); - break; case SYSTEMOUT: default: logger = getSystemOutLogger(); @@ -163,37 +150,6 @@ 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 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 bb5e1142..00000000 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/Logger4J.java +++ /dev/null @@ -1,607 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP-Logging - * ================================================================================ - * 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. - * 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.OnapLoggingUtils; -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 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. - * - * @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 - * @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. - * - * @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 - * @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. - * - * @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 - * @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. - * - * @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 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. - * - * @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); - } - - /** - * 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. - * - * @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.writeUTF(methodName); - out.writeUTF(className); - out.writeUTF(transId); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - - // read in 'methodName', 'className', 'transId' strings - methodName = in.readUTF(); - className = in.readUTF(); - transId = in.readUTF(); - - // 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/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java index 500c484f..f43c85df 100644 --- a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java +++ b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/FlexLoggerTest.java @@ -43,13 +43,6 @@ public class FlexLoggerTest { } @Test - public void testGetLoggerClassOfQLog4j() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.LOG4J); - Logger logger = FlexLogger.getLogger(this.getClass()); - assertSame(logger, FlexLogger.getLogger(this.getClass())); - } - - @Test public void testGetLoggerClassOfQSystemOut() { Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(this.getClass()); @@ -64,13 +57,6 @@ public class FlexLoggerTest { } @Test - public void testGetLoggerStringLog4j() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.LOG4J); - Logger logger = FlexLogger.getLogger("str1"); - assertSame(logger, FlexLogger.getLogger("str1")); - } - - @Test public void testGetLoggerStringSystemOut() { Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger("str1"); @@ -85,13 +71,6 @@ public class FlexLoggerTest { } @Test - public void testGetLoggerClassOfQBooleanLog4j() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.LOG4J); - Logger logger = FlexLogger.getLogger(this.getClass(), true); - assertSame(logger, FlexLogger.getLogger(this.getClass(), true)); - } - - @Test public void testGetLoggerClassOfQBooleanSystemOut() { Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger(this.getClass(), true); @@ -106,13 +85,6 @@ public class FlexLoggerTest { } @Test - public void testGetLoggerStringBooleanLog4j() { - Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.LOG4J); - Logger logger = FlexLogger.getLogger("str1", true); - assertSame(logger, FlexLogger.getLogger("str1", true)); - } - - @Test public void testGetLoggerStringBooleanSystemOut() { Whitebox.setInternalState(FlexLogger.class, "loggerType", LoggerType.SYSTEMOUT); Logger logger = FlexLogger.getLogger("str1", true); diff --git a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java b/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java deleted file mode 100644 index 9ceef99c..00000000 --- a/common-logging/src/test/java/org/onap/policy/common/logging/flexlogger/Logger4JTest.java +++ /dev/null @@ -1,351 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP-Logging - * ================================================================================ - * Copyright (C) 2018 Ericsson. 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. - * 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.assertj.core.api.Assertions.assertThatCode; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.UUID; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.policy.common.logging.eelf.MessageCodes; -import org.powermock.reflect.Whitebox; - -public class Logger4JTest { - - private Logger4J logger4J = new Logger4J("str1", "Logger4JTest"); - - @Test - public void testLogger4JClassOfQ() { - assertThatCode(() -> new Logger4J(this.getClass())).doesNotThrowAnyException(); - } - - @Test - public void testSetAndGetTransId() { - logger4J.setTransId("transactionId"); - assertEquals("transactionId", logger4J.getTransId()); - } - - @Test - public void testDebugObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Mockito.when(logger.isDebugEnabled()).thenReturn(true); - logger4J.setTransId("transactionId"); - logger4J.debug("message"); - Mockito.verify(logger).debug("transactionId|message"); - } - - @Test - public void testErrorObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.error("message"); - Mockito.verify(logger).error("transactionId|Logger4JTest|message"); - } - - @Test - public void testInfoObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.info("message"); - Mockito.verify(logger).info("transactionId|Logger4JTest|message"); - } - - @Test - public void testWarnObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.warn("message"); - Mockito.verify(logger).warn("transactionId|Logger4JTest|message"); - } - - @Test - public void testTraceObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.trace("message"); - Mockito.verify(logger).trace("transactionId|Logger4JTest|message"); - } - - @Test - public void testIsDebugEnabled() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Mockito.when(logger.isDebugEnabled()).thenReturn(true).thenReturn(false); - assertTrue(logger4J.isDebugEnabled()); - assertFalse(logger4J.isDebugEnabled()); - } - - @Test - public void testIsErrorEnabled() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Mockito.when(logger.isEnabledFor(Level.ERROR)).thenReturn(true).thenReturn(false); - assertTrue(logger4J.isErrorEnabled()); - assertFalse(logger4J.isErrorEnabled()); - } - - @Test - public void testIsInfoEnabled() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Mockito.when(logger.isInfoEnabled()).thenReturn(true).thenReturn(false); - assertTrue(logger4J.isInfoEnabled()); - assertFalse(logger4J.isInfoEnabled()); - } - - @Test - public void testIsWarnEnabled() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Mockito.when(logger.isEnabledFor(Level.WARN)).thenReturn(true).thenReturn(false); - assertTrue(logger4J.isWarnEnabled()); - assertFalse(logger4J.isWarnEnabled()); - } - - @Test - public void testAuditObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.audit("str1"); - Mockito.verify(logger).info("Logger4JTest|str1"); - } - - @Test - public void testRecordAuditEventStartString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.recordAuditEventEnd("eventId", "rule"); - Mockito.verify(logger).info("Logger4JTest|eventId:rule"); - } - - @Test - public void testRecordAuditEventStartUuid() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - UUID uuid = UUID.randomUUID(); - logger4J.recordAuditEventStart(uuid); - Mockito.verify(logger).info("Logger4JTest|recordAuditEventStart with eventId " + uuid.toString()); - } - - @Test - public void testRecordAuditEventEndStringStringString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.recordAuditEventEnd("eventId", "rule", "policyVersion"); - Mockito.verify(logger).info("Logger4JTest|eventId:rule"); - } - - @Test - public void testRecordAuditEventEndUuidStringString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - UUID uuid = UUID.randomUUID(); - logger4J.recordAuditEventEnd(uuid, "rule", "policyVersion"); - Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":rule"); - } - - @Test - public void testRecordAuditEventEndStringString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.recordAuditEventEnd("eventId", "rule"); - Mockito.verify(logger).info("Logger4JTest|eventId:rule"); - } - - @Test - public void testRecordAuditEventEndUuidString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - UUID uuid = UUID.randomUUID(); - logger4J.recordAuditEventEnd(uuid, "rule"); - Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":rule"); - } - - @Test - public void testRecordMetricEventStringString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.recordMetricEvent("eventId", "str1"); - Mockito.verify(logger).info("Logger4JTest|eventId:str1"); - } - - @Test - public void testRecordMetricEventUuidString() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - UUID uuid = UUID.randomUUID(); - logger4J.recordMetricEvent(uuid, "str1"); - Mockito.verify(logger).info("Logger4JTest|" + uuid.toString() + ":str1"); - } - - @Test - public void testMetrics() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.metrics("str1"); - Mockito.verify(logger).info("str1"); - } - - @Test - public void testErrorMessageCodesThrowableStringArray() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.error(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); - Mockito.verify(logger) - .error("transactionId|Logger4JTest|MessageCodes :" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); - } - - @Test - public void testErrorMessageCodesStringArray() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.error(MessageCodes.GENERAL_ERROR, "str1", "str2"); - Mockito.verify(logger) - .error("transactionId|Logger4JTest|MessageCode:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); - } - - @Test - public void testPostMdcInfoForEventString() { - String returnedTransactionId = logger4J.postMdcInfoForEvent("transactionId"); - assertEquals("transactionId", returnedTransactionId); - } - - @Test - public void testPostMdcInfoForEventEmptyString() { - String returnedTransactionId = logger4J.postMdcInfoForEvent(""); - assertNotNull("", returnedTransactionId); - assertNotEquals("", returnedTransactionId); - } - - @Test - public void testWarnMessageCodesStringArray() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.warn(MessageCodes.GENERAL_ERROR, "str1", "str2"); - Mockito.verify(logger).warn("Logger4JTest|MessageCodes:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); - } - - @Test - public void testWarnMessageCodesThrowableStringArray() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - logger4J.warn(MessageCodes.GENERAL_ERROR, new NullPointerException(), "str1", "str2"); - Mockito.verify(logger).warn("Logger4JTest|MessageCodes:" + MessageCodes.GENERAL_ERROR + "[str1, str2]"); - } - - @Test - public void testDebugObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - Exception exception = new NullPointerException(); - logger4J.debug("message", exception); - Mockito.verify(logger).debug("message", exception); - } - - @Test - public void testErrorObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - Exception exception = new NullPointerException(); - logger4J.error("message", exception); - Mockito.verify(logger).error("message", exception); - } - - @Test - public void testInfoObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.setTransId("transactionId"); - Exception exception = new NullPointerException(); - logger4J.info("message", exception); - Mockito.verify(logger).info("message", exception); - } - - @Test - public void testWarnObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Exception exception = new NullPointerException(); - logger4J.warn("message", exception); - Mockito.verify(logger).warn("message", exception); - } - - @Test - public void testTraceObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Exception exception = new NullPointerException(); - logger4J.trace("message", exception); - Mockito.verify(logger).trace("message", exception); - } - - @Test - public void testAuditObjectThrowable() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - Exception exception = new NullPointerException(); - logger4J.audit("message", exception); - Mockito.verify(logger).info("message", exception); - } - - @Test - public void testIsTraceEnabled() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.isTraceEnabled(); - Mockito.verify(logger).isTraceEnabled(); - } - - @Test - public void testPostMdcInfoForTriggeredRule() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.postMdcInfoForTriggeredRule("transactionId"); - Mockito.verify(logger).info("transactionId"); - } - - @Test - public void testPostMdcInfoForEventObject() { - Logger logger = Mockito.mock(Logger.class); - Whitebox.setInternalState(logger4J, "log", logger); - logger4J.postMdcInfoForEvent(1); - Mockito.verify(logger).info(1); - } - -} |