Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot logging : use of system variables in log4j2.xml

I am running spring boot application as jar.

java -Dlogs.location=<path/to/my/logs> -jar my-app.jar
or 
java -Dlogs.location=<path/to/my/logs> -jar my-app.jar --logs.location=<path/to/my/logs>

Here is a sample log4j2.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration >
    <Properties>
        <Property name="base.log.dir">${sys:logs.location}</Property>
    </Properties>
....
</Configuration>

Spring boot app is creating ${sys:logs.location} folder instead of correctly resolving system properties from jvm args.

Same configuration file working fine with Spring application. I am unable to make logs.location configurable with my custom log4j2.xml file. Any help or suggestion is appreciated.

Please refer this sample project on github

I am using log4j2-spring.xml to configure log4j2.

I have looked at the StackOverflow q's. This answer reads properties bundle. But I want to read sys properties

like image 861
Shishir Avatar asked Oct 20 '25 16:10

Shishir


1 Answers

Define a property like

<Properties>
    <Property name="filePathVar"> ${sys:filepath:-/logs/app.log} </Property>
</Properties>

and use filePathVar like "${filePathVar}" in your xml file and refer this for runtime args - https://stackoverflow.com/a/37439625/5055762

Note - /logs/app.log will be the default value if none is passed as a runtime arg

like image 189
Khwaja Moiz Avatar answered Oct 23 '25 08:10

Khwaja Moiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!