Spring Boot's logging location can be set using the logging.file
property in application.properties
. However I want to set the location programmatically. My logic is not based on any Spring beans, only on trial-and-error because the same code has to run across different environments which may have different security configurations.
According to the documentation:
Since logging is initialized before the
ApplicationContext
is created, it isn’t possible to control logging from@PropertySources
in Spring@Configuration
files. System properties and the conventional Spring Boot external configuration files work just fine.) [sic]
So I know I can't use @Configuration
files, but can I use some other way like a static initialiser and still manage to set the logging location programmatically?
(NOTE: I'm personally using SLF4J with Logback, but Spring Boot's logging framework is meant to cope with different logging facades/implementations and in fact uses Commons Logging internally.)
(NOTE 2: Some people have noted this as a duplicate of another question...but that question seems to be talking more about configuring the actual logging implementation, whereas this question is about Spring Boot's own configuration. In fact, I cannot immediately determine from those answers how to solve my specific question, as noted in a comment underneath the relevant answer. The answer to that other question focusses mainly on how to get the code to load at the correct point of context initialization, whereas my question is about how to set the logging location.)
There's another answer: Spring Boot programmatic logging configuration
Basically, it's possible to do that via the SpringApplicationInitializer
(implementing ApplicationContextInitializer)
SpringApplication application = new SpringApplication(MySources.class);
application.addInitializers(new LoggingInitializer());
application.run(args);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With