GitHub user mlangc created a discussion: 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][more-log4j2-async-http-appender-ref] was released 
under the Apache License as part  of 
[more-log4j2-2.0.0][more-log4j2-async-http-appender-ref] 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][more-log4j2-github-ref]:

1. Some of you might use the [io.github.hakky54:logcaptor][log-captor-ref] 
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][log-captor-force-logback-ref], and thereby blocks you from using 
`more-log4j2` for your tests.  [more-log4j2-2.1.0][more-log4j2-new-version-ref] 
addresses this problem  [by reimplementing the LogCaptor 
API][more-log4j2-log-captor-ref] 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][log-captor-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][spring-boot-prop-source], that 
unconditionally disables the [log4j2 shutdown-hook][log4j2-shutdown-hook]. 
Unfortunately this affects also tests that are completely independent of 
Spring, since the [SpringBootPropertySource][spring-boot-prop-source] is 
installed automatically as soon as it's on the classpath. Once installed, 
setting [log4j2.shutdownHookEnabled][log4j2-shutdown-hook] has no effect, since 
the `SpringBootPropertySource` gives itself [a higher 
priority][spring-boot-prop-source-prio] than the 
[SystemPropertiesPropertySource][log4j2-system-property-source-prio] and the 
[EnvironmentPropertySource][log4j2-env-property-source-prio] which are shipped 
with `log4j2`. The new [more-log4j2-junit-2.1.0 module][more-log4j2-junit-ref] 
addresses this problem for Junit tests, by providing a [TestExecu
 tionListener][junit-test-exec-listener] that flushes `AsyncHttpAppender` 
instances when tests
   have finished. This listener is installed automatically once on the runtime 
classpath.

Any feedback is highly appreciated.

[junit-test-exec-listener]: 
https://docs.junit.org/6.0.0/api/org.junit.platform.launcher/org/junit/platform/launcher/TestExecutionListener.html

[log-captor-ref]: https://github.com/Hakky54/log-captor

[log-captor-force-logback-ref]: 
https://github.com/Hakky54/log-captor?tab=readme-ov-file#using-log-captor-alongside-with-other-logging-libraries

[log-captor-model]: 
https://github.com/Hakky54/log-captor/tree/master/src/main/java/nl/altindag/log/model

[more-log4j2-new-version-ref]: 
https://central.sonatype.com/artifact/com.github.mlangc/more-log4j2/2.1.0

[more-log4j2-junit-ref]: 
https://github.com/mlangc/more-log4j2?tab=readme-ov-file#more-log4j2-junit

[more-log4j2-github-ref]: https://github.com/mlangc/more-log4j2

[more-log4j2-async-http-appender-ref]: 
https://github.com/mlangc/more-log4j2?tab=readme-ov-file#async-httpappender

[more-log4j2-maven-central-versions-ref]: 
https://central.sonatype.com/artifact/com.github.mlangc/more-log4j2/2.0.0/versions

[more-log4j2-log-captor-ref]: 
https://github.com/mlangc/more-log4j2?tab=readme-ov-file#Capturing-Appender

[spring-boot-prop-source]: 
https://docs.spring.io/spring-boot/api/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.html

[spring-boot-prop-source-prio]: 
https://github.com/spring-projects/spring-boot/blob/27e3ddc7fba34ab9f6bb1cd2e5209fe5d1e42765/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java#L53

[log4j2-shutdown-hook]: 
https://logging.apache.org/log4j/2.x/manual/systemproperties.html#log4j2.shutdownHookEnabled

[log4j2-system-property-source-prio]: 
https://github.com/apache/logging-log4j2/blob/rel/2.25.3/log4j-api/src/main/java/org/apache/logging/log4j/util/SystemPropertiesPropertySource.java#L38

[log4j2-env-property-source-prio]: 
https://github.com/apache/logging-log4j2/blob/2.x/log4j-api/src/main/java/org/apache/logging/log4j/util/EnvironmentPropertySource.java#L40

GitHub link: https://github.com/apache/logging-log4j2/discussions/4035

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to