log4j之DailyRollingFileAppender源码 重写文件名后缀

DailyRollingFileAppende每天(yyyy-MM-dd)生成一个日志文件,但是当天的文件没有日期后缀。试着重写了一下他的方法。

主要注意这个方法public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)  throws IOException

还有这个void rollOver() throws IOException

【注】有个问题没有解决,当服务器将时间调向过去,日志不会写到那天的日志文件里,而是会写到最新的日志文件里;系统时间调向将来,没有问题。

/**
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.  */



package com.kong.trinet.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import com.kong.trinet.util.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/***
   DailyRollingFileAppender extends {@link FileAppender} so that the
   underlying file is rolled over at a user chosen frequency.

   <p>The rolling schedule is specified by the <b>DatePattern</b>
   option. This pattern should follow the {@link SimpleDateFormat}
   conventions. In particular, you <em>must</em> escape literal text
   within a pair of single quotes. A formatted version of the date
   pattern is used as the suffix for the rolled file name.

   <p>For example, if the <b>File</b> option is set to
   <code>/foo/bar.log</code> and the <b>DatePattern</b> set to
   <code>'.'yyyy-MM-dd</code>, on 2001-02-16 at midnight, the logging
   file <code>/foo/bar.log</code> will be copied to
   <code>/foo/bar.log.2001-02-16</code> and logging for 2001-02-17
   will continue in <code>/foo/bar.log</code> until it rolls over
   the next day.

   <p>Is is possible to specify monthly, weekly, half-daily, daily,
   hourly, or minutely rollover schedules.

   <p><table border="1" cellpadding="2">
   <tr>
   <th>DatePattern</th>
   <th>Rollover schedule</th>
   <th>Example</th>

   <tr>
   <td><code>'.'yyyy-MM</code>
   <td>Rollover at the beginning of each month</td>

   <td>At midnight of May 31st, 2002 <code>/foo/bar.log</code> will be
   copied to <code>/foo/bar.log.2002-05</code>. Logging for the month
   of June will be output to <code>/foo/bar.log</code> until it is
   also rolled over the next month.

   <tr>
   <td><code>'.'yyyy-ww</code>

   <td>Rollover at the first day of each week. The first day of the
   week depends on the locale.</td>

   <td>Assuming the first day of the week is Sunday, on Saturday
   midnight, June 9th 2002, the file <i>/foo/bar.log</i> will be
   copied to <i>/foo/bar.log.2002-23</i>.  Logging for the 24th week
   of 2002 will be output to <code>/foo/bar.log</code> until it is
   rolled over the next week.

   <tr>
   <td><code>'.'yyyy-MM-dd</code>

   <td>Rollover at midnight each day.</td>

   <td>At midnight, on March 8th, 2002, <code>/foo/bar.log</code> will
   be copied to <code>/foo/bar.log.2002-03-08</code>. Logging for the
   9th day of March will be output to <code>/foo/bar.log</code> until
   it is rolled over the next day.

   <tr>
   <td><code>'.'yyyy-MM-dd-a</code>

   <td>Rollover at midnight and midday of each day.</td>

   <td>At noon, on March 9th, 2002, <code>/foo/bar.log</code> will be
   copied to <code>/foo/bar.log.2002-03-09-AM</code>. Logging for the
   afternoon of the 9th will be output to <code>/foo/bar.log</code>
   until it is rolled over at midnight.

   <tr>
   <td><code>'.'yyyy-MM-dd-HH</code>

   <td>Rollover at the top of every hour.</td>

   <td>At approximately 11:00.000 o'clock on March 9th, 2002,
   <code>/foo/bar.log</code> will be copied to
   <code>/foo/bar.log.2002-03-09-10</code>. Logging for the 11th hour
   of the 9th of March will be output to <code>/foo/bar.log</code>
   until it is rolled over at the beginning of the next hour.


   <tr>
   <td><code>'.'yyyy-MM-dd-HH-mm</code>

   <td>Rollover at the beginning of every minute.</td>

   <td>At approximately 11:23,000, on March 9th, 2001,
   <code>/foo/bar.log</code> will be copied to
   <code>/foo/bar.log.2001-03-09-10-22</code>. Logging for the minute
   of 11:23 (9th of March) will be output to
   <code>/foo/bar.log</cod
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
log4j是一个日志管理工具,其中DailyRollingFileAppender和RollingFileAppender都是log4j中的文件输出器,用于将日志输出到文件。 DailyRollingFileAppender按照时间切割日志文件,例如每天或每小时生成一个新的日志文件,以避免单个日志文件过大。使用DailyRollingFileAppender时,需要指定日志文件的文件名、日期格式和日志文件最大大小等参数。 例如,下面的配置将在每天生成一个新的日志文件,并将日志文件名设置为"app.log": ``` log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender log4j.appender.myAppender.File=app.log log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd log4j.appender.myAppender.Threshold=DEBUG log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout log4j.appender.myAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n ``` RollingFileAppender也是按照一定的规则切割日志文件,但是与DailyRollingFileAppender不同的是,RollingFileAppender是按照日志文件大小来切割日志文件。当一个日志文件达到指定的大小时,将自动创建一个新的日志文件,并将日志输出到新的文件中。使用RollingFileAppender时,需要指定日志文件的文件名、最大文件大小、最大备份数量等参数。 例如,下面的配置将在每个日志文件达到100MB时生成一个新的日志文件,最多保留5个备份: ``` log4j.appender.myAppender=org.apache.log4j.RollingFileAppender log4j.appender.myAppender.File=app.log log4j.appender.myAppender.MaxFileSize=100MB log4j.appender.myAppender.MaxBackupIndex=5 log4j.appender.myAppender.Threshold=DEBUG log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout log4j.appender.myAppender.layout.ConversionPattern=%d [%t] %-5p %c - %m%n ``` 总的来说,DailyRollingFileAppender和RollingFileAppender都是log4j中非常实用的日志文件输出器,可以根据实际需求选择使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值