All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.butor.log4j.TimeBasedRollingPolicyFromApacheLog4jExtras Maven / Gradle / Ivy

Go to download

This project is an log4j specific module, contains utility specific to log4j.

The newest version!
/**
 * Copyright 2013-2019 Butor Inc.
 *
 * Licensed 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;

import org.apache.log4j.Appender;
import org.apache.log4j.pattern.PatternConverter;
import org.apache.log4j.rolling.RollingPolicyBase;
import org.apache.log4j.rolling.RolloverDescription;
import org.apache.log4j.rolling.RolloverDescriptionImpl;
import org.apache.log4j.rolling.TriggeringPolicy;
import org.apache.log4j.rolling.helper.Action;
import org.apache.log4j.rolling.helper.FileRenameAction;
import org.apache.log4j.rolling.helper.GZCompressAction;
import org.apache.log4j.rolling.helper.ZipCompressAction;
import org.apache.log4j.spi.LoggingEvent;

/**
 * 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 {@link 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 {@link #activateOptions} * method before using this policy. Moreover, {@link #activateOptions} of * TimeBasedRollingPolicy must be called before calling * the {@link #activateOptions} method of the owning * RollingFileAppender. * * @author Ceki Gülcü * @author Curt Arnold */ class TimeBasedRollingPolicyFromApacheLog4jExtras extends RollingPolicyBase 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. */ public TimeBasedRollingPolicyFromApacheLog4jExtras() { } /** * Prepares instance of use. */ public void activateOptions() { super.activateOptions(); PatternConverter dtc = getDatePatternConverter(); 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); lastFileName = buf.toString(); suffixLength = 0; if (lastFileName.endsWith(".gz")) { suffixLength = 3; } else if (lastFileName.endsWith(".zip")) { suffixLength = 4; } } /** * {@inheritDoc} */ public RolloverDescription initialize( final String currentActiveFile, final boolean append) { long n = System.currentTimeMillis(); nextCheck = ((n / 1000) + 1) * 1000; StringBuffer buf = new StringBuffer(); formatFileName(new Date(n), buf); lastFileName = buf.toString(); // // RollingPolicyBase.activeFileName duplicates RollingFileAppender.file // and should be removed. // if (activeFileName != null) { return new RolloverDescriptionImpl(activeFileName, append, null, null); } else if (currentActiveFile != null) { return new RolloverDescriptionImpl( currentActiveFile, append, null, null); } else { return new RolloverDescriptionImpl( lastFileName.substring(0, lastFileName.length() - suffixLength), append, null, null); } } /** * {@inheritDoc} */ public RolloverDescription rollover(final String currentActiveFile) { long n = System.currentTimeMillis(); nextCheck = ((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(lastFileName)) { return null; } Action renameAction = null; Action compressAction = null; String lastBaseName = lastFileName.substring(0, lastFileName.length() - suffixLength); String nextActiveFile = newFileName.substring(0, newFileName.length() - suffixLength); // // 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 (suffixLength == 3) { compressAction = new GZCompressAction( new File(lastBaseName), new File(lastFileName), true); } if (suffixLength == 4) { compressAction = new ZipCompressAction( new File(lastBaseName), new File(lastFileName), true); } lastFileName = newFileName; return new RolloverDescriptionImpl( nextActiveFile, false, renameAction, compressAction); } /** * {@inheritDoc} */ public boolean isTriggeringEvent( final Appender appender, final LoggingEvent event, final String filename, final long fileLength) { return System.currentTimeMillis() >= nextCheck; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy