Hello Amanda,

See my comments inline below.

On Tue, 6 Aug 2024 at 01:05, Amanda Liu <as...@terpmail.umd.edu> wrote:
> The reason I want a custom appender is to pretty print the JSON log
> objects produced by JsonTemplateLayout, to make it more readable.

Would you mind giving us a little bit more context, please? Why do you need
the JSON output to be readable?

> Is there any strong reason why JsonTemplateLayout does
> not support JSON pretty print?

Yes, efficiency – both from computation and bandwidth usage perspectives.
Plus, JTL output is not intended to be consumed by humans, but machines.
The recommended practice is to contain multiple logging configurations for
different use cases. That is, while developing locally, you can choose a
layout producing a more human-readable output, e.g., Pattern Layout. At
production, you can switch to JTL. You can implement such an
environment-specific Log4j configuration either using your framework
best-practices (e.g., Spring Boot has excellent support for these kinds of
scenarios) or Log4j itself (e.g., using arbiters). To guide you on choosing
the right solution, we need to know more about your context: Which
framework do you use? How do you deploy your application? etc.

> This would resolve my issue, and I think other log4j
> JsonTemplateLayout users may be interested in the pretty print feature.

I am not keen on adding such a feature without a use case, of which we
haven't been told so far.

On Tue, Aug 13, 2024 at 8:52 PM Piotr P. Karwasz <piotr.karw...@gmail.com>
wrote:

> Hi Amanda,
>
> I am crossposting this to dev@logging.apache.org. Please answer to
> that mailing list.
>
> On Tue, 6 Aug 2024 at 01:05, Amanda Liu <as...@terpmail.umd.edu> wrote:
> > 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.
>
> There is no special reason why `JsonTemplateLayout` does not support
> pretty print. It was simply never implemented.
>
> Currently JTL only writes to a small `JsonWriter`[1] class through
> `TemplateResolver.resolve()`[2]. This could be easily generalized
> (probably in `3.x`) by replacing `JsonWriter` with an interface, to
> support multiple serialization formats. There is already a JIRA issue
> for that (see LOG4J2-3082[0]).
>
> Admittedly this requires quite some work, but it is within the grasp
> of a semester-long student project or Google Summer of Code.
>
> If you are willing to contribute such a feature, we can always assist
> you with the Log4j-related details.
>
> Piotr
>
> BTW: I added more info about shading applications that use Log4j Core
> to our documentation[3]
>
> [0] https://issues.apache.org/jira/browse/LOG4J2-3082
> [1]
> https://javadoc.io/static/org.apache.logging.log4j/log4j-layout-template-json/2.19.0/org/apache/logging/log4j/layout/template/json/util/JsonWriter.html
> [2]
> https://javadoc.io/static/org.apache.logging.log4j/log4j-layout-template-json/2.19.0/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolver.html#resolve-V-org.apache.logging.log4j.layout.template.json.util.JsonWriter-
> [3]
> https://github.com/apache/logging-log4j2/blob/2.x/src/site/antora/modules/ROOT/pages/faq.adoc#how-do-i-create-a-single-jar-application-containing-log4j-core
>

Reply via email to