gpt4 book ai didi

logback - 有没有办法在 logback 中为单个日志文件定义多个滚动策略?

转载 作者:行者123 更新时间:2023-12-03 21:40:13 27 4
gpt4 key购买 nike

我正在尝试设置一个 logback 配置,该配置创建一个解压缩的日志文件作为第一卷,然后将压缩文件写入不同的目录。我相信最好的方法是在单个 appender 中创建多个 rollingPolicy 元素。任何人都可以指导我举一个有人成功做到这一点的例子吗?

<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>archives/tests.%i.log.zip</fileNamePattern>
<minIndex>2</minIndex>
<maxIndex>5</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

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

最佳答案

这是如何做到的:

<!-- Time and Size based: Roll every day and split big file in smaller peaces -->
<appender name="ROOT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/root.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${OTHER_HOME}/root-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %-5level [%thread] - %mdc{loginName} - [%logger]- %msg%n</pattern>
</encoder>
</appender>
  • ${LOG_HOME} 是您的第一个目录
  • ${OTHER_HOME} 用于压缩文件
  • 关于logback - 有没有办法在 logback 中为单个日志文件定义多个滚动策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21266125/

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