Add testing support for logging and capturing log events.

Adds a custom, SLF4J, Logback Appender named 'TestAppender' to listen for and record log events (ILoggingEvent) in a Stack.
This commit is contained in:
John Blum
2019-06-28 16:17:09 -07:00
parent 839a1c650f
commit cb0a0e02ed
3 changed files with 149 additions and 6 deletions

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.data.gemfire.tests.logging.slf4j.logback;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.util.Optional;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.util.StringUtils;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AppenderBase;
/**
* The {@link TestAppender} class is a SLF4J, Logback {@link Appender} implementation used for testing purposes.
*
* @author John Blum
* @see java.util.Stack
* @see ch.qos.logback.core.Appender
* @see ch.qos.logback.core.AppenderBase
* @since 0.0.5.RELEASE
*/
@SuppressWarnings("unused")
public class TestAppender extends AppenderBase<ILoggingEvent> implements Appender<ILoggingEvent> {
private static final AtomicReference<TestAppender> INSTANCE = new AtomicReference<>(null);
private static final Stack<String> logMessages = new Stack<>();
public static TestAppender getInstance() {
return Optional.ofNullable(INSTANCE.get())
.orElseThrow(() -> newIllegalStateException("[%s] was not properly configured",
TestAppender.class.getName()));
}
public TestAppender() {
INSTANCE.compareAndSet(null, this);
}
@Override
protected void append(ILoggingEvent event) {
Optional.ofNullable(event)
.map(ILoggingEvent::getFormattedMessage)
.filter(StringUtils::hasText)
.ifPresent(logMessages::push);
}
public String lastLogMessage() {
synchronized (logMessages) {
return logMessages.empty() ? null : logMessages.pop();
}
}
public void clear() {
logMessages.clear();
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2019 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.data.gemfire.tests.logging.slf4j.logback;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Unit Tests for {@link TestAppender}.
*
* @author John Blum
* @see org.junit.Test
* @see org.slf4j.Logger
* @see org.springframework.data.gemfire.tests.logging.slf4j.logback.TestAppender
* @since 0.0.5.RELEASE
*/
public class TestAppenderUnitTests {
private static final Logger logger = LoggerFactory.getLogger(TestAppenderUnitTests.class);
@Test
public void logEventsAppendedCorrectly() {
TestAppender testAppender = TestAppender.getInstance();
assertThat(testAppender).isNotNull();
assertThat(testAppender.lastLogMessage()).isNull();
LoggableObject object = new LoggableObject();
object.logAnError();
object.logAnInfoMessage();
object.logAWarning();
assertThat(testAppender.lastLogMessage()).isEqualTo("WARN TEST");
assertThat(testAppender.lastLogMessage()).isEqualTo("ERROR TEST");
assertThat(testAppender.lastLogMessage()).isNull();
}
static class LoggableObject {
public void logAnError() {
logger.error("ERROR TEST");
}
public void logAnInfoMessage() {
logger.info("INFO TEST");
}
public void logAWarning() {
logger.warn("WARN TEST");
}
}
}

View File

@@ -11,7 +11,7 @@
<appender name="nop" class="ch.qos.logback.core.helpers.NOPAppender"/>
<appender name="testAppender" class="org.springframework.data.gemfire.test.logging.slf4j.logback.TestAppender">
<appender name="testAppender" class="org.springframework.data.gemfire.tests.logging.slf4j.logback.TestAppender">
<encoder>
<pattern>TEST - %m%n</pattern>
</encoder>
@@ -21,14 +21,10 @@
<logger name="org.springframework" level="${logback.log.level:-ERROR}"/>
<logger name="org.springframework.data.gemfire.config.annotation.support.RegionDataAccessTracingAspect" level="trace" additivity="false">
<logger name="org.springframework.data.gemfire.tests.logging.slf4j.logback.TestAppenderUnitTests" level="WARN">
<appender-ref ref="testAppender"/>
</logger>
<logger name="org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter" level="off" additivity="false"/>
<logger name="org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer" level="off" additivity="false"/>
<root level="${logback.log.level:-ERROR}">
<appender-ref ref="console"/>
</root>