logback 当前目录_logback中使用日期做为文件目录

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

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

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

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

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

修改配置如下:

class="ch.qos.logback.core.rolling.RollingFileAppender">

${log.home}/system.log

${log.home}/%d{yyyy-MM-dd,aux}/system.%d{yyyy-MM-dd-HH-mm}.log

10

7

%d{HH:mm:ss} %-5level [%thread][%file:%line] : %msg%n

在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那个日期判断的。如下面代码所示:

publicRollingCalendar(String datePattern) {super();this.datePattern =datePattern;this.periodicityType =computePeriodicityType();

}publicPeriodicityType computePeriodicityType() {

GregorianCalendar calendar= newGregorianCalendar(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= newSimpleDateFormat(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)) {returni;

}

}

}//we failed

returnPeriodicityType.ERRONEOUS;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值