Skip to content

Commit

Permalink
Added option to disable log output to the console (#43)
Browse files Browse the repository at this point in the history
* Added option to disable log output to the console

* Applied sonar recommendation

* Reduced duplicate code

* made console captor test scoped dependency
  • Loading branch information
Hakky54 committed Mar 21, 2023
1 parent aaeb257 commit b11d8fa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<version.lombok>1.18.24</version.lombok>
<version.slf4j>2.0.6</version.slf4j>
<version.log4j-over-slf4j>1.7.36</version.log4j-over-slf4j>
<version.consolecaptor>1.0.3</version.consolecaptor>

<!-- plugin properties -->
<version.jacoco-maven-plugin>0.8.5</version.jacoco-maven-plugin>
Expand Down Expand Up @@ -144,6 +145,12 @@
<version>${version.lombok}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>consolecaptor</artifactId>
<version>${version.consolecaptor}</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/nl/altindag/log/LogCaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
public final class LogCaptor implements AutoCloseable {

private static final Map<String, Level> LOG_LEVEL_CONTAINER = new HashMap<>();
private static final String CONSOLE_APPENDER_NAME = "console";

private final Logger logger;
private final Appender<ILoggingEvent> appender;
Expand Down Expand Up @@ -181,6 +182,28 @@ public void disableLogs() {
logger.setLevel(Level.OFF);
}

/**
* Disables the output of the log entries to the console. To revert this option use {@link LogCaptor#enableConsoleOutput()}.
* LogCaptor will still be capturing the log entries.
*/
public void disableConsoleOutput() {
getConsoleAppender().stop();
}

/**
* The output of the log entries to the console are enabled by default but can be re-enabled if
* they are disabled earlier by {@link LogCaptor#disableConsoleOutput()}
*/
public void enableConsoleOutput() {
getConsoleAppender().start();
}

private Appender<ILoggingEvent> getConsoleAppender() {
return logger.getLoggerContext()
.getLogger(ROOT_LOGGER_NAME)
.getAppender(CONSOLE_APPENDER_NAME);
}

/**
* Resets the log level of the target logger to the initial value which was available before
* changing it with {@link LogCaptor#setLogLevelToInfo()}, {@link LogCaptor#setLogLevelToDebug()} or with {@link LogCaptor#setLogLevelToTrace()}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/nl/altindag/log/LogCaptorShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.spi.FilterReply;
import nl.altindag.console.ConsoleCaptor;
import nl.altindag.log.appender.InMemoryAppender;
import nl.altindag.log.model.LogEvent;
import nl.altindag.log.service.LogMessage;
Expand Down Expand Up @@ -536,6 +537,26 @@ void detachAppenderWithAutoClosable() {
assertThat(fetchAppenders(logger)).isEmpty();
}

@Test
void disableConsoleOutput() {
logCaptor = LogCaptor.forClass(ServiceWithApacheLog4j.class);
logCaptor.disableConsoleOutput();

Service service = new ServiceWithApacheLog4j();
try(ConsoleCaptor consoleCaptor = new ConsoleCaptor()) {
service.sayHello();
assertThat(logCaptor.getLogs()).hasSizeGreaterThan(0);
assertThat(consoleCaptor.getStandardOutput()).isEmpty();

logCaptor.enableConsoleOutput();
logCaptor.clearLogs();

service.sayHello();
assertThat(logCaptor.getLogs()).hasSizeGreaterThan(0);
assertThat(consoleCaptor.getStandardOutput()).hasSizeGreaterThan(0);
}
}

private static void assertListAppender(Logger logger) {
assertThat(fetchAppenders(logger))
.hasSize(1)
Expand Down

0 comments on commit b11d8fa

Please sign in to comment.