zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    How to set up a Headless Chrome Node.js server in Docker
    ozone chromium headless
    编译 chromium 的老版本
    chrome单元测试 单独编译 chromium的Gtest
    HTTP协议header中Content-Disposition中文文件名乱码
    windows 10 cmd 窗口 不支持中文 中文乱码 默认gbk 需要改为utf8 临时修改:CHCP 65001
    ubuntu查看core dumped的详细错误原因
    Ubuntu18.04 图形界面 切换 命令行
    Headless Chromium
    添加chromium mojom调用
  • 原文地址:https://www.cnblogs.com/wgslucky/p/11454453.html
Copyright © 2011-2022 走看看