Sometimes you will need to test for content of log output. For instance, you are testing some service deep inside the stack and do not have a direct access to it.

Configure a Log4j Logger

Create a file (presuming a Maven file structure):


with the following content:


This logger simply drops everything in STDIO.

Maven will place the src/test/resources at the top of the classpath for tests but will not package into the final artifact, so your real Log4j configuration is safe.

Writing a spec

  • Line 3: will start intercepting and accumulating all output to System.out.
  • Line 5: get all log output as a single String object
  • Line 6: splitting the log output into individual log lines
  • Line 7: asserting the value in a specific line
  • Line 8: restorin the STDIO back to its natural state for all other tests that follow.


The same technique can be done with SystemStreamUtil.getSystemErr() and SystemStreamUtil.restoreSystemErr() respectively.

Do not forget to restore the STDIO or STDERR output, otherwise you will not see much of log output.

How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page