Get the value of ${catalina.base} in log4j2.xml
Asked Answered
F

2

5

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>

Farrell answered 2/4, 2019 at 9:53 Comment(0)
V
5

Try setting a context for catalina.base, i.e.

<Properties>
    <Property name="log-path" >$${sys:catalina.base}</Property>
</Properties>
Votaw answered 2/4, 2019 at 11:4 Comment(3)
Nope. it again created the folder of the same name. @SpodgerFarrell
Oh. That syntax was a change between Log4j 1 and 2, AFAIK.Votaw
Need to use $$ instead of just $ in the propertyPaugh
T
3

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

Trichology answered 2/1, 2022 at 11:18 Comment(1)
Yep, worked with one $ for me too.Sensualism

© 2022 - 2024 — McMap. All rights reserved.