'Change where RollingFile log4j2 writes the log with variables

I need to be able to modify the location where my log4j2.xml file writes log files.

I would like to create a folder at the same level as the .jar ( my/path/app.jar ) of the application to store the logs ( my/path/logs/log1.log ) .

The problem comes in that I can't get where my jar is running from (which can vary) in the file (I don't want to use environment variables if I don't have to). How would you do it ? This is my log4j2.xml file

    <RollingFile name="rollingFile"
        fileName="my/path/application.log"
        filePattern="my/path/application.%d{dd-MMM}.log"
        ignoreExceptions="false">
        <PatternLayout>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" />
        </Policies>
        <DefaultRolloverStrategy>
        <!-- Delete logs older than 60 days -->
            <Delete basePath="my/route/">
                <IfLastModified age="60d" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

I have seen that you can use spring variables but I don't want to implement that cloud dependency ( since I don't use it for anything ).

And within the same question , if in the IfLastModified I add the path of the folder , I should delete the logs ( in this case every 60 days , no? )

context :

  • Java application with spring
  • can change the OS I run the application

Any idea is welcome, thanks !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source