简体   繁体   中英

Not able to fetch ${catalina.base}, system property not working in log4j2.xml?

Previously we had a log4j 1.x compatible xml format which was having this Rolling file appender configured-

<appender name="ALL" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="${catalina.base}/logs/trw.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="4096KB" />
        <param name="MaxBackupIndex" value="10" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %p [%t] %C{1}:%L - %m%n" />
        </layout>
    </appender>

Now I have changed it to the new log4j2.xml format where based on my research the catalina.base becomes like so -

    <RollingFile name="ALL" fileName="${sys:catalina.base}/logs/trw.log"
                     filePattern="${sys:catalina.base}/logs/trw_%i.log"
                     append ="true">
            <PatternLayout pattern="%d %p [%t] %c{1}:%L - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="4096KB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>

But when I run a test file which uses this logging configuration, the xml file is found and loaded and all is going well but instead of finding the system variable cataline.base it creates a folder ${sys:catalina.base} and puts the log under it. My question is that - is this the expected behaviour? Does log4j2.xml config try to search for the system property and if not found just creates a folder with that name? This config is in a web-application which runs on TomCat 7, spring 3.1 and servlet 2.5. Logs are getting generated as expected but only folder name seems to be the issue.

Found core of the problem thanks to this answer.

Had the same issue with running tests using ${sys:catalina.base} in log4j2.

The 1st problem is that your app running in tomcat while your tests running outside of it. Tomcat itself sets variable catalina.base from CATALINA_BASE on start, so log4j2 instance for tests doesn't know such variable as catalina.base .

The 2nd problem I figured out in my case that I put export CATALINA_BASE="/path/to/catalina/location" in .bashrc instead of .profile .

SOLUTIONS

  1. set absolute path like (for example if your catalina lies in some user directory) "/home/${env:USER}/path/to/cataina/location"

OR

  1. put export CATALINA_BASE="/path/to/catalina/location" in .profile and then you'll be able to set log4j2 property like ${env:CATALINA_BASE} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM