Is it possible to reference system environment variables (as opposed to Java system properties) in a log4j xml configuration file?
I'd like to be able to do something like:
<level value="${env.LOG_LEVEL}" />
and have it get that from the system environment variables, so I can avoid having to pass in so many things with -D parameters.
System. setProperty("{my. log", "C:/logfile. log");
Locate the log4j. xml file under the oarm/WEB-INF/classes/ directory. Update the log output path for each appender.
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.
I tried to do that recently and couldn't get it to work. What I ended up doing is sending a variable at startup. So say you have an environment variable called $LOG_LEVEL:
<level value="${log_level}" />
and at startup...
java -Dlog_level=$LOG_LEVEL your_app
This syntax is documented only in log4j 2.X so make sure you are using the correct version. It does not work on the 1.X versions.
<Appenders> <File name="file" fileName="${env:LOG_PATH}"> <PatternLayout> <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern> </PatternLayout> </File> </Appenders>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With