• logback中使用日期做为文件目录


    在开发中,有这样一种需求:

    1. 日志每10分钟生成一个文件

    2. 当前所有的日志文件都归档到以今天日期为目录的文件夹中

    那么在logback.xml中该如何配置呢?

    在这个https://www.cnblogs.com/wgslucky/p/10026322.html日志中,已经实现了每隔10分钟生成一个文件的配置,现在需要添加把这些生成的文件按当前日期进行归档。

    修改配置如下:

    <appender name="SYSTEM"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${log.home}/system.log</file>
    
            <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- daily rollover ,每10分钟生成一份日志文件-->
                <fileNamePattern>${log.home}/%d{yyyy-MM-dd,aux}/system.%d{yyyy-MM-dd-HH-mm}.log
                </fileNamePattern>
                  <timeBasedFileNamingAndTriggeringPolicy
                   class="com.common.log.MyTimeBasedFileNamingAndTriggeringPolicy">
                   <multiple>10</multiple>
                    </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>7</maxHistory>
            </rollingPolicy>
    
            <encoder>
                <pattern>%d{HH:mm:ss} %-5level [%thread][%file:%line] : %msg%n
                </pattern>
            </encoder>
        </appender>

    在fileNamePattern的配置中添加一个日期目录:%d{yyyy-MM-dd,aux},最主要的是添加了aux标记,logback文档的原文是这样说明的:

    Multiple %d specifiers
    
    It is possible to specify multiple %d specifiers but only one of which can be primary, i.e. used to infer the rollover period. All other tokens must be marked as auxiliary by passing the 'aux' parameter (see examples below).
    
    Multiple %d specifiers allow you to organize archive files in a folder structure different than that of the roll-over period. For example, the file name pattern shown below organizes log folders by year and month but roll-over log files every day at midnight.
    
    /var/log/%d{yyyy/MM, aux}/myapplication.%d{yyyy-MM-dd}.log

    它的意思是说,可以在fileNamePattern配置中添加多个%d的日期符号,但是只能有一个是主要的,其它的只能做为辅助(auxiliary)。在RollingCalendar类中,日志的文件滚动方式就是根据主%d那个日期判断的。如下面代码所示:

    public RollingCalendar(String datePattern) {
            super();
            this.datePattern = datePattern;
            this.periodicityType = computePeriodicityType();
    }
    
    public PeriodicityType computePeriodicityType() {
    
            GregorianCalendar calendar = new GregorianCalendar(GMT_TIMEZONE, Locale.getDefault());
    
            // set sate to 1970-01-01 00:00:00 GMT
            Date epoch = new Date(0);
    
            if (datePattern != null) {
                for (PeriodicityType i : PeriodicityType.VALID_ORDERED_LIST) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
                    simpleDateFormat.setTimeZone(GMT_TIMEZONE); // all date formatting done in GMT
    
                    String r0 = simpleDateFormat.format(epoch);
    
                    Date next = innerGetEndOfThisPeriod(calendar, i, epoch);
                    String r1 = simpleDateFormat.format(next);
    
                    // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1);
                    if ((r0 != null) && (r1 != null) && !r0.equals(r1)) {
                        return i;
                    }
                }
            }
            // we failed
            return PeriodicityType.ERRONEOUS;
        }
  • 相关阅读:
    POJ1475 Pushing Boxes 华丽丽的双重BFS
    POJ3322 Bloxorz I 无脑广搜(我死了。。。)
    CH2401 送礼物 双向搜索
    POJ2248 Addition Chains 迭代加深
    POJ3074 Sudoku 剪枝深(神?)搜
    Luogu P1120 小木棍 [数据加强版] 来来来我们一起来剪枝,剪枝,剪枝、、、
    Luogu P4095 [HEOI2013]Eden 的新背包问题 思维/动规
    Luogu P5201 [USACO19JAN]Shortcut 最短路树???
    Luogu P5122 [USACO18DEC]Fine Dining 最短路
    Luogu P1608 路径统计 最短路计数
  • 原文地址:https://www.cnblogs.com/wgslucky/p/11454453.html
Copyright © 2020-2023  润新知