java - Spring 启动: Log File in current date directory and rollover

标签 java spring spring-boot logging

我的要求是打印当前日期目录中的日志,并且需要在以下条件下在当前日期目录中滚动日志:

  1. 达到最大文件大小
  2. 或者日期已更改

所以今天的日期是 16/07/2019 所以目录结构应该是

16_07_2019/fde.log(当前日志)

16_07_2019/fde.1.log(由于最大大小)

16_07_2019/fde.2.log(由于最大大小)

15_07_2019/fde.log(昨天的日志)

15_07_2019/fde.1.log(昨天的日志由于最大大小)

我的 logback.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/VVV/AA/%d{yyyy_MM_dd}/fde.log</file>
        <rollingPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>/VVV/AA/%d{yyyy_MM_dd}/fde.%i.log
            </fileNamePattern>
            <maxFileSize>2MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>
</configuration>

不是创建当前日期目录,而是创建 %d{yyyy_MM_dd} 目录。我不知道为什么?

另外,根据我的要求,这个 xml 看起来不错吗?

使用SpringBoot:2.0.5版本

最佳答案

试试这个:

<file>/VVV/AA/logs/fde.log</file>
<rollingPolicy
    class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <!-- rollover daily -->
    <fileNamePattern>/VVV/AA/logs/fde-%d{yyyy_MM_dd}.%i.log</fileNamePattern>
    <maxFileSize>2MB</maxFileSize>
</rollingPolicy>

如仅滚动文件名而不是日志文件目录。

关于java - Spring 启动: Log File in current date directory and rollover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57056532/

相关文章:

java - 将经度、纬度转换为来自 s2 geometry-library python 的 cell id

java - 如何在 java 中使用 setText 更新 FXML 按钮 - 当它返回 java.lang.NullPointerException 错误时?

spring - 动态添加实体到 hibernate

java - 我们需要为 Spring Boot 中的所有内置类创建 bean 吗?

spring-boot - 如何使用 Spring Security 在 Spring Boot 中设置上下文路径

java - Feign Client : Post a Map<String, Object> in Request Body => feign.FeignException: status 400 reading MAp

java - Spring AOP : Only Context Beans Could be Adviced?

java - Spring aop中@Before @After和@AfterExecution有什么区别

java - 使用 JUnit 5 的 spring-boot-starter-test

java - MapReduce - reducer 在一行中发出输出