Re: Reusing a PatternLayout pattern across several config files

Matt Sicker <[email protected]>
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <CACmp6krdcEnGFLP6tSa2ktBruN791yWhMV6K2A-rQ2M3t5+PRQ@mail.gmail.com>
That plug-in wrapper pattern will always work. Maybe creating properties to
reference as variables is an option? There might be a way to structure your
configuration to use routing appenders or something like that to minimize
duplication, too.

On Fri, Nov 20, 2020 at 05:07 Stig Rohde Døssing <[email protected]>
wrote:

> Hi,
>
> I have an application that ships with a number of config files included. We
> have a PatternLayout pattern that is used across all configurations, since
> our application needs to parse logs from that certain logs. We currently
> copy-paste the pattern to each file.
>
> I took a look at using properties and XInclude to avoid repeating the
> pattern, but this doesn't work too well with log4j2-test.xml, since that
> file is not located in the same directory as the production log4j2.xml
> files. I would also like to make it easy for customers doing their own
> custom log configs to reference the pattern, and it would be nice if they
> could just refer to the pattern by name.
>
> A bit of a hack I came up with was to programmatically create a named
> PatternLayout by using the PluginFactory mechanism
>
> @Plugin(name = "MyPatternLayout", category = Node.CATEGORY, elementType =
> Layout.ELEMENT_TYPE, printObject = true)
> class MyPatternLayout {
>   @PluginFactory
>   public static PatternLayout createLayout() {
>     PatternLayout.builder.withPattern(myPattern).build()
>   }
> }
>
> This lets me refer to MyPatternLayout rather than including the pattern in
> the config files.
>
> Is this a reasonable way to accomplish reuse, or am I misusing the factory
> mechanism in a way that is likely to break in future releases?
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.