gpt4 book ai didi

java - logback-spring.xml 不会附加文件名

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:07 26 4
gpt4 key购买 nike

这是我第一次在 spring 中使用日志记录,我得到了一个 logback-spring.xml 来使用和调整。这是我当前的 logback-spring.xml 的样子:

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include
resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="logs/my_file_name.log}"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>10</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>

我还在我的 application.properties 中添加了以下内容:

logging.config=config/logback-spring.xml

当我运行该程序时,会自动创建日志目录。但是,根据我的需要和我的 logback-spring.xml 指定,存在的日志被命名为 my_file_name.log 而不是 my_file_name.log.2017-10-25.gz。

为什么会这样?是因为 logback-spring.xml 中的错误还是因为我需要在我的 application.properites 中指定其他内容或在我的 pom.xml 中添加内容?或者当我在不同的日子再次运行这个程序时,日志名称会自动改变吗?

最佳答案

启动时,Logback 将写入 appender/file 定义的文件,当 Logback 中的滚动策略启动时,会将当前日志文件复制到 appender/rollingPolicy/fileNamePattern 建议的文件中

在您的情况下,滚动策略将在一天滚动时启动(这就是您的模式 - %d{yyyy-MM-dd} - 的意思)。所以每天直到第 10 天,Logback 将滚动当前日志文件,在第 10 天,它将滚动当前日志文件丢弃最旧的存档文件,从而仅保留 maxHistory 存档文件。

更多细节 in the docs .

顺便说一句,你的 logback-spring.xml 似乎确实有错误,我怀疑这是......

<property name="LOG_FILE" value="logs/my_file_name.log}"/>

...应该是:

<property name="LOG_FILE" value="logs/my_file_name.log"/>

关于java - logback-spring.xml 不会附加文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46936207/

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