user1298426
user1298426

Reputation: 3717

How to set logging.file.name through command line arguments in spring boot?

I am setting the logging.file.name in application.properties file which I want to pass as command line argument. Is it possible? The reason is I am trying to run multiple jar files from a single application and I want to display logs of each application run at a single location.

Upvotes: 1

Views: 4117

Answers (1)

knittl
knittl

Reputation: 265211

Spring Boot provides several options to externalize configuration.

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order (with values from lower items overriding earlier ones):

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
  3. Config data (such as application.properties files).
  4. A RandomValuePropertySource that has properties only in random.*.
  5. OS environment variables.
  6. Java System properties (System.getProperties()).
  7. JNDI attributes from java:comp/env.
  8. ServletContext init parameters.
  9. ServletConfig init parameters.
  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  11. Command line arguments.
  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  13. @TestPropertySource annotations on your tests.
  14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

So you should be possible to override the values defined in your application.properties by setting environment variables, Java system properties, or command line arguments:

  • Environmant variables: export LOGGING_FILE_NAME=yourfile.txt
  • Java system property: -Dlogging.file.name=yourfile.txt
  • Command line argument: --logging.file.name=yourfile.txt

Upvotes: 2

Related Questions