Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2009, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.logmanager.handlers;
 
 import java.util.Date;
 import java.io.File;
 
A file handler which rotates the log at a preset time interval. The interval is determined by the content of the suffix string which is passed in to setSuffix(java.lang.String).
 
 public class PeriodicRotatingFileHandler extends FileHandler {
 
     private SimpleDateFormat format;
     private String nextSuffix;
     private Period period = .;
     private long nextRollover = .;

    
Construct a new instance with no formatter and no output file.
 
     public PeriodicRotatingFileHandler() {
     }

    
Construct a new instance with the given output file.

Parameters:
fileName the file name
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public PeriodicRotatingFileHandler(final String fileNamethrows FileNotFoundException {
         super(fileName);
     }

    
Construct a new instance with the given output file and append setting.

Parameters:
fileName the file name
append true to append, false to overwrite
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public PeriodicRotatingFileHandler(final String fileNamefinal boolean appendthrows FileNotFoundException {
         super(fileNameappend);
     }

    
Construct a new instance with the given output file.

Parameters:
file the file
suffix the format suffix to use
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public PeriodicRotatingFileHandler(final File filefinal String suffixthrows FileNotFoundException {
         super(file);
         setSuffix(suffix);
     }

    
Construct a new instance with the given output file and append setting.

Parameters:
file the file
suffix the format suffix to use
append true to append, false to overwrite
Throws:
java.io.FileNotFoundException if the file could not be found on open
 
     public PeriodicRotatingFileHandler(final File filefinal String suffixfinal boolean appendthrows FileNotFoundException {
         super(fileappend);
         setSuffix(suffix);
     }

    
This implementation checks to see if the scheduled rollover time has yet occurred.
    protected void preWrite(final ExtLogRecord record) {
        final long recordMillis = record.getMillis();
        if (recordMillis >= ) {
            rollOver();
            calcNextRollover(recordMillis);
        }
    }

    
Set the suffix string. The string is in a format which can be understood by java.text.SimpleDateFormat. The period of the rotation is automatically calculated based on the suffix.

Parameters:
suffix the suffix
Throws:
java.lang.IllegalArgumentException if the suffix is not valid
    public void setSuffix(String suffixthrows IllegalArgumentException {
        final SimpleDateFormat format = new SimpleDateFormat(suffix);
        final int len = suffix.length();
        Period period = .;
        for (int i = 0; i < leni ++) {
            switch (suffix.charAt(i)) {
                case 'y'period = min(period.); break;
                case 'M'period = min(period.); break;
                case 'w':
                case 'W'period = min(period.); break;
                case 'D':
                case 'd':
                case 'F':
                case 'E'period = min(period.); break;
                case 'a'period = min(period.); break;
                case 'H':
                case 'k':
                case 'K':
                case 'h'period = min(period.); break;
                case 'm'period = min(period.); break;
                case '\''while (suffix.charAt(++i) != '\''); break;
                case 's':
                case 'S'throw new IllegalArgumentException("Rotating by second or millisecond is not supported");
            }
        }
        synchronized () {
            this. = format;
            this. = period;
            final long now = System.currentTimeMillis();
            calcNextRollover(now);
        }
    }
    private void rollOver() {
        try {
            final File file = getFile();
            // first, close the original file (some OSes won't let you move/rename a file that is open)
            setFile(null);
            // next, rotate it
            file.renameTo(new File(file.getAbsolutePath() + ));
            // start new file
            setFile(file);
        } catch (FileNotFoundException e) {
            reportError("Unable to rotate log file"e.);
        }
    }
    private void calcNextRollover(final long fromTime) {
        if ( == .) {
             = .;
            return;
        }
         = .format(new Date(fromTime));
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(fromTime);
        final Period period = this.;
        // clear out less-significant fields
        switch (period) {
            default:
            case :
                calendar.clear(.);
            case :
                calendar.clear(.);
                calendar.clear(.);
            case :
                calendar.clear(.);
                calendar.clear(.);
            case :
                calendar.clear(.);
            case :
                calendar.clear(.);
            case :
                calendar.clear(.);
            case :
                calendar.clear(.);
                calendar.clear(.);
        }
        // increment the relevant field
        switch (period) {
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
            case :
                calendar.add(., 1);
                break;
        }
         = calendar.getTimeInMillis();
    }
    private static <T extends Comparable<? super T>> T min(T a, T b) {
        return a.compareTo(b) <= 0 ? a : b;
    }

    
Possible period values. Keep in strictly ascending order of magnitude.
    public enum Period {
        MINUTE,
        HOUR,
        HALF_DAY,
        DAY,
        WEEK,
        MONTH,
        YEAR,
        NEVER,
    }
New to GrepCode? Check out our FAQ X