如何使用Spring Boot/slf4j在日志文件的名称中包含日期? [英] How to include date in log file's name with Spring Boot / slf4j?

查看:1116
本文介绍了如何使用Spring Boot/slf4j在日志文件的名称中包含日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

application.properties

application.properties

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

推荐答案

Spring Boot具有LoggingSystem抽象,该抽象试图基于类路径的内容来配置日志记录.

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

雇用

最简单的方法是通过都依赖于spring-boot-starter-logging的启动程序poms.对于Web应用程序,您只需要spring-boot-starter-web,因为它暂时取决于日志记录启动程序.

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,所以它是首选.

Because Logback is available it is the first choice.

要配置日志记录系统的更细粒度的设置,您需要使用所讨论的LoggingSystem支持的本机配置格式.默认情况下,Spring Boot从系统的默认位置(例如,用于Logback的classpath:logback.xml)中拾取本机配置,但是您可以使用"logging.config"属性设置配置文件的位置.

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并添加相应的文件附加程序即可,例如

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

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

可以在此处

这篇关于如何使用Spring Boot/slf4j在日志文件的名称中包含日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆