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?

Reply via email to