DEV Community

Matthias Langer
Matthias Langer

Posted on

more-log4j2-2.1.0 with improved test support has been released

I have invested quite some time writing an asynchronous HTTP appender, that can be used to push logs to various observability platforms. This appender was released under the Apache License as part of more-log4j2-2.0.0 about 2 weeks ago. One of my personal use cases is ingesting logs from locally executed unit tests. And while that works nicely with the previous release already, I discovered two problems, that are addressed in more-log4j2-2.1.0:

  1. Some of you might use the io.github.hakky54:logcaptor library. This library is very helpful if you want to have assertions on your log output, however, there is a catch: The library relies on logback, and thereby blocks you from using more-log4j2 for your tests. more-log4j2-2.1.0 addresses this problem by reimplementing the LogCaptor API for log4j2. A few small tweaks to your log4j2-test.xml and switching your imports from nl.altindag.log.LogCaptor to com.github.mlangc.more.log4j2.captor.LogCaptor should be enough. In some cases trivial refactorings might be necessary, since I didn't clone the nl.altindag.log.model classes, but choose to expose the log4j2 APIs directly.
  2. Spring Boot users might stumble over logs being dropped on test shutdown. Spring Boot normally takes care of shutting down the logger context, and therefore installs a property source, that unconditionally disables the log4j2 shutdown-hook. Unfortunately this affects also tests that are completely independent of Spring, since the SpringBootPropertySource is installed automatically as soon as it's on the classpath. Once installed, setting log4j2.shutdownHookEnabled has no effect, since the SpringBootPropertySource gives itself a higher priority than the SystemPropertiesPropertySource and the EnvironmentPropertySource which are shipped with log4j2. The new more-log4j2-junit-2.1.0 module addresses this problem for Junit tests, by providing a TestExecutionListener that flushes AsyncHttpAppender instances when tests have finished. This listener is installed automatically once on the runtime classpath.

Any feedback is highly appreciated.

Top comments (0)