Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User JVM parameter in log4j2 configuration

I have an RollingFile Appender defined in my log4j2.xml.

<RollingFile name="RollingFile" fileName="/logs/app.log"
            filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} - %-5p - %m - [%l]%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
</RollingFile>

What I want to do is to pass this parameter to the JVM at startup:

-Dapp_home=/home/admin/server

The documentation is pretty straight forward. And from what I understand it should work like this:

<RollingFile name="RollingFile" fileName="${jvmrunargs:app_home}/logs/app.log"

But it doesn't. I verified that it is working in general by using the absolut path.

In an other application where I use log4j (1.x) it works like this:

log4j.appender.file.File=${app_home}/logs/app.log
like image 558
Chris Avatar asked Jul 17 '15 19:07

Chris


People also ask

What is configuration status in log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.

How do I reconfigure log4j2 in code with a specific configuration file?

public class MyClass { static { try { InputStream inputStream = new FileInputStream("C:/path/to/log4j2. xml"); ConfigurationSource source = new ConfigurationSource(inputStream); Configurator. initialize(null, source); } catch (Exception ex) { // Handle here } } private static final Logger LOGGER = LoggerFactory.


1 Answers

Take a look at the System Properties Lookup section of the documentation. If you define a variable as system property using -D like this:

-Dapp_home=/home/admin/server

use

${sys:app_home}

in your Log4j 2 configuration to access it.

like image 123
hzpz Avatar answered Sep 25 '22 12:09

hzpz