3
votes

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

1
Try to rename log4j2.xml to log4j2-spring.xml. According to documentation if you're using standard one spring does not have full control over it docs.spring.io/spring-boot/docs/current/reference/html/…Sasha Shpota
Hi Oleksandr, Thanks for your comment. I have tried log4j2.xml and log4j2-spring.xml both. I have added my github link in q's. Please have a look.Shishir
This issue is already answered here stackoverflow.com/a/14877698/5055762Khwaja Moiz
BTW java option -Dlogs.location=<path/to/my/logs> has to be before -jar option - everything after -jar <file>.jar will be considered an application argument and not a java option - therefore use instead java -Dlogs.location=<path/to/my/logs> -jar my-app.jarFrk

1 Answers

2
votes

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