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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35552647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:16:27  来源:igfitidea点击:

How to include date in log file's name with Spring Boot / slf4j?

javaspringloggingspring-bootslf4j

提问by sinedsem

It is the same question as Setting a log file name to include current date in Log4j, but how to apply it to Spring Boot, which comes with slf4j?

这与设置日志文件名以在 Log4j 中包含当前日期相同,但如何将其应用于 slf4j 附带的 Spring Boot?

application.properties

应用程序属性

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

采纳答案by Mike Shauneu

As described here

如上所述这里

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

Spring Boot 有一个 LoggingSystem 抽象,它尝试根据类路径的内容配置日志记录。

To employ it

使用它

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-websince it depends transitively on the logging starter.

最简单的方法是通过所有依赖于spring-boot-starter-logging. 对于 Web 应用程序,您只需要spring-boot-starter-web它,因为它传递地依赖于日志记录启动器。

enter image description here

在此处输入图片说明

Because Logback is available it is the first choice.

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

要配置日志系统的更细粒度的设置,您需要使用相关 LoggingSystem 支持的本机配置格式。默认情况下,Spring Boot 从系统的默认位置获取本机配置(例如,用于 Logback 的 classpath:logback.xml),但您可以使用“logging.config”属性设置配置文件的位置。

If default is ok for you just create logback.xmland add corresponding file appender, e.g.

如果默认值对您来说没问题,只需创建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>

Additional documentation could be found here

可以在此处找到其他文档