有没有一种方法可以在logback中为单个日志文件定义多个滚动策略? [英] Is there a way to define multiple rolling policies for a single log file in logback?

查看:81
本文介绍了有没有一种方法可以在logback中为单个日志文件定义多个滚动策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个先创建一个未压缩日志文件的logback配置,然后再将压缩后的文件写入另一个目录.我相信最好的方法是在一个附加程序中创建多个rollingPolicy元素.谁能指导我举一个成功完成此举的例子?

I am trying to set up a logback configuration that creates an unzipped log file as the first roll, and from then on writes zipped files to a different directory. I believe that the best way to do it is to create multiple rollingPolicy elements within a single appender. Can anyone direct me to an example where someone has successfully done this?

<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中为单个日志文件定义多个滚动策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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