zoukankan      html  css  js  c++  java
  • [Java][log4j]支持同一时候按日期和文件大小切割日志

    依据DailyRollingFileAppender和RollingFileAppender改编,支持按日期和文件大小切割日志。 
    源文件: 
    Java代码  收藏代码
    1. package com.bao.logging;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.io.Writer;  
    6. import java.text.SimpleDateFormat;  
    7. import java.util.Calendar;  
    8. import java.util.Date;  
    9. import java.util.GregorianCalendar;  
    10. import java.util.Locale;  
    11. import java.util.TimeZone;  
    12.   
    13. import org.apache.log4j.FileAppender;  
    14. import org.apache.log4j.Layout;  
    15. import org.apache.log4j.helpers.CountingQuietWriter;  
    16. import org.apache.log4j.helpers.LogLog;  
    17. import org.apache.log4j.helpers.OptionConverter;  
    18. import org.apache.log4j.spi.LoggingEvent;  
    19.   
    20. /** 
    21.  * MyDailyRollingFileAppender extends {@link FileAppender} so that the underlying 
    22.  * file is rolled over at a user chosen frequency. 
    23.  *  
    24.  * <p> 
    25.  * The rolling schedule is specified by the <b>DatePattern</b> option. This 
    26.  * pattern should follow the {@link SimpleDateFormat} conventions. In 
    27.  * particular, you <em>must</em> escape literal text within a pair of single 
    28.  * quotes. A formatted version of the date pattern is used as the suffix for the 
    29.  * rolled file name. 
    30.  *  
    31.  * <p> 
    32.  * For example, if the <b>File</b> option is set to <code>/foo/bar.log</code> 
    33.  * and the <b>DatePattern</b> set to <code>'.'yyyy-MM-dd</code>, on 2001-02-16 
    34.  * at midnight, the logging file <code>/foo/bar.log</code> will be copied to 
    35.  * <code>/foo/bar.log.2001-02-16</code> and logging for 2001-02-17 will continue 
    36.  * in <code>/foo/bar.log</code> until it rolls over the next day. 
    37.  *  
    38.  * <p> 
    39.  * Is is possible to specify monthly, weekly, half-daily, daily, hourly, or 
    40.  * minutely rollover schedules. 
    41.  *  
    42.  * <p> 
    43.  * <table border="1" cellpadding="2"> 
    44.  * <tr> 
    45.  * <th>DatePattern</th> 
    46.  * <th>Rollover schedule</th> 
    47.  * <th>Example</th> 
    48.  *  
    49.  * <tr> 
    50.  * <td><code>'.'yyyy-MM</code> 
    51.  * <td>Rollover at the beginning of each month</td> 
    52.  *  
    53.  * <td>At midnight of May 31st, 2002 <code>/foo/bar.log</code> will be copied to 
    54.  * <code>/foo/bar.log.2002-05</code>. Logging for the month of June will be 
    55.  * output to <code>/foo/bar.log</code> until it is also rolled over the next 
    56.  * month. 
    57.  *  
    58.  * <tr> 
    59.  * <td><code>'.'yyyy-ww</code> 
    60.  *  
    61.  * <td>Rollover at the first day of each week. The first day of the week depends 
    62.  * on the locale.</td> 
    63.  *  
    64.  * <td>Assuming the first day of the week is Sunday, on Saturday midnight, June 
    65.  * 9th 2002, the file <i>/foo/bar.log</i> will be copied to 
    66.  * <i>/foo/bar.log.2002-23</i>. Logging for the 24th week of 2002 will be output 
    67.  * to <code>/foo/bar.log</code> until it is rolled over the next week. 
    68.  *  
    69.  * <tr> 
    70.  * <td><code>'.'yyyy-MM-dd</code> 
    71.  *  
    72.  * <td>Rollover at midnight each day.</td> 
    73.  *  
    74.  * <td>At midnight, on March 8th, 2002, <code>/foo/bar.log</code> will be copied 
    75.  * to <code>/foo/bar.log.2002-03-08</code>. Logging for the 9th day of March 
    76.  * will be output to <code>/foo/bar.log</code> until it is rolled over the next 
    77.  * day. 
    78.  *  
    79.  * <tr> 
    80.  * <td><code>'.'yyyy-MM-dd-a</code> 
    81.  *  
    82.  * <td>Rollover at midnight and midday of each day.</td> 
    83.  *  
    84.  * <td>At noon, on March 9th, 2002, <code>/foo/bar.log</code> will be copied to 
    85.  * <code>/foo/bar.log.2002-03-09-AM</code>. Logging for the afternoon of the 9th 
    86.  * will be output to <code>/foo/bar.log</code> until it is rolled over at 
    87.  * midnight. 
    88.  *  
    89.  * <tr> 
    90.  * <td><code>'.'yyyy-MM-dd-HH</code> 
    91.  *  
    92.  * <td>Rollover at the top of every hour.</td> 
    93.  *  
    94.  * <td>At approximately 11:00.000 o'clock on March 9th, 2002, 
    95.  * <code>/foo/bar.log</code> will be copied to 
    96.  * <code>/foo/bar.log.2002-03-09-10</code>. Logging for the 11th hour of the 9th 
    97.  * of March will be output to <code>/foo/bar.log</code> until it is rolled over 
    98.  * at the beginning of the next hour. 
    99.  *  
    100.  *  
    101.  * <tr> 
    102.  * <td><code>'.'yyyy-MM-dd-HH-mm</code> 
    103.  *  
    104.  * <td>Rollover at the beginning of every minute.</td> 
    105.  *  
    106.  * <td>At approximately 11:23,000, on March 9th, 2001, <code>/foo/bar.log</code> 
    107.  * will be copied to <code>/foo/bar.log.2001-03-09-10-22</code>. Logging for the 
    108.  * minute of 11:23 (9th of March) will be output to <code>/foo/bar.log</code> 
    109.  * until it is rolled over the next minute. 
    110.  *  
    111.  * </table> 
    112.  *  
    113.  * <p> 
    114.  * Do not use the colon ":" character in anywhere in the <b>DatePattern</b> 
    115.  * option. The text before the colon is interpeted as the protocol specificaion 
    116.  * of a URL which is probably not what you want. 
    117.  */  
    118. public class MyDailyRollingFileAppender extends FileAppender {  
    119.   
    120.     // The code assumes that the following constants are in a increasing  
    121.     // sequence.  
    122.     static final int TOP_OF_TROUBLE = -1;  
    123.     static final int TOP_OF_MINUTE = 0;  
    124.     static final int TOP_OF_HOUR = 1;  
    125.     static final int HALF_DAY = 2;  
    126.     static final int TOP_OF_DAY = 3;  
    127.     static final int TOP_OF_WEEK = 4;  
    128.     static final int TOP_OF_MONTH = 5;  
    129.   
    130.     /** 
    131.      * The default maximum file size is 10MB. 
    132.      */  
    133.     protected long maxFileSize = 10 * 1024 * 1024;  
    134.   
    135.     /** 
    136.      * There is one backup file by default. 
    137.      */  
    138.     protected int maxBackupIndex = 1;  
    139.   
    140.     /** 
    141.      * The date pattern. By default, the pattern is set to "'.'yyyy-MM-dd" 
    142.      * meaning daily rollover. 
    143.      */  
    144.     private String datePattern = "'.'yyyy-MM-dd";  
    145.   
    146.     /** 
    147.      * The log file will be renamed to the value of the scheduledFilename 
    148.      * variable when the next interval is entered. For example, if the rollover 
    149.      * period is one hour, the log file will be renamed to the value of 
    150.      * "scheduledFilename" at the beginning of the next hour. 
    151.      *  
    152.      * The precise time when a rollover occurs depends on logging activity. 
    153.      */  
    154.     private String scheduledFilename;  
    155.   
    156.     /** 
    157.      * The next time we estimate a rollover should occur. 
    158.      */  
    159.     private long nextCheck = System.currentTimeMillis() - 1;  
    160.   
    161.     Date now = new Date();  
    162.   
    163.     SimpleDateFormat sdf;  
    164.   
    165.     RollingCalendar rc = new RollingCalendar();  
    166.   
    167.     int checkPeriod = TOP_OF_TROUBLE;  
    168.   
    169.     // The gmtTimeZone is used only in computeCheckPeriod() method.  
    170.     static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");  
    171.   
    172.     /** 
    173.      * The default constructor does nothing. 
    174.      */  
    175.     public MyDailyRollingFileAppender() {  
    176.     }  
    177.   
    178.     /** 
    179.      * Instantiate a <code>MyDailyRollingFileAppender</code> and open the file 
    180.      * designated by <code>filename</code>. The opened filename will become the 
    181.      * ouput destination for this appender. 
    182.      */  
    183.     public MyDailyRollingFileAppender(Layout layout, String filename,  
    184.             String datePattern) throws IOException {  
    185.         super(layout, filename, true);  
    186.         this.datePattern = datePattern;  
    187.         activateOptions();  
    188.     }  
    189.   
    190.     /** 
    191.      * Get the maximum size that the output file is allowed to reach before 
    192.      * being rolled over to backup files. 
    193.      *  
    194.      * @since 1.1 
    195.      */  
    196.     public long getMaximumFileSize() {  
    197.         return maxFileSize;  
    198.     }  
    199.   
    200.     /** 
    201.      * Set the maximum size that the output file is allowed to reach before 
    202.      * being rolled over to backup files. 
    203.      *  
    204.      * <p> 
    205.      * This method is equivalent to {@link #setMaxFileSize} except that it is 
    206.      * required for differentiating the setter taking a <code>long</code> 
    207.      * argument from the setter taking a <code>String</code> argument by the 
    208.      * JavaBeans {@link java.beans.Introspector Introspector}. 
    209.      *  
    210.      * @see #setMaxFileSize(String) 
    211.      */  
    212.     public void setMaximumFileSize(long maxFileSize) {  
    213.         this.maxFileSize = maxFileSize;  
    214.     }  
    215.   
    216.     /** 
    217.      * Set the maximum size that the output file is allowed to reach before 
    218.      * being rolled over to backup files. 
    219.      *  
    220.      * <p> 
    221.      * In configuration files, the <b>MaxFileSize</b> option takes an long 
    222.      * integer in the range 0 - 2^63. You can specify the value with the 
    223.      * suffixes "KB", "MB" or "GB" so that the integer is interpreted being 
    224.      * expressed respectively in kilobytes, megabytes or gigabytes. For example, 
    225.      * the value "10KB" will be interpreted as 10240. 
    226.      */  
    227.     public void setMaxFileSize(String value) {  
    228.         maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1);  
    229.     }  
    230.   
    231.     /** 
    232.      * Returns the value of the <b>MaxBackupIndex</b> option. 
    233.      */  
    234.     public int getMaxBackupIndex() {  
    235.         return maxBackupIndex;  
    236.     }  
    237.   
    238.     /** 
    239.      * Set the maximum number of backup files to keep around. 
    240.      *  
    241.      * <p> 
    242.      * The <b>MaxBackupIndex</b> option determines how many backup files are 
    243.      * kept before the oldest is erased. This option takes a positive integer 
    244.      * value. If set to zero, then there will be no backup files and the log 
    245.      * file will be truncated when it reaches <code>MaxFileSize</code>. 
    246.      */  
    247.     public void setMaxBackupIndex(int maxBackups) {  
    248.         this.maxBackupIndex = maxBackups;  
    249.     }  
    250.   
    251.     /** 
    252.      * The <b>DatePattern</b> takes a string in the same format as expected by 
    253.      * {@link SimpleDateFormat}. This options determines the rollover schedule. 
    254.      */  
    255.     public void setDatePattern(String pattern) {  
    256.         datePattern = pattern;  
    257.     }  
    258.   
    259.     /** Returns the value of the <b>DatePattern</b> option. */  
    260.     public String getDatePattern() {  
    261.         return datePattern;  
    262.     }  
    263.   
    264.     public void activateOptions() {  
    265.         super.activateOptions();  
    266.         if (datePattern != null && fileName != null) {  
    267.             now.setTime(System.currentTimeMillis());  
    268.             sdf = new SimpleDateFormat(datePattern);  
    269.             int type = computeCheckPeriod();  
    270.             printPeriodicity(type);  
    271.             rc.setType(type);  
    272.             File file = new File(fileName);  
    273.             scheduledFilename = fileName  
    274.                     + sdf.format(new Date(file.lastModified()));  
    275.   
    276.         } else {  
    277.             LogLog.error("Either File or DatePattern options are not set for appender ["  
    278.                     + name + "].");  
    279.         }  
    280.     }  
    281.   
    282.     void printPeriodicity(int type) {  
    283.         switch (type) {  
    284.         case TOP_OF_MINUTE:  
    285.             LogLog.debug("Appender [" + name + "] to be rolled every minute.");  
    286.             break;  
    287.         case TOP_OF_HOUR:  
    288.             LogLog.debug("Appender [" + name  
    289.                     + "] to be rolled on top of every hour.");  
    290.             break;  
    291.         case HALF_DAY:  
    292.             LogLog.debug("Appender [" + name  
    293.                     + "] to be rolled at midday and midnight.");  
    294.             break;  
    295.         case TOP_OF_DAY:  
    296.             LogLog.debug("Appender [" + name + "] to be rolled at midnight.");  
    297.             break;  
    298.         case TOP_OF_WEEK:  
    299.             LogLog.debug("Appender [" + name  
    300.                     + "] to be rolled at start of week.");  
    301.             break;  
    302.         case TOP_OF_MONTH:  
    303.             LogLog.debug("Appender [" + name  
    304.                     + "] to be rolled at start of every month.");  
    305.             break;  
    306.         default:  
    307.             LogLog.warn("Unknown periodicity for appender [" + name + "].");  
    308.         }  
    309.     }  
    310.   
    311.     // This method computes the roll over period by looping over the  
    312.     // periods, starting with the shortest, and stopping when the r0 is  
    313.     // different from from r1, where r0 is the epoch formatted according  
    314.     // the datePattern (supplied by the user) and r1 is the  
    315.     // epoch+nextMillis(i) formatted according to datePattern. All date  
    316.     // formatting is done in GMT and not local format because the test  
    317.     // logic is based on comparisons relative to 1970-01-01 00:00:00  
    318.     // GMT (the epoch).  
    319.   
    320.     int computeCheckPeriod() {  
    321.         RollingCalendar rollingCalendar = new RollingCalendar(gmtTimeZone,  
    322.                 Locale.ENGLISH);  
    323.         // set sate to 1970-01-01 00:00:00 GMT  
    324.         Date epoch = new Date(0);  
    325.         if (datePattern != null) {  
    326.             for (int i = TOP_OF_MINUTE; i <= TOP_OF_MONTH; i++) {  
    327.                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(  
    328.                         datePattern);  
    329.                 simpleDateFormat.setTimeZone(gmtTimeZone); // do all date  
    330.                                                             // formatting in GMT  
    331.                 String r0 = simpleDateFormat.format(epoch);  
    332.                 rollingCalendar.setType(i);  
    333.                 Date next = new Date(rollingCalendar.getNextCheckMillis(epoch));  
    334.                 String r1 = simpleDateFormat.format(next);  
    335.                 // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1);  
    336.                 if (r0 != null && r1 != null && !r0.equals(r1)) {  
    337.                     return i;  
    338.                 }  
    339.             }  
    340.         }  
    341.         return TOP_OF_TROUBLE; // Deliberately head for trouble...  
    342.     }  
    343.   
    344.     /** 
    345.      * Implements the usual roll over behaviour. 
    346.      *  
    347.      * <p> 
    348.      * If <code>MaxBackupIndex</code> is positive, then files { 
    349.      * <code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code> are renamed 
    350.      * to {<code>File.2</code>, ..., <code>File.MaxBackupIndex</code> . 
    351.      * Moreover, <code>File</code> is renamed <code>File.1</code> and closed. A 
    352.      * new <code>File</code> is created to receive further log output. 
    353.      *  
    354.      * <p> 
    355.      * If <code>MaxBackupIndex</code> is equal to zero, then the 
    356.      * <code>File</code> is truncated with no backup files created. 
    357.      */  
    358.     public// synchronization not necessary since doAppend is alreasy synched  
    359.     void sizeRollOver() {  
    360.         File target;  
    361.         File file;  
    362.   
    363.         LogLog.debug("rolling over count="  
    364.                 + ((CountingQuietWriter) qw).getCount());  
    365.         LogLog.debug("maxBackupIndex=" + maxBackupIndex);  
    366.   
    367.         String datedFilename = fileName + sdf.format(now);  
    368.   
    369.         if (maxBackupIndex > 0) {  
    370.             // Delete the oldest file, to keep Windows happy.  
    371.             file = new File(datedFilename + '.' + maxBackupIndex);  
    372.             if (file.exists())  
    373.                 file.delete();  
    374.   
    375.             // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3,  
    376.             // 2}  
    377.             for (int i = maxBackupIndex - 1; i >= 1; i--) {  
    378.                 file = new File(datedFilename + "." + i);  
    379.                 if (file.exists()) {  
    380.                     target = new File(datedFilename + '.' + (i + 1));  
    381.                     LogLog.debug("Renaming file " + file + " to " + target);  
    382.                     file.renameTo(target);  
    383.                 }  
    384.             }  
    385.   
    386.             // Rename fileName to datedFilename.1  
    387.             target = new File(datedFilename + "." + 1);  
    388.   
    389.             this.closeFile(); // keep windows happy.  
    390.   
    391.             file = new File(fileName);  
    392.             LogLog.debug("Renaming file " + file + " to " + target);  
    393.             file.renameTo(target);  
    394.         }else if (maxBackupIndex < 0){//infinite number of files   
    395.             //find the max backup index  
    396.             for (int i = 1; i < Integer.MAX_VALUE; i++) {  
    397.                 target = new File(datedFilename + "." + i);  
    398.                 if (! target.exists()) {//Rename fileName to datedFilename.i  
    399.                     this.closeFile();  
    400.                     file = new File(fileName);  
    401.                     file.renameTo(target);  
    402.                     LogLog.debug("Renaming file " + file + " to " + target);  
    403.                     break;  
    404.                 }  
    405.             }  
    406.         }  
    407.   
    408.         try {  
    409.             // This will also close the file. This is OK since multiple  
    410.             // close operations are safe.  
    411.             this.setFile(fileName, false, bufferedIO, bufferSize);  
    412.         } catch (IOException e) {  
    413.             LogLog.error("setFile(" + fileName + ", false) call failed.", e);  
    414.         }  
    415.         scheduledFilename = datedFilename;  
    416.     }  
    417.   
    418.     public synchronized void setFile(String fileName, boolean append,  
    419.             boolean bufferedIO, int bufferSize) throws IOException {  
    420.         super.setFile(fileName, append, this.bufferedIO, this.bufferSize);  
    421.         if (append) {  
    422.             File f = new File(fileName);  
    423.             ((CountingQuietWriter) qw).setCount(f.length());  
    424.         }  
    425.     }  
    426.   
    427.     protected void setQWForFiles(Writer writer) {  
    428.         this.qw = new CountingQuietWriter(writer, errorHandler);  
    429.     }  
    430.   
    431.     /** 
    432.      * Rollover the current file to a new file. 
    433.      */  
    434.     void timeRollOver() throws IOException {  
    435.   
    436.         /* Compute filename, but only if datePattern is specified */  
    437.         if (datePattern == null) {  
    438.             errorHandler.error("Missing DatePattern option in rollOver().");  
    439.             return;  
    440.         }  
    441.   
    442.         String datedFilename = fileName + sdf.format(now);  
    443.         // It is too early to roll over because we are still within the  
    444.         // bounds of the current interval. Rollover will occur once the  
    445.         // next interval is reached.  
    446.         if (scheduledFilename.equals(datedFilename)) {  
    447.             return;  
    448.         }  
    449.   
    450.         // close current file, and rename it to datedFilename  
    451.         this.closeFile();  
    452.   
    453.         File target = new File(scheduledFilename);  
    454.         if (target.exists()) {  
    455.             target.delete();  
    456.         }  
    457.   
    458.         File file = new File(fileName);  
    459.         boolean result = file.renameTo(target);  
    460.         if (result) {  
    461.             LogLog.debug(fileName + " -> " + scheduledFilename);  
    462.         } else {  
    463.             LogLog.error("Failed to rename [" + fileName + "] to ["  
    464.                     + scheduledFilename + "].");  
    465.         }  
    466.   
    467.         try {  
    468.             // This will also close the file. This is OK since multiple  
    469.             // close operations are safe.  
    470.             super.setFile(fileName, falsethis.bufferedIO, this.bufferSize);  
    471.         } catch (IOException e) {  
    472.             errorHandler.error("setFile(" + fileName + ", false) call failed.");  
    473.         }  
    474.         scheduledFilename = datedFilename;  
    475.     }  
    476.   
    477.     /** 
    478.      * This method differentiates MyDailyRollingFileAppender from its super class. 
    479.      *  
    480.      * <p> 
    481.      * Before actually logging, this method will check whether it is time to do 
    482.      * a rollover. If it is, it will schedule the next rollover time and then 
    483.      * rollover. 
    484.      * */  
    485.     protected void subAppend(LoggingEvent event) {  
    486.         long n = System.currentTimeMillis();  
    487.   
    488.         if (n >= nextCheck) {  
    489.             now.setTime(n);  
    490.             nextCheck = rc.getNextCheckMillis(now);  
    491.             try {  
    492.                 timeRollOver();  
    493.             } catch (IOException ioe) {  
    494.                 LogLog.error("rollOver() failed.", ioe);  
    495.             }  
    496.         } else if ((fileName != null)  
    497.                 && ((CountingQuietWriter) qw).getCount() >= maxFileSize) {  
    498.             sizeRollOver();  
    499.         }  
    500.         super.subAppend(event);  
    501.   
    502.     }  
    503. }  
    504.   
    505. /** 
    506.  * RollingCalendar is a helper class to MyDailyRollingFileAppender. Given a 
    507.  * periodicity type and the current time, it computes the start of the next 
    508.  * interval. 
    509.  * */  
    510. class RollingCalendar extends GregorianCalendar {  
    511.   
    512.     int type = MyDailyRollingFileAppender.TOP_OF_TROUBLE;  
    513.   
    514.     RollingCalendar() {  
    515.         super();  
    516.     }  
    517.   
    518.     RollingCalendar(TimeZone tz, Locale locale) {  
    519.         super(tz, locale);  
    520.     }  
    521.   
    522.     void setType(int type) {  
    523.         this.type = type;  
    524.     }  
    525.   
    526.     public long getNextCheckMillis(Date now) {  
    527.         return getNextCheckDate(now).getTime();  
    528.     }  
    529.   
    530.     public Date getNextCheckDate(Date now) {  
    531.         this.setTime(now);  
    532.   
    533.         switch (type) {  
    534.         case MyDailyRollingFileAppender.TOP_OF_MINUTE:  
    535.             this.set(Calendar.SECOND, 0);  
    536.             this.set(Calendar.MILLISECOND, 0);  
    537.             this.add(Calendar.MINUTE, 1);  
    538.             break;  
    539.         case MyDailyRollingFileAppender.TOP_OF_HOUR:  
    540.             this.set(Calendar.MINUTE, 0);  
    541.             this.set(Calendar.SECOND, 0);  
    542.             this.set(Calendar.MILLISECOND, 0);  
    543.             this.add(Calendar.HOUR_OF_DAY, 1);  
    544.             break;  
    545.         case MyDailyRollingFileAppender.HALF_DAY:  
    546.             this.set(Calendar.MINUTE, 0);  
    547.             this.set(Calendar.SECOND, 0);  
    548.             this.set(Calendar.MILLISECOND, 0);  
    549.             int hour = get(Calendar.HOUR_OF_DAY);  
    550.             if (hour < 12) {  
    551.                 this.set(Calendar.HOUR_OF_DAY, 12);  
    552.             } else {  
    553.                 this.set(Calendar.HOUR_OF_DAY, 0);  
    554.                 this.add(Calendar.DAY_OF_MONTH, 1);  
    555.             }  
    556.             break;  
    557.         case MyDailyRollingFileAppender.TOP_OF_DAY:  
    558.             this.set(Calendar.HOUR_OF_DAY, 0);  
    559.             this.set(Calendar.MINUTE, 0);  
    560.             this.set(Calendar.SECOND, 0);  
    561.             this.set(Calendar.MILLISECOND, 0);  
    562.             this.add(Calendar.DATE, 1);  
    563.             break;  
    564.         case MyDailyRollingFileAppender.TOP_OF_WEEK:  
    565.             this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());  
    566.             this.set(Calendar.HOUR_OF_DAY, 0);  
    567.             this.set(Calendar.SECOND, 0);  
    568.             this.set(Calendar.MILLISECOND, 0);  
    569.             this.add(Calendar.WEEK_OF_YEAR, 1);  
    570.             break;  
    571.         case MyDailyRollingFileAppender.TOP_OF_MONTH:  
    572.             this.set(Calendar.DATE, 1);  
    573.             this.set(Calendar.HOUR_OF_DAY, 0);  
    574.             this.set(Calendar.SECOND, 0);  
    575.             this.set(Calendar.MILLISECOND, 0);  
    576.             this.add(Calendar.MONTH, 1);  
    577.             break;  
    578.         default:  
    579.             throw new IllegalStateException("Unknown periodicity type.");  
    580.         }  
    581.         return getTime();  
    582.     }  
    583. }  

    使用方法: 
    Java代码  收藏代码
    1. <appender name="PROJECT" class="com.bao.logging.MyDailyRollingFileAppender">  
    2.         <param name="file" value="e:/test.log"/>  
    3.         <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />  
    4.         <param name="append" value="true"/>  
    5.         <param name="MaxFileSize" value="500MB"/>  
    6.         <param name="MaxBackupIndex" value="20"/>  
    7. <!--         <param name="MaxBackupIndex" value="-1"/> --><!-- 无限的文件数量,index顺序按时间顺序递增 -->  
    8.         <param name="encoding" value="UTF-8"/>  
    9.         <param name="threshold" value="info"/>  
    10.         <layout class="org.apache.log4j.PatternLayout">  
    11.             <param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS} %-5p] [%t] %c{2} - %m%n"/>  
    12.         </layout>  
    13.     </appender>  
  • 相关阅读:
    关于viewports 设备像素比 密度
    脚本检测 media query 分界点
    chrome Web开放 字体格式不能显示问题
    响应式图片
    ECMAScript 6 proxies
    大小不固定 文字图片居中
    prototype
    基于综合服务平台浅谈Sass应用
    Sass浅谈
    JQ怎么获取margin-left的值
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6934336.html
Copyright © 2011-2022 走看看