You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Stig Rohde Døssing <st...@gmail.com> on 2020/11/20 11:07:16 UTC

Reusing a PatternLayout pattern across several config files

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?

Re: Reusing a PatternLayout pattern across several config files

Posted by Matt Sicker <bo...@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 <st...@gmail.com>
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?
>