'customize log file name from code in springboot
I have a simple springboot application with rest end points. Every time I call the rest end point, it performs a certain activity. Right now, all the logging information goes to server.log file in wildfly. I would like to have a scenario where i can customize the name of the log file everytime my rest end points are invoked. For e.g.
All logging information from point A to Point B shall goto a file called First.log and all the logging information from point B to Point C shall goto Second.log
Is such an implementation possible either by using logback or log4j2. can i customize the name of the log file from code where in I specify every logging information from this point on should go to a specific file.
Solution 1:[1]
If you are using Log4j2, you can reconfigure it at runtime with Configurator#reconfigure
.
The standard log4j2.xml
file used by Spring Boot (cf. Github) uses the Java system property LOG_FILE
to store the location of the log file. Therefore you just need to:
configure Spring Boot to log to a file (cf. documentation). For example you can add:
logging.file.name = initial.log
to your `application.properties,
whenever you want to change the name of the log file call
System.setProperty("LOG_FILE", "first.log"); Configurator.reconfigure();
Log4j2 guarantees you that no message will be lost during reconfiguration (the old file is closed after the new one has been created).
There is certainly a similar solution for Logback, but Logback can lose messages during reconfiguration.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Piotr P. Karwasz |