From 67532c8953bc4b61c6e1ba0970d01a3af9333f9e Mon Sep 17 00:00:00 2001 From: schlathoeltt Date: Thu, 7 Sep 2023 15:16:58 +0200 Subject: [PATCH] feat(#973): Allow deactivation of standard LoggingReporter --- .../report/LoggingReporter.java | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/core/citrus-base/src/main/java/org/citrusframework/report/LoggingReporter.java b/core/citrus-base/src/main/java/org/citrusframework/report/LoggingReporter.java index 7246415d2a..a927cf8481 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/report/LoggingReporter.java +++ b/core/citrus-base/src/main/java/org/citrusframework/report/LoggingReporter.java @@ -27,24 +27,40 @@ import org.citrusframework.message.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.helpers.NOPLoggerFactory; import org.springframework.util.StringUtils; /** * Simple logging reporter printing test start and ending to the console/logger. + *

+ * Implementation note: The disablement of the reporter is achieved by using a {@link org.slf4j.helpers.NOPLogger}, + * meaning that this class should primarily focus on logging operations and not extend beyond that functionality. * * @author Christoph Deppisch */ public class LoggingReporter extends AbstractTestReporter implements MessageListener, TestSuiteListener, TestListener, TestActionListener { /** Inbound message logger */ - private static final Logger INBOUND_MSG_LOGGER = LoggerFactory.getLogger("Logger.Message_IN"); + private static Logger inboundMessageLogger = LoggerFactory.getLogger("Logger.Message_IN"); + + /** The inbound message logger used when the reporter is enabled */ + private static final Logger enabledInboundMessageLogger = inboundMessageLogger; /** Outbound message logger */ - private static final Logger OUTBOUND_MSG_LOGGER = LoggerFactory.getLogger("Logger.Message_OUT"); + private static Logger outboundMessageLogger = LoggerFactory.getLogger("Logger.Message_OUT"); + + /** The inbound message logger used when the reporter is enabled */ + private static final Logger enabledOutboundMessageLogger = outboundMessageLogger; /** Logger */ private static Logger log = LoggerFactory.getLogger(LoggingReporter.class); + /** The standard logger used when the reporter is enabled */ + private static final Logger enabledLog = log; + + /** A {@link org.slf4j.helpers.NOPLogger} used in case the reporter is not enabled. */ + private static final Logger noOpLogger = new NOPLoggerFactory().getLogger(LoggingReporter.class.getName()); + @Override public void generate(TestResults testResults) { separator(); @@ -228,12 +244,12 @@ public void onTestActionSkipped(TestCase testCase, TestAction testAction) { @Override public void onInboundMessage(Message message, TestContext context) { - INBOUND_MSG_LOGGER.debug(message.print(context)); + inboundMessageLogger.debug(message.print(context)); } @Override public void onOutboundMessage(Message message, TestContext context) { - OUTBOUND_MSG_LOGGER.debug(message.print(context)); + outboundMessageLogger.debug(message.print(context)); } /** @@ -284,4 +300,23 @@ protected void debug(String line) { protected boolean isDebugEnabled() { return log.isDebugEnabled(); } + + /** + * Sets the enablement state of the reporter. + */ + public void setEnabled(boolean enabled) { + if (enabled) { + log = enabledLog; + inboundMessageLogger = enabledInboundMessageLogger; + outboundMessageLogger = enabledOutboundMessageLogger; + } else { + log = noOpLogger; + inboundMessageLogger = noOpLogger; + outboundMessageLogger = noOpLogger; + } + } + + protected boolean isEnabled() { + return log != noOpLogger; + } }