Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.butor.log4j;
 
 import java.io.File;
 import java.util.Date;
 
This class was copied from org.apache.log4j.rolling.TimeBasedRollingPolicy of apache-log4j-extras 1.1. We removed class modifiers 'public final' so that we can extend it.
 
TimeBasedRollingPolicy is both easy to configure and quite powerful.

In order to use TimeBasedRollingPolicy, the FileNamePattern option must be set. It basically specifies the name of the rolled log files. The value FileNamePattern should consist of the name of the file, plus a suitably placed %d conversion specifier. The %d conversion specifier may contain a date and time pattern as specified by the java.text.SimpleDateFormat class. If the date and time pattern is ommitted, then the default pattern of "yyyy-MM-dd" is assumed. The following examples should clarify the point.

FileNamePattern valueRollover scheduleExample
/wombat/folder/foo.%dDaily rollover (at midnight). Due to the omission of the optional time and date pattern for the %d token specifier, the default pattern of "yyyy-MM-dd" is assumed, which corresponds to daily rollover. During November 23rd, 2004, logging output will go to the file /wombat/foo.2004-11-23. At midnight and for the rest of the 24th, logging output will be directed to /wombat/foo.2004-11-24.
/wombat/foo.%d{yyyy-MM}.logRollover at the beginning of each month.During the month of October 2004, logging output will go to /wombat/foo.2004-10.log. After midnight of October 31st and for the rest of November, logging output will be directed to /wombat/foo.2004-11.log.

Automatic file compression

TimeBasedRollingPolicy supports automatic file compression. This feature is enabled if the value of the FileNamePattern option ends with .gz or .zip.

FileNamePattern valueRollover scheduleExample
/wombat/foo.%d.gzDaily rollover (at midnight) with automatic GZIP compression of the archived files.During November 23rd, 2004, logging output will go to the file /wombat/foo.2004-11-23. However, at midnight that file will be compressed to become /wombat/foo.2004-11-23.gz. For the 24th of November, logging output will be directed to /wombat/folder/foo.2004-11-24 until its rolled over at the beginning of the next day.

Decoupling the location of the active log file and the archived log files

The active file is defined as the log file for the current period whereas archived files are those files which have been rolled over in previous periods.

By setting the ActiveFileName option you can decouple the location of the active log file and the location of the archived log files.

FileNamePattern valueActiveFileNameRollover scheduleExample
/wombat/foo.log.%d/wombat/foo.logDaily rollover.During November 23rd, 2004, logging output will go to the file /wombat/foo.log. However, at midnight that file will archived as /wombat/foo.log.2004-11-23. For the 24th of November, logging output will be directed to /wombat/folder/foo.log until its archived as /wombat/foo.log.2004-11-24 at the beginning of the next day.

If configuring programatically, do not forget to call activateOptions() method before using this policy. Moreover, activateOptions() of TimeBasedRollingPolicy must be called before calling the activateOptions() method of the owning RollingFileAppender.

Author(s):
Ceki Gülcü
Curt Arnold
  implements TriggeringPolicy {

  
Time for next determination if time for rollover.
  private long nextCheck = 0;

  
File name at last rollover.
  private String lastFileName = null;

  
Length of any file type suffix (.gz, .zip).
  private int suffixLength = 0;

  
Constructs a new instance.
  }

  
Prepares instance of use.
  public void activateOptions() {
    super.activateOptions();
    if (dtc == null) {
      throw new IllegalStateException(
        "FileNamePattern [" + getFileNamePattern()
        + "] does not contain a valid date format specifier");
    }
    long n = System.currentTimeMillis();
    StringBuffer buf = new StringBuffer();
    formatFileName(new Date(n), buf);
     = buf.toString();
     = 0;
    if (.endsWith(".gz")) {
       = 3;
    } else if (.endsWith(".zip")) {
       = 4;
    }
  }

  
    final String currentActiveFilefinal boolean append) {
    long n = System.currentTimeMillis();
     = ((n / 1000) + 1) * 1000;
    StringBuffer buf = new StringBuffer();
    formatFileName(new Date(n), buf);
     = buf.toString();
    //
    //  RollingPolicyBase.activeFileName duplicates RollingFileAppender.file
    //    and should be removed.
    //
    if ( != null) {
      return new RolloverDescriptionImpl(appendnullnull);
    } else if (currentActiveFile != null) {
      return new RolloverDescriptionImpl(
        currentActiveFileappendnullnull);
    } else {
      return new RolloverDescriptionImpl(
        .substring(0, .length() - ), append,
        nullnull);
    }
  }

  
  public RolloverDescription rollover(final String currentActiveFile) {
    long n = System.currentTimeMillis();
     = ((n / 1000) + 1) * 1000;
    StringBuffer buf = new StringBuffer();
    formatFileName(new Date(n), buf);
    String newFileName = buf.toString();
    //
    //  if file names haven't changed, no rollover
    //
    if (newFileName.equals()) {
      return null;
    }
    Action renameAction = null;
    Action compressAction = null;
    String lastBaseName =
    String nextActiveFile =
      newFileName.substring(0, newFileName.length() - );
    //
    //   if currentActiveFile is not lastBaseName then
    //        active file name is not following file pattern
    //        and requires a rename plus maintaining the same name
    if (!currentActiveFile.equals(lastBaseName)) {
      renameAction =
        new FileRenameAction(
          new File(currentActiveFile), new File(lastBaseName), true);
      nextActiveFile = currentActiveFile;
    }
    if ( == 3) {
      compressAction =
        new GZCompressAction(
          new File(lastBaseName), new File(), true);
    }
    if ( == 4) {
      compressAction =
        new ZipCompressAction(
          new File(lastBaseName), new File(), true);
    }
     = newFileName;
    return new RolloverDescriptionImpl(
      nextActiveFilefalserenameActioncompressAction);
  }

  
  public boolean isTriggeringEvent(
    final Appender appenderfinal LoggingEvent eventfinal String filename,
    final long fileLength) {
    return System.currentTimeMillis() >= ;
  }
New to GrepCode? Check out our FAQ X