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:
- 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-log4j2for your tests. more-log4j2-2.1.0 addresses this problem by reimplementing the LogCaptor API for log4j2. A few small tweaks to yourlog4j2-test.xmland switching your imports fromnl.altindag.log.LogCaptortocom.github.mlangc.more.log4j2.captor.LogCaptorshould 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. - 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
SpringBootPropertySourcegives itself a higher priority than the SystemPropertiesPropertySource and the EnvironmentPropertySource which are shipped withlog4j2. The new more-log4j2-junit-2.1.0 module addresses this problem for Junit tests, by providing a TestExecutionListener that flushesAsyncHttpAppenderinstances when tests have finished. This listener is installed automatically once on the runtime classpath.
Any feedback is highly appreciated.
Top comments (0)