Skip to content

Commit

Permalink
Merge pull request #8 from dm-drogeriemarkt/depreacte-for-compatibility
Browse files Browse the repository at this point in the history
re-add addAppenderAndSetLogLevelToDebug for compatibility
  • Loading branch information
waschmittel authored Apr 19, 2021
2 parents 48c73f5 + 498a7fc commit 21b1335
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ logCapture
* [Cucumber stepdefs](#cucumber-stepdefs)
* [Cucumber DTOs](#cucumber-dtos)
* [Changes](#changes)
* [3.2.1](#321)
* [3.2.0](#320)
* [3.1.0](#310)
* [3.0.0](#300)
Expand All @@ -41,7 +42,7 @@ Add log-capture as a test dependency to your project. If you use Maven, add this
<dependency>
<groupId>de.dm.infrastructure</groupId>
<artifactId>log-capture</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -283,6 +284,10 @@ public class LogEntry {

## Changes

### 3.2.1

Added deprecated `addAppenderAndSetLogLevelToDebug()` again for compatibility

### 3.2.0

Added fluent API
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.dm.infrastructure</groupId>
<artifactId>log-capture</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.2.1-SNAPSHOT</version>

<name>Log Capture</name>
<description>Makes it possible to capture logs and assert if things have been logged</description>
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/de/dm/infrastructure/logcapture/LogCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ public void addAppenderAndSetLogLevelToTrace() {
setLogLevelToTrace();
}

/**
* delegates to {@link LogCapture#addAppenderAndSetLogLevelToTrace()} for compatibility
*/
@Deprecated
public void addAppenderAndSetLogLevelToDebug() {
addAppenderAndSetLogLevelToTrace();
}

private void setLogLevelToTrace() {
if (originalLogLevels != null) {
throw new IllegalStateException("LogCapture.addAppenderAndSetLogLevelToTrace() should not be called only once or after calling removeAppenderAndResetLogLevel() again.");
}
originalLogLevels = new HashMap<>();
capturedPackages.forEach(packageName -> {
Logger packageLogger = rootLogger.getLoggerContext().getLogger(packageName);
originalLogLevels.put(packageName, packageLogger.getLevel());
Logger packageLogger = rootLogger.getLoggerContext().getLogger(packageName);
originalLogLevels.put(packageName, packageLogger.getLevel());
rootLogger.getLoggerContext().getLogger(packageName).setLevel(Level.TRACE);
}
);
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/de/dm/infrastructure/logcapture/LogCaptureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,20 @@ private void logLevelIsResetTo(Level originalLevel) {

assertThat(comExampleLogger.getLevel()).isEqualTo(originalLevel);
}

private void logLevelIsResetWithDeprecatedMedhod() {
final ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
final ch.qos.logback.classic.Logger comExampleLogger = rootLogger.getLoggerContext().getLogger("com.example");

comExampleLogger.setLevel(INFO);

LogCapture logCapture = LogCapture.forPackages("com.example");
logCapture.addAppenderAndSetLogLevelToDebug();

assertThat(comExampleLogger.getLevel()).isEqualTo(TRACE);

logCapture.removeAppenderAndResetLogLevel();

assertThat(comExampleLogger.getLevel()).isEqualTo(INFO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ void resetWithoutInitializationFails() {
void assertionWithoutInitializationFails() {
assertThrows(IllegalStateException.class, () -> logCapture.assertLogged(INFO, "something"));
}

@Test
void deprecatedAddAppenderDelegates() {
logCapture.addAppenderAndSetLogLevelToDebug();

assertThrows(IllegalStateException.class, () -> logCapture.addAppenderAndSetLogLevelToDebug());
}
}

0 comments on commit 21b1335

Please sign in to comment.