How to intercept SLF4J (with logback) logging via a JUnit test?

The Slf4j API doesn’t provide such a way but Logback provides a simple solution. You can use ListAppender : a whitebox logback appender where log entries are added in a public List field that we could use to make our assertions. Here is a simple example. Foo class : import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class … Read more

Mocking Logger and LoggerFactory with PowerMock and Mockito

EDIT 2020-09-21: Since 3.4.0, Mockito supports mocking static methods, API is still incubating and is likely to change, in particular around stubbing and verification. It requires the mockito-inline artifact. And you don’t need to prepare the test or use any specific runner. All you need to do is : @Test public void name() { try … Read more

Error scanning entry “module-info.class” when starting Jetty server

module-info.class is a Java9 (JPMS) feature. Jetty 9.4.9 (or newer) supports the new JAR file changes from Java 9. It does not matter what Runtime JVM you are using (Oracle Java 8, or even something like OpenJDK 11.0.3), if you are using those JEP-238 Multi-Release Jar files in your WebApp (or Server classloader) then you … Read more

What is the best way to unit-test SLF4J log messages?

Create a test rule: import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.slf4j.LoggerFactory; import java.util.List; import java.util.stream.Collectors; public class LoggerRule implements TestRule { private final ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); private final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); @Override public Statement apply(Statement base, Description description) { return new Statement() { @Override … Read more

Building with Lombok’s @Slf4j and Intellij: Cannot find symbol log

In addition to having Lombok plugin installed, also make sure that the “Enable annotation processing” checkbox is ticked under: Preferences > Compiler > Annotation Processors Note: starting with IntelliJ 2017, the “Enable Annotation Processing” checkbox has moved to: Settings > Build, Execution, Deployment > Compiler > Annotation Processors

Logback to log different messages to two files

It’s very possible to do something like this in logback. Here’s an example configuration: <?xml version=”1.0″?> <configuration> <appender name=”FILE” class=”ch.qos.logback.core.FileAppender”> <file>logfile.log</file> <append>true</append> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} – %msg %n</pattern> </encoder> </appender> <appender name=”ANALYTICS-FILE” class=”ch.qos.logback.core.FileAppender”> <file>analytics.log</file> <append>true</append> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} – %msg %n</pattern> </encoder> </appender> <!– additivity=false ensures analytics data only goes to … Read more

How to disable Spring logging DEBUG messages?

Spring uses commons-logging which auto-detects the logging framework to use. There are various ways to tune which logging framework will be chosen so the first thing to do is to make sure commons-logging binds to log4j. To do that, start your application with an additional flag -Dorg.apache.commons.logging.diagnostics.dest=STDOUT that will output the result of the discovery … Read more