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/21 22:30:17 UTC
cvs commit: jakarta-log4j/tests/src/java/org/apache/log4j/rolling TimeBasedRollingTestCase.java
ceki 2003/05/21 13:30:17
Modified: src/java/org/apache/log4j/rolling/helpers Compress.java
src/java/org/apache/log4j/rolling RollingPolicySkeleton.java
TimeBasedRollingPolicy.java
tests/src/java/org/apache/log4j/rolling
TimeBasedRollingTestCase.java
Added: tests/input compress3.txt compress2.txt
Log:
It rolls and compresses too. (This code actually works.)
Revision Changes Path
1.4 +9 -4 jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/Compress.java
Index: Compress.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/Compress.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Compress.java 21 May 2003 18:59:59 -0000 1.3
+++ Compress.java 21 May 2003 20:30:17 -0000 1.4
@@ -72,7 +72,6 @@
public static void ZIPCompress(String nameOfFile2zip, String nameOfZippedFile) {
File file2zip = new File(nameOfFile2zip);
- File zippedFile = new File(nameOfZippedFile);
if (!file2zip.exists()) {
logger.warn(
@@ -84,7 +83,8 @@
if (!nameOfZippedFile.endsWith(".zip")) {
nameOfZippedFile = nameOfZippedFile + ".zip";
}
-
+
+ File zippedFile = new File(nameOfZippedFile);
if (zippedFile.exists()) {
logger.warn(
"The target compressed file named [" + nameOfZippedFile
@@ -115,10 +115,14 @@
}
}
+ public static void GZCompress(String nameOfFile2gz) {
+ // Here we rely on the fact that the two argument version of GZCompress automatically
+ // add te .gz exention to the second argument
+ GZCompress(nameOfFile2gz, nameOfFile2gz);
+ }
public static void GZCompress(String nameOfFile2gz, String nameOfgzedFile) {
File file2gz = new File(nameOfFile2gz);
- File gzedFile = new File(nameOfgzedFile);
-
+
if (!file2gz.exists()) {
logger.warn(
"The file to compress named [" + nameOfFile2gz + "] does not exist.");
@@ -130,6 +134,7 @@
nameOfgzedFile = nameOfgzedFile + ".gz";
}
+ File gzedFile = new File(nameOfgzedFile);
if (gzedFile.exists()) {
logger.warn(
"The target compressed file named [" + nameOfgzedFile
1.1 jakarta-log4j/tests/input/compress3.txt
Index: compress3.txt
===================================================================
/*
* ============================================================================
* 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/>.
*
*/
===========
Using log4j
===========
1) First untar or unzip the distribution file.
2) Assuming you chose to extract the distribution in to the
PATH_OF_YOUR_CHOICE, untarring the distribution file should create
a jakarta-log4j-VERSION directory, where VERSION is the log4j
version number, under PATH_OF_YOUR_CHOICE. We will refer to the
directory PATH_OF_YOUR_CHOICE/jakarta-log4j-VERSION/ as $LOG4J_HOME/.
3) Assuming you are using log4j version 1.2, add
$LOG4J_HOME/dist/lib/log4j-1.2.jar to your CLASSPATH,
4) You can now test your installation by first compiling the following
simple program.
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class Hello {
static Logger logger = Logger.getLogger(Hello.class);
public
static
void main(String argv[]) {
BasicConfigurator.configure();
logger.debug("Hello world.");
logger.info("What a beatiful day.");
}
}
After compilation, try it out by issuing the command
java Hello
You should see log statements appearing on the console.
5) Refer to the javadoc documentation and the user manual on how to
include log statements in your own code.
=========
JAR files
=========
The log4j distribution comes with one jar file: log4j-VERSION.jar
under the LOG4J_HOME/dist/lib/ directory.
This jar file contains all the class files of the log4j project,
except test cases and classes from the "examples" and
"org.apache.log4j.performance" packages.
==================
log4j dependencies
==================
Log4j is based on JDK 1.1 with the following additional requirements:
----------------------------
Package org.apache.log4j.xml
----------------------------
The DOMConfigurator is based on the DOM Level 1 API. The
DOMConfigurator.configure(Element) method will work with any
XML parser that will pass it a DOM tree.
The DOMConfigurator.configure(String filename) method and its variants
require a JAXP compatible XMLparser, for example the Apache Xerces
parser. Compiling the DOMConfigurator requires the presence of a
JAXP parser in the classpath.
Given that Ant already ships with a compatible XML parser, you do
*not* need to worry about setting the parser when building,
i.e. compiling, log4j.
------------
SMTPAppender
------------
The SMTPAppender relies on the JavaMail API. It has been tested with
JavaMail API version 1.2. The JavaMail API requires the
JavaBeans Activation Framework package. You can download the
JavaMail API at:
http://java.sun.com/products/javamail/
and the JavaBeans Activation Framework at:
http://java.sun.com/beans/glasgow/jaf.html
-----------
JMSAppender
-----------
The JMSAppender requires the JMS API as well as JNDI. The JMS API
is usually bundled with the products of the vendors listed under
http://java.sun.com/products/jms/vendors.html
-----------------------
JUnit testing framework
-----------------------
Log4j uses the JUnit framework version 3.7 for internal unit
testing. If you want to compile the source code in the tests/
directory, then you will need JUnit. JUnit is available from:
http://www.junit.org
==============
Building log4j
==============
Like most java appilicatios today, log4j relies on ANT as its build
tool. ANT is availale from "http://jakarta.apache.org/ant/". ANT
requires a build file called build.xml which is part of this
distribution. Required components from other projects are specified in
the build.properties and example of which is supplied in the
build.properties.sample file.
In case of problems send an e-mail note to
log4j-user@jakarta.apache.org. Please do not directly e-mail any
log4j developers. The answer to your question might be useful to other
users. Moreover, there are many knowledgeable users on the log4j-user
mailing lists who can quickly answer your questions.
1.1 jakarta-log4j/tests/input/compress2.txt
Index: compress2.txt
===================================================================
===========
Using log4j
===========
1) First untar or unzip the distribution file.
2) Assuming you chose to extract the distribution in to the
PATH_OF_YOUR_CHOICE, untarring the distribution file should create
a jakarta-log4j-VERSION directory, where VERSION is the log4j
version number, under PATH_OF_YOUR_CHOICE. We will refer to the
directory PATH_OF_YOUR_CHOICE/jakarta-log4j-VERSION/ as $LOG4J_HOME/.
3) Assuming you are using log4j version 1.2, add
$LOG4J_HOME/dist/lib/log4j-1.2.jar to your CLASSPATH,
4) You can now test your installation by first compiling the following
simple program.
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class Hello {
static Logger logger = Logger.getLogger(Hello.class);
public
static
void main(String argv[]) {
BasicConfigurator.configure();
logger.debug("Hello world.");
logger.info("What a beatiful day.");
}
}
After compilation, try it out by issuing the command
java Hello
You should see log statements appearing on the console.
5) Refer to the javadoc documentation and the user manual on how to
include log statements in your own code.
=========
JAR files
=========
The log4j distribution comes with one jar file: log4j-VERSION.jar
under the LOG4J_HOME/dist/lib/ directory.
This jar file contains all the class files of the log4j project,
except test cases and classes from the "examples" and
"org.apache.log4j.performance" packages.
==================
log4j dependencies
==================
Log4j is based on JDK 1.1 with the following additional requirements:
----------------------------
Package org.apache.log4j.xml
----------------------------
The DOMConfigurator is based on the DOM Level 1 API. The
DOMConfigurator.configure(Element) method will work with any
XML parser that will pass it a DOM tree.
The DOMConfigurator.configure(String filename) method and its variants
require a JAXP compatible XMLparser, for example the Apache Xerces
parser. Compiling the DOMConfigurator requires the presence of a
JAXP parser in the classpath.
Given that Ant already ships with a compatible XML parser, you do
*not* need to worry about setting the parser when building,
i.e. compiling, log4j.
------------
SMTPAppender
------------
The SMTPAppender relies on the JavaMail API. It has been tested with
JavaMail API version 1.2. The JavaMail API requires the
JavaBeans Activation Framework package. You can download the
JavaMail API at:
http://java.sun.com/products/javamail/
and the JavaBeans Activation Framework at:
http://java.sun.com/beans/glasgow/jaf.html
-----------
JMSAppender
-----------
The JMSAppender requires the JMS API as well as JNDI. The JMS API
is usually bundled with the products of the vendors listed under
http://java.sun.com/products/jms/vendors.html
-----------------------
JUnit testing framework
-----------------------
Log4j uses the JUnit framework version 3.7 for internal unit
testing. If you want to compile the source code in the tests/
directory, then you will need JUnit. JUnit is available from:
http://www.junit.org
==============
Building log4j
==============
Like most java appilicatios today, log4j relies on ANT as its build
tool. ANT is availale from "http://jakarta.apache.org/ant/". ANT
requires a build file called build.xml which is part of this
distribution. Required components from other projects are specified in
the build.properties and example of which is supplied in the
build.properties.sample file.
In case of problems send an e-mail note to
log4j-user@jakarta.apache.org. Please do not directly e-mail any
log4j developers. The answer to your question might be useful to other
users. Moreover, there are many knowledgeable users on the log4j-user
mailing lists who can quickly answer your questions.
1.2 +2 -0 jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicySkeleton.java
Index: RollingPolicySkeleton.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingPolicySkeleton.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RollingPolicySkeleton.java 21 May 2003 18:59:59 -0000 1.1
+++ RollingPolicySkeleton.java 21 May 2003 20:30:17 -0000 1.2
@@ -83,11 +83,13 @@
public void setCompressionMode(String compMode) {
+ System.out.println("================"+compMode);
if(compMode == null) {
compressionMode = Compress.NONE;
}
compMode = compMode.trim();
if(compMode.equalsIgnoreCase(Compress.GZ_STR)) {
+ System.out.println("==================================");
compressionMode = Compress.GZ;
} else if (compMode.equalsIgnoreCase(Compress.ZIP_STR)) {
compressionMode = Compress.ZIP;
1.2 +43 -12 jakarta-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java
Index: TimeBasedRollingPolicy.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TimeBasedRollingPolicy.java 21 May 2003 18:59:59 -0000 1.1
+++ TimeBasedRollingPolicy.java 21 May 2003 20:30:17 -0000 1.2
@@ -50,6 +50,7 @@
package org.apache.log4j.rolling;
import org.apache.log4j.Logger;
+import org.apache.log4j.rolling.helpers.Compress;
import org.apache.log4j.rolling.helpers.DateTokenConverter;
import org.apache.log4j.rolling.helpers.FileNamePattern;
import org.apache.log4j.rolling.helpers.RollingCalendar;
@@ -66,14 +67,15 @@
*
* @author Ceki Gülcü
*/
-public class TimeBasedRollingPolicy extends RollingPolicySkeleton implements TriggeringPolicy {
+public class TimeBasedRollingPolicy extends RollingPolicySkeleton
+ implements TriggeringPolicy {
static final Logger logger = Logger.getLogger(TimeBasedRollingPolicy.class);
FileNamePattern fileNamePattern;
RollingCalendar rc;
long nextCheck;
Date now = new Date();
- int compressionMode;
-
+ String oldFileName = null;
+
public void activateOptions() {
// find out period from the filename pattern
if (fileNamePattern != null) {
@@ -84,17 +86,21 @@
"FileNamePattern [" + fileNamePattern.getPattern()
+ "] does not contain a valid DateToken");
}
+
rc = new RollingCalendar();
rc.init(dtc.getDatePattern());
logger.debug(
- "The date pattern is [" + dtc.getDatePattern()
- + "] from file name pattern [" + fileNamePattern.getPattern() + "].");
+ "The date pattern is [" + dtc.getDatePattern()
+ + "] from file name pattern [" + fileNamePattern.getPattern() + "].");
rc.printPeriodicity();
+
long n = System.currentTimeMillis();
now.setTime(n);
nextCheck = rc.getNextCheckMillis(now);
- Date x = new Date(); x.setTime(nextCheck);
- logger.debug("Next check set to: "+x);
+
+ Date x = new Date();
+ x.setTime(nextCheck);
+ logger.debug("Next check set to: " + x);
}
}
@@ -103,7 +109,24 @@
}
public void rollover() {
- // nothing to do!!!
+
+ logger.debug("rollover called");
+ logger.debug("compressionMode: "+compressionMode);
+ if (oldFileName != null) {
+ logger.debug("oldFileName != null");
+ switch (compressionMode) {
+ case Compress.NONE:
+
+ // nothing to do;
+ break;
+
+ case Compress.GZ:
+ logger.debug("Compressing ["+oldFileName+"]");
+ Compress.GZCompress(oldFileName);
+
+ break;
+ }
+ }
}
public void setFileNamePattern(String fnp) {
@@ -111,17 +134,25 @@
}
public boolean isTriggeringEvent(File file) {
- logger.debug("Is triggering event called");
+ //logger.debug("Is triggering event called");
+
long n = System.currentTimeMillis();
if (n >= nextCheck) {
logger.debug("Time to trigger rollover");
+
+ // we set the oldFileName before we set the 'now' variable
+ oldFileName = fileNamePattern.convert(now);
+
now.setTime(n);
- logger.debug("ActiveLogFileName will return "+getActiveLogFileName());
+ logger.debug("ActiveLogFileName will return " + getActiveLogFileName());
nextCheck = rc.getNextCheckMillis(now);
+
//logger.debug("nextCheck is :"+nextCheck);
- Date x = new Date(); x.setTime(nextCheck);
- logger.debug("Next check: "+x);
+ Date x = new Date();
+ x.setTime(nextCheck);
+ logger.debug("Next check: " + x);
+
return true;
} else {
return false;
1.2 +36 -2 jakarta-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTestCase.java
Index: TimeBasedRollingTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/tests/src/java/org/apache/log4j/rolling/TimeBasedRollingTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TimeBasedRollingTestCase.java 21 May 2003 19:00:00 -0000 1.1
+++ TimeBasedRollingTestCase.java 21 May 2003 20:30:17 -0000 1.2
@@ -57,6 +57,7 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
+import org.apache.log4j.rolling.helpers.Compress;
import org.apache.log4j.util.Compare;
@@ -76,6 +77,7 @@
}
public void tearDown() {
+ logger.debug("Tear down called.");
LogManager.shutdown();
}
@@ -109,13 +111,45 @@
// The File.length() method is not accurate under Windows
}
+
+ public void test2() throws Exception {
+ Logger root = Logger.getRootLogger();
+ root.addAppender(new ConsoleAppender(new PatternLayout()));
+
+ // We purposefully use the \n as the line separator.
+ // This makes the regression test system indepent.
+ PatternLayout layout = new PatternLayout("%d %c %m\n");
+ RollingFileAppender rfa = new RollingFileAppender();
+ rfa.setLayout(layout);
+
+ TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
+ tbrp.setFileNamePattern("output/tbt%d{yyyy-MM-dd_HH_mm}");
+ tbrp.setCompressionMode(Compress.GZ_STR);
+ rfa.setRollingPolicy(tbrp);
+ rfa.activateOptions();
+ root.addAppender(rfa);
+
+ // Write exactly 10 bytes with each log
+ for (int i = 0; i < 30; i++) {
+ Thread.sleep(1000);
+ if (i < 10) {
+ logger.debug("Hello---" + i);
+ } else if (i < 100) {
+ logger.debug("Hello--" + i);
+ } else {
+ logger.debug("Hello-" + i);
+ }
+ }
+
+ // The File.length() method is not accurate under Windows
+ }
public static Test suite() {
TestSuite suite = new TestSuite();
- suite.addTest(new TimeBasedRollingTestCase("test1"));
- //suite.addTest(new TimeBasedRollingTestCase("test2"));
+ //suite.addTest(new TimeBasedRollingTestCase("test1"));
+ suite.addTest(new TimeBasedRollingTestCase("test2"));
return suite;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org