从Springboot中的代码自定义日志文件名
我有一个简单的Springboot应用程序,上面有休息端点。每次我称之为休息点时,它都会执行一项活动。现在,所有记录信息都访问wildfly中的server.log文件。我想有一个场景,每次调用我的休息终点时,我都可以自定义日志文件的名称。例如,
从A点到B点的所有日志记录信息都应获取一个名为first的文件
。我可以从代码中自定义日志文件的名称,其中我在其中指定每个日志记录信息的名称应转到特定文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
If you are using Log4j2, you can reconfigure it at runtime with
标准
log4j2.xml
Spring Boot使用的文件(cf. github ) /代码>存储日志文件的位置。因此,您只需要:将Spring Boot配置为登录到文件(cf. 文档)。例如,您可以添加:
到您的“应用程序”
''
log4j2都会保证您在重新配置期间不会丢失任何消息( 创建了新文件)。
肯定有类似的记录解决方案,但是记录在重新配置期间可能会丢失消息。
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 propertyLOG_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:
to your `application.properties,
whenever you want to change the name of the log file call
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.