1

In my Spring Boot configuration file, can I specify a logging file location relative to the Spring Boot jar instead of relative to the directory the jar is started from?

Example:

#application.yml
logging:
  file: ./logs/app.log

Directories:

  • /app/dir/app.jar
  • /random/dir

Start:

cd /random/dir
java -jar /app/dir/app.jar

Result:

  • /random/dir/logs/app.log

Desired:

  • /app/dir/logs/app.log

Additional Info: The jar will be running on different operating systems. So the jar path could be similar to:

  • /app/dir/app.jar
  • C:\app\dir\app.jar

At run time, I can calculate what the jar/install directory is. Is there a Java solution that would allow me to set the logging.file location after startup? Or is the logger already initialized at that point?

Complete Solution: based on @satyesht

#application.yml

app:
  log:
    #Set a default directory in case a user "double clicks" the jar to launch it.
    dir: ./logs
    file: app.log

logging:
  file: ${app.log.dir}/${app.log.file}

Start:

cd /random/dir
java -Dapp.log.dir=/app/dir/logs -jar /app/dir/app.jar

2 Answers 2

2

One simple way would be to pass the log directory as a system property as below .

#application.yml
logging:
  file: ${LOGDIR}/logs/app.log

and while running your application you can specify the LOGDIR system property as a VM argument

cd /random/dir
java -jar /app/dir/app.jar -DLOGDIR=/app/dir/
1
  • java -DLOGDIR=/app/dir -jar /app/dir/app.jar Commented Feb 27, 2019 at 18:51
0

You would have to know the location of the jarfile ahead of time. But yes, the difference is between absolute, and relative paths.

Using . indicates that you want to start from the location where our console window is open. This is a path that is relative to the location you are in the terminal.

Using / indicates that you want to start at the root of the drive. Now, obviousally you don't want to store the file in the root of the drive. But let's say i Had my jar in the folder: /app/dir/app.jar Then configuration is as simple as saying:

#application.yml
logging:
  file: /app/dir/logs/app.log
1
  • Thanks, I added "Addition Info" to my original post to clarify that the jar will be running on different OS's at different paths. So an absolute path isn't an option. Unless I can do an OS conditional variable in the yaml/properties file. Commented Feb 27, 2019 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.