forked from spring-projects/spring-boot-data-geode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide DelegatingAppender class to allow SLF4J logging provider Appe…
…nders to be injected [programmatically] at configuration/runtime. This is very useful in Integration Tests where the test class needs to assert log statements in infrastructure, system, framework or application code. Resolves spring-projectsgh-73.
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...ing/src/main/java/org/springframework/geode/logging/slf4j/logback/DelegatingAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.geode.logging.slf4j.logback; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import ch.qos.logback.core.Appender; | ||
import ch.qos.logback.core.AppenderBase; | ||
import ch.qos.logback.core.Context; | ||
import ch.qos.logback.core.helpers.NOPAppender; | ||
|
||
/** | ||
* {@link DelegatingAppender} is an SLF4J {@link Appender} that delegates to the configured {@link Appender}. | ||
* | ||
* If no {@link Appender} was configured, then the {@link DelegatingAppender} delegates to the {@link NOPAppender}. | ||
* | ||
* @author John Blum | ||
* @see ch.qos.logback.core.Appender | ||
* @see ch.qos.logback.core.AppenderBase | ||
* @see ch.qos.logback.core.helpers.NOPAppender | ||
* @since 1.3.0 | ||
*/ | ||
public class DelegatingAppender<T> extends AppenderBase<T> { | ||
|
||
@SuppressWarnings("rawtypes") | ||
protected static final Appender defaultAppender = new NOPAppender<>(); | ||
|
||
public DelegatingAppender() { | ||
|
||
Optional.ofNullable(LoggerFactory.getILoggerFactory()) | ||
.filter(it -> Objects.isNull(defaultAppender.getContext())) | ||
.filter(Context.class::isInstance) | ||
.map(Context.class::cast) | ||
.ifPresent(defaultAppender::setContext); | ||
} | ||
|
||
private volatile Appender<T> appender; | ||
|
||
public void setAppender(Appender<T> appender) { | ||
this.appender = appender; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected Appender<T> getAppender() { | ||
return Optional.ofNullable(this.appender).orElse(defaultAppender); | ||
} | ||
|
||
@Override | ||
protected void append(T eventObject) { | ||
getAppender().doAppend(eventObject); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...est/java/org/springframework/geode/logging/slf4j/logback/DelegatingAppenderUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.geode.logging.slf4j.logback; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.junit.Test; | ||
|
||
import ch.qos.logback.core.Appender; | ||
import ch.qos.logback.core.helpers.NOPAppender; | ||
|
||
/** | ||
* Unit Tests for {@link DelegatingAppender}. | ||
* | ||
* @author John Blum | ||
* @see org.junit.Test | ||
* @see org.mockito.Mock | ||
* @see org.mockito.Mockito | ||
* @see org.springframework.geode.logging.slf4j.logback.DelegatingAppender | ||
* @since 1.3.0 | ||
*/ | ||
public class DelegatingAppenderUnitTests { | ||
|
||
@Test | ||
public void delegatingAppenderDefaultsToNoOpAppender() { | ||
assertThat(new DelegatingAppender<>().getAppender()).isInstanceOf(NOPAppender.class); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public void delegatingAppenderDelegatesToMockAppender() { | ||
|
||
Appender mockAppender = mock(Appender.class); | ||
|
||
DelegatingAppender delegatingAppender = new DelegatingAppender<>(); | ||
|
||
delegatingAppender.setAppender(mockAppender); | ||
|
||
assertThat(delegatingAppender.getAppender()).isSameAs(mockAppender); | ||
|
||
delegatingAppender.append("TEST"); | ||
|
||
verify(mockAppender, times(1)).doAppend(eq("TEST")); | ||
} | ||
} |