gpt4 book ai didi

java - 如何使用 Spring Boot/slf4j 在日志文件的名称中包含日期?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:17 24 4
gpt4 key购买 nike

Setting a log file name to include current date in Log4j是同一个问题,但是如何应用到slf4j自带的Spring Boot中呢?

应用程序属性

spring.application.name=keywords
logging.file=logs/${spring.application.name}.log

最佳答案

如所述here

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath.

使用它

The simplest way to do that is through the starter poms which all depend on spring-boot-starter-logging. For a web application you only need spring-boot-starter-web since it depends transitively on the logging starter.

enter image description here

因为 Logback 是可用的,所以它是首选。

To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question. By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.

如果默认设置对您来说没问题,只需创建 logback.xml 并添加相应的文件附加程序,例如

<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder>
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
</encoder>
</appender>

可以找到其他文档 here

关于java - 如何使用 Spring Boot/slf4j 在日志文件的名称中包含日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35552647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com