'log4j2 replacement for RollingFileAppender that set log directory programmatically

We are upgrading our Java application from log4j 1x to 2x.

In 1x we had an extension to RollingFileAppender specified in log4j.properties. As well as rolling the files, it would compute the path of the logs directory according to whether it was running in JBoss/Tomcat/Webserver, and file the logs there with a specific name.

It is not possible to extend RollingFileAppender in 2x.

My question is: What can I specify in log4j2.properties (or XML if necessary) that will allow me to set the log directory programmatically as before, and roll the files.

It's something like this question, but without having to set up an Environment Variable for every installation, if possible.

Log4j2 customize file path with rollingFileAppender (Java)



Solution 1:[1]

Log4j 2.x is very extensible. In your case you can write a StrLookup that computes the parameters you require and annotate it with:

@Plugin(name="some_prefix", category="Lookup")

The class must be compiled using the annotation processor in log4j-core and can be used in a configuration file as:

${some_prefix:key}

where key is the value that will be passed to StrLookup#lookup.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piotr P. Karwasz