从Springboot中的代码自定义日志文件名

发布于 2025-01-26 10:31:39 字数 223 浏览 2 评论 0原文

我有一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2025-02-02 10:31:39

If you are using Log4j2, you can reconfigure it at runtime with

标准log4j2.xml Spring Boot使用的文件(cf. github ) /代码>存储日志文件的位置。因此,您只需要:

  • 将Spring Boot配置为登录到文件(cf. 文档)。例如,您可以添加:

      loggging.file.name = jirital.log
     

    到您的“应用程序”

  • ''

      system.set.setproperty(“ log_file”,“ first.log”);
    configurator.reconfigure();
     

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文