Skip to content

Commit

Permalink
feat(#973): Allow deactivation of standard LoggingReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
schlathoeltt committed Sep 7, 2023
1 parent 3cf2480 commit 67532c8
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p/>
* 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();
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 67532c8

Please sign in to comment.