You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2003/05/14 20:20:28 UTC
cvs commit: jakarta-log4j/src/java/org/apache/log4j/spi OptionHandler.java
ceki 2003/05/14 11:20:28
Modified: src/java/org/apache/log4j/rolling RollingPolicy.java
RollingFileAppender.java
SlidingWindowRollingPolicy.java
src/java/org/apache/log4j/spi OptionHandler.java
Added: src/java/org/apache/log4j/rolling/helpers
FileNamePattern.java Util.java
Removed: src/java/org/apache/log4j/rolling/helpers
FileNamePatternParser.java
Log:
Work in progress
Revision Changes Path
1.3 +2 -2 jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java
Index: RollingPolicy.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicy.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RollingPolicy.java 5 May 2003 20:42:57 -0000 1.2
+++ RollingPolicy.java 14 May 2003 18:20:28 -0000 1.3
@@ -70,7 +70,7 @@
public void rollover(File oldLogFile);
/**
- * Get the new log file where logging should go.
+ * Get the new name of the active log file.
* */
- public File getActiveLogFile(String filename);
+ public String getActiveLogFileName();
}
1.5 +5 -2 jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java
Index: RollingFileAppender.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RollingFileAppender.java 5 May 2003 21:14:38 -0000 1.4
+++ RollingFileAppender.java 14 May 2003 18:20:28 -0000 1.5
@@ -93,10 +93,13 @@
*/
public void rollover() {
// Note: synchronization not necessary since doAppend is already synched
-
+
+ // make sure to close the hereto active log file!!
+ this.closeFile();
+
rollingPolicy.rollover(file);
- file = rollingPolicy.getActiveLogFile(fileName);
+ File activeFile = new File(rollingPolicy.getActiveLogFileName());
try {
// This will also close the file. This is OK since multiple
1.4 +51 -37 jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java
Index: SlidingWindowRollingPolicy.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SlidingWindowRollingPolicy.java 13 May 2003 22:32:49 -0000 1.3
+++ SlidingWindowRollingPolicy.java 14 May 2003 18:20:28 -0000 1.4
@@ -50,6 +50,9 @@
package org.apache.log4j.rolling;
import org.apache.log4j.Logger;
+import org.apache.log4j.rolling.helpers.FileNamePattern;
+import org.apache.log4j.rolling.helpers.Util;
+import org.apache.log4j.spi.OptionHandler;
import java.io.File;
@@ -60,70 +63,81 @@
* @author Ceki Gülcü
* @since 1.3
* */
-public class SlidingWindowRollingPolicy implements RollingPolicy {
+public class SlidingWindowRollingPolicy implements RollingPolicy,
+ OptionHandler {
static Logger logger = Logger.getLogger(SlidingWindowRollingPolicy.class);
int maxIndex;
int minIndex;
- String fileNamePattern;
+ FileNamePattern fileNamePattern;
String activeFileName;
public SlidingWindowRollingPolicy() {
minIndex = 1;
+ maxIndex = 7;
+ activeFileName = null;
}
- public void rollover(File file) {
+ public void rollover(File f) {
+ // Inside this method it is guaranteed that the hereto active log fil is closed.
// If maxIndex <= 0, then there is no file renaming to be done.
- if (maxIndex > 0) {
- String filename = file.getName();
-
+ if (maxIndex >= 0) {
// Delete the oldest file, to keep Windows happy.
- file = new File(filename + '.' + maxIndex);
+ File file = new File(fileNamePattern.convert(maxIndex));
if (file.exists()) {
file.delete();
}
- // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
-
- /* for (int i = maxIndex - 1; i >= 1; i--) {
- file = new File(filename + "." + i);
-
- if (file.exists()) {
- target = new File(filename + '.' + (i + 1));
- logger.debug("Renaming file " + file + " to " + target);
- file.renameTo(target);
- }
+ // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1}
+ for (int i = maxIndex - 1; i > minIndex; i--) {
+ Util.rename(
+ fileNamePattern.convert(i), fileNamePattern.convert(i + 1));
}
- // Rename fileName to fileName.1
- target = new File(fileName + "." + 1);
-
- this.closeFile(); // keep windows happy.
- */
+ if (activeFileName != null) {
+ //move active file name to min
+ // TODO: compress the currently active file into minIndex
+ Util.rename(activeFileName, fileNamePattern.convert(minIndex));
+ } else {
+ // TODO: compress the currently active file (minIndex) into minIndex+1
+ Util.rename(
+ fileNamePattern.convert(minIndex),
+ fileNamePattern.convert(minIndex + 1));
+ }
}
}
- /**
- * Return the file name for the i-th file in the sliding window according
- * to the file name pattern.
- * */
- protected String getFilenameInWindow(int i) {
- return fileNamePattern + i;
- }
-
- public File getActiveLogFile(String old) {
- return new File(old);
+ public void activateOptions() {
+ if (maxIndex < minIndex) {
+ logger.warn(
+ "maxIndex (" + maxIndex + ") cannot be smaller than minIndex ("
+ + minIndex + ").");
+ logger.warn("Setting maxIndex to equal minIndex.");
+ maxIndex = minIndex;
+ }
}
- public String getActiveFileName() {
- return activeFileName;
+ /**
+ *
+ * If the <b>ActiveFileName</b> option is set, then this method simply returns the
+ * value of the option. Otherwise, it returns the value of <b>FileNamePattern</b>
+ * for <b>MaxIndex</b>. For example, if <b>ActiveFileName</b> is not set and
+ * <b>FileNamePattern</b> is set to "mylogfile.%i" and <b>MaxIndex</b> is set to 0,
+ * then this method will return "mylogfile.0".
+ *
+ */
+ public String getActiveLogFileName() {
+ if (activeFileName == null) {
+ return fileNamePattern.convert(minIndex);
+ } else {
+ return activeFileName;
+ }
}
public String getFileNamePattern() {
- return fileNamePattern;
+ return fileNamePattern.toString();
}
-
public int getMaxIndex() {
return maxIndex;
}
@@ -133,7 +147,7 @@
}
public void setFileNamePattern(String fnp) {
- fileNamePattern = fnp;
+ fileNamePattern = new FileNamePattern(fnp);
}
public void setMaxIndex(int maxIndex) {
1.1 jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/FileNamePattern.java
Index: FileNamePattern.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package org.apache.log4j.rolling.helpers;
import org.apache.log4j.Logger;
/**
*
* This class helps parse file name patterns. Given a number or a date it returns
* a file name according to the file name pattern.
*
* @author Ceki Gülcü
*
*/
public class FileNamePattern {
static Logger logger = Logger.getLogger(FileNamePattern.class);
String pattern;
TokenConverter headTokenConverter;
public FileNamePattern(String pattern) {
if (pattern == null) {
throw new IllegalArgumentException(
"The argument to constrcutor cannot be null. ");
}
this.pattern = pattern;
parse();
}
public String toString() {
return pattern;
}
void parse() {
int lastIndex = 0;
TokenConverter tc = null;
int len = pattern.length();
while (true) {
int i = pattern.indexOf('%', lastIndex);
if (i == -1) {
String remainingStr = pattern.substring(lastIndex);
System.out.println("adding the identity token");
addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
break;
} else {
// test for degenerate case where the '%' character is at the end.
if (i == (len - 1)) {
String remainingStr = pattern.substring(lastIndex);
addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
break;
}
System.out.println("2adding the identity token");
tc =
addTokenConverter(
tc, new IdentityTokenConverter(pattern.substring(lastIndex, i)));
// At this stage, we can suppose that i < len -1
char nextChar = pattern.charAt(i + 1);
switch (nextChar) {
case 'i':
tc = addTokenConverter(tc, new IntegerTokenConverter());
lastIndex = i + 2;
break; // break from switch statement
case '%':
tc = addTokenConverter(tc, new IdentityTokenConverter("%"));
lastIndex = i + 2;
break;
default:
throw new IllegalArgumentException(
"The pattern[" + pattern
+ "] does not contain a valid specifer at position " + (i + 1));
}
}
}
}
TokenConverter addTokenConverter(
TokenConverter tc, TokenConverter newTokenConverter) {
if (tc == null) {
tc = headTokenConverter = newTokenConverter;
} else {
tc.next = newTokenConverter;
tc = newTokenConverter;
}
return tc;
}
public String convert(int i) {
TokenConverter p = headTokenConverter;
StringBuffer buf = new StringBuffer();
while (p != null) {
switch (p.getType()) {
case TokenConverter.IDENTITY:
buf.append(((IdentityTokenConverter) p).convert());
break;
case TokenConverter.INTEGER:
buf.append(((IntegerTokenConverter) p).convert(i));
break;
default:
logger.error(
"Encountered an unknown TokenConverter type for pattern [" + pattern
+ "].");
}
p = p.getNext();
}
return buf.toString();
}
}
1.1 jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/Util.java
Index: Util.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package org.apache.log4j.rolling.helpers;
import org.apache.log4j.Logger;
import java.io.File;
/**
* @author Ceki
*
*/
public class Util {
static Logger logger = Logger.getLogger(Util.class);
public static void rename(String from, String to) {
File fromFile = new File(from);
if (fromFile.exists()) {
File toFile = new File(to);
logger.debug("Renaming file [" + fromFile + "] to [" + toFile + "]");
boolean result = fromFile.renameTo(toFile);
if (!result) {
logger.debug(
"Failed to rename file [" + fromFile + "] to [" + toFile + "].");
}
}
}
}
1.13 +0 -21 jakarta-log4j/src/java/org/apache/log4j/spi/OptionHandler.java
Index: OptionHandler.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/spi/OptionHandler.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- OptionHandler.java 18 Mar 2003 21:41:33 -0000 1.12
+++ OptionHandler.java 14 May 2003 18:20:28 -0000 1.13
@@ -71,25 +71,4 @@
org.apache.log4j.FileAppender#setAppend Append} options both of
which are ambigous until the other is also set. */
void activateOptions();
-
- /**
- Return list of strings that the OptionHandler instance recognizes.
-
- @deprecated We now use JavaBeans style getters/setters.
- */
-
- // String[] getOptionStrings();
-
- /**
- Set <code>option</code> to <code>value</code>.
-
- <p>The handling of each option depends on the OptionHandler
- instance. Some options may become active immediately whereas
- other may be activated only when {@link #activateOptions} is
- called.
-
- @deprecated We now use JavaBeans style getters/setters.
- */
-
- //void setOption(String option, String value);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org