Hi Piotr,

Thank you again for the detailed help! I wanted to ask about an alternative
approach to the issue.

The reason I want a custom appender is to pretty print the JSON log objects
produced by JsonTemplateLayout, to make it more readable. I see in the
log4j docs that JSON pretty print is supported in JsonLayout, but not
JsonTemplateLayout. I don't want to use JsonLayout, since it's deprecated
and doesn't support some other features that JSONTemplateLayout has.

Is there any strong reason why JsonTemplateLayout does not support JSON
pretty print? Or could I contribute to the JsonTemplateLayout file in the
log4j repo to add this feature? (here:
https://github.com/apache/logging-log4j2/blob/a884e991c1a551cb321b2efed770a15fbc55aa2b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java).
This would resolve my issue, and I think other log4j JsonTemplateLayout
users may be interested in the pretty print feature.

Best,
Amanda


On Mon, Aug 5, 2024 at 10:14 AM Piotr P. Karwasz <piotr.karw...@gmail.com>
wrote:

> Hi Amanda,
>
> On Mon, 5 Aug 2024 at 17:35, Amanda Liu <as...@terpmail.umd.edu> wrote:
> > Thanks for your response! I am using SBT build tool, and the JDK version
> is 17. And yes, I do see a `Log4jPlugins.dat` file in my JAR. I see some
> `log4j.core.appender` objects are included in this .dat file, however my
> new custom appender I added is not there.
>
> You must be packaging your application as an Uber JAR / Fat JAR, so
> the contents of your application and all its dependencies end up in a
> flat JAR.
>
> Shading causes problems when multiple JARs contain the same file. When
> such a situation occurs, you must apply some strategy to merge these
> files. The files that might require merging in an application that
> uses Log4j 2 Core are:
>
> 1. The `META-INF/MANIFEST.MF` of the Uber JAR must contain the
> attribute `Multi-Release: true`,
> 2. The contents of all `META-INF/services/<something>` service files
> must be concatenated to obtain a merged
> `META-INF/services/<something>` file,
> 3. Multiple
> `META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat`
> files must be merged together using a specific algorithm. We do
> publish a Maven Shade resource transformer to merge these files [1]
> and the same functionality is offered by a Gradle Plugin [2]. I am not
> aware however of any sbt plugins that do it.
>
> To solve the problem with the merging of `Log4j2Plugins.dat`, you can
> upgrade to version `3.x`, which uses a service file for the same
> purpose.
>
> Could you provide a minimal reproducible sbt example that reproduces
> the issue you are having?
>
> Piotr
>
> [1]
> https://logging.apache.org/log4j/transform/latest/#maven-shade-plugin-extensions
> [2]
> https://github.com/GradleUp/shadow/blob/main/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.groovy
>

Reply via email to