4

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

Shishir
  • 199
  • 3
  • 9
  • 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 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration – Sasha Shpota Mar 17 '17 at 08:43
  • 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 Mar 17 '17 at 13:32
  • This issue is already answered here https://stackoverflow.com/a/14877698/5055762 – Khwaja Moiz Dec 27 '18 at 18:20
  • 1
    BTW java option -Dlogs.location= has to be before -jar option - everything after -jar .jar will be considered an application argument and not a java option - therefore use instead java -Dlogs.location= -jar my-app.jar – Frk Mar 05 '19 at 10:27

1 Answers1

2

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

Khwaja Moiz
  • 89
  • 2
  • 10
  • 1
    Yes, that is exactly how I solved it. In fact, I have used both sys and env based on different use cases. Details about sys/env are : https://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup. I got it working long time back and moved on. It's been ages since I looked at my own questions. Thank you for looking at it and posting your answer. – Shishir Dec 31 '18 at 18:12
  • Awesome! This was very helpful. The properties file equivalent is `property.filePathVar=${sys:filepath:-/logs/app.log}` – RobOhRob Sep 14 '21 at 20:44