Can I set Spring to the local location of the boot log programmatically?

Spring Download registration location can be set using property logging.filein 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 should work in different environments that can have different security configurations.

According to the documentation :

Because logging is initialized before creation ApplicationContext, it is not possible to control logging from @PropertySourceswithin Spring files @Configuration. System properties and normal external Spring configuration files work fine.) [Sic]

So, I know that I can’t use the files @Configuration, but can I use some other way, like a static initializer, and still manage the logging location software?

(NOTE: I personally use SLF4J with the Logback protocol, but the Spring Logging Platform is designed to work with various log protocols / implementations and actually uses Commons Logging internally.)

(NOTE 2: Some people have noted this as a duplicate of another question ... but this question seems to talk more about setting up the actual logging implementation, while this question is about Spring Boot configuration. In fact, I cannot immediately determine from these answers how to solve your specific question, as indicated in the comment under the corresponding answer.The answer to this other question focuses mainly on how to get the code to load at the right point in the context initialization, whereas my question is how to set Change logging location.)

+4
source share
1 answer

: Spring

, SpringApplicationInitializer ( ApplicationContextInitializer)

SpringApplication application = new SpringApplication(MySources.class);
application.addInitializers(new LoggingInitializer());
application.run(args);
+1

Source: https://habr.com/ru/post/1628519/


All Articles