'Get the value of ${catalina.base} in log4j2.xml
Following is my log4j2.xml file. I am trying to put my log files in tomcat. But here it is picking ${catalina.base} literally and creating a folder with the same name i.e ${catalina.base} in the current directory. I have also checked ${catalina.base} is set and it is returning the proper value if I am using a .properties file. How can I get the value of ${catalina.base} in log4j2.xml. Any help is appreciated. I am stuck on it.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Properties>
<Property name="log-path" >${catalina.base}</Property>
</Properties>
<Appenders>
<Console name="Console"
target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />
</Console>
<!--<File name="MyFile"-->
<!--append="true" immediateFlush="true"-->
<!--fileName="${log-path}/tearsLog.log">-->
<!--<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />-->
<!--</File>-->
<RollingFile name="MyRollingFile"
append="true" immediateFlush="true"
fileName="${log-path}/logs/catalina.log"
filePattern="${log-path}/logs/catalina_%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t [%t] (%F:%L) - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy filePattern="${LOG_DIR}/application.%d{dd-MMM-hh}.log.gz" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.visa.dp.ags.probe.api.server" level="debug" additivity="false">
<AppenderRef ref="Console" />
<!--<AppenderRef ref="MyFile" level="trace" />-->
<AppenderRef ref="MyRollingFile" />
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
<!--<AppenderRef ref="MyFile" level="trace" additivity="false" />-->
<AppenderRef ref="MyRollingFile" additivity="false" />
</Root>
</Loggers>
Solution 1:[1]
Try setting a context for catalina.base, i.e.
<Properties>
<Property name="log-path" >$${sys:catalina.base}</Property>
</Properties>
Solution 2:[2]
This is what worked for me with latest log4j2 (2.17.1):
<RollingFile name="MyRollingFile"
append="true" immediateFlush="true"
fileName="${sys:catalina.base}/logs/catalina.log"
filePattern="${sys:catalina.base}/logs/catalina_%d{yyyy-MM-dd}.log">
Just one $, by the way
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 | Felby |
| Solution 2 | mwarren |
