如何使用 Spring Boot / slf4j 在日志文件的名称中包含日期?
How to include date in log file's name with Spring Boot / slf4j?
和Setting a log file name to include current date in Log4j是同一个问题,但是如何应用到Springboot,slf4j自带的?
application.properties
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.
因为 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
和Setting a log file name to include current date in Log4j是同一个问题,但是如何应用到Springboot,slf4j自带的?
application.properties
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 needspring-boot-starter-web
since it depends transitively on the logging starter.
因为 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