Ashish Bhosle
Ashish Bhosle

Reputation: 657

Change Spring boot logging file in main class

In main class get logging.file value and append passed argument to it. If same Spring Boot application needs to run more than once, we will pass the some argument to the app and based on argument it will create new logging file. Did anyone come across this use case?

Upvotes: 0

Views: 1551

Answers (2)

Rentius2407
Rentius2407

Reputation: 1148

Spring Boot lets you externalize your configuration.

Document Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Inside your app.jar you can have an application.properties file that provides a sensible default property value for "name". When running in a new environment you can launch with a specific command line switch which will override "name". java -jar app.jar --name="Spring"

If you want to override the logging.file value from the application.properties, you can use the following command:

java -jar app.jar --logging.file=thefileName

Edit:

If you want to append to the logging file in the application.properties you can do the following.

application.properties

instance.value=one
logging.file=C:/log/${instance.value}-spring.log

The default log file will be one-spring.log. When running the command java -jar app.jar --instance.value=two it will substitute/override the ${instance.value} with two, two-spring.log

Upvotes: 0

rustyx
rustyx

Reputation: 85541

Just specify logging.file when starting the Spring Boot application. No coding necessary:

$ java -Dlogging.file=file1.log -jar my-app.jar

$ java -Dlogging.file=file2.log -jar my-app.jar

$ java -Dlogging.file=file3.log -jar my-app.jar

If you really want to do it in code, you can do it in main() before starting Spring Boot:

public static void main(String[] args) {
    System.setProperty("logging.file", "file1.log");
    SpringApplication.run(MyApplication.class, args);
}

Upvotes: 1

Related Questions