001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.test;
056:
057: import java.io.File;
058: import org.apache.log.Hierarchy;
059: import org.apache.log.LogTarget;
060: import org.apache.log.Logger;
061: import org.apache.log.format.RawFormatter;
062: import org.apache.log.output.io.rotate.FileStrategy;
063: import org.apache.log.output.io.rotate.RevolvingFileStrategy;
064: import org.apache.log.output.io.rotate.RotateStrategy;
065: import org.apache.log.output.io.rotate.RotateStrategyBySize;
066: import org.apache.log.output.io.rotate.RotateStrategyByTime;
067: import org.apache.log.output.io.rotate.RotatingFileTarget;
068: import org.apache.log.output.io.rotate.UniqueFileStrategy;
069:
070: /**
071: *
072: * @author <a href="mailto:bh22351@i-one.at">Bernhard Huber</a>
073: */
074: public class TestRotatingFileOutputLogTarget {
075: private RawFormatter m_formatter = new RawFormatter();
076:
077: /** test file rotation by size, using unique filenames
078: */
079: public void testSizeUnique() throws Exception {
080: final File file = new File("test/size-unique.log");
081: final FileStrategy fileStrategy = new UniqueFileStrategy(file);
082: final RotateStrategy rotateStrategy = new RotateStrategyBySize(
083: 128 * 1024);
084: final Logger logger = getLogger(fileStrategy, rotateStrategy);
085:
086: doTest(logger);
087: }
088:
089: /** test file rotation by size, using revolving filenames
090: */
091: public void testSizeRevoling() throws Exception {
092: final File file = new File("test/size-revolve.log");
093: final FileStrategy fileStrategy = new RevolvingFileStrategy(
094: file, 20);
095: final RotateStrategy rotateStrategy = new RotateStrategyBySize(
096: 128 * 1024);
097: final Logger logger = getLogger(fileStrategy, rotateStrategy);
098:
099: doTest(logger);
100: }
101:
102: /** test file rotation by time, using unique filenames
103: */
104: public void testTimeUnique() throws Exception {
105: final File file = new File("test/time-unique.log");
106: final FileStrategy fileStrategy = new UniqueFileStrategy(file);
107: final RotateStrategy rotateStrategy = new RotateStrategyByTime(
108: 3 * 1000);
109: final Logger logger = getLogger(fileStrategy, rotateStrategy);
110:
111: doTest(logger);
112: }
113:
114: /** test file rotation by time, using revolving filenames
115: */
116: public void testTimeRevolving() throws Exception {
117: final File file = new File("test/time-revolve.log");
118: final FileStrategy fileStrategy = new RevolvingFileStrategy(
119: file, 5);
120: final RotateStrategy rotateStrategy = new RotateStrategyByTime(
121: 3 * 1000);
122: final Logger logger = getLogger(fileStrategy, rotateStrategy);
123:
124: doTest(logger);
125: }
126:
127: private void doTest(final Logger logger) {
128: final long startTime = System.currentTimeMillis();
129: final long diffTime = 10 * 1000;
130: long endTime = startTime;
131:
132: int size = 0;
133: for (int i = 0; (endTime - startTime) < diffTime; i++) {
134: size += generateMessages(logger, i, size,
135: (endTime - startTime));
136: endTime = System.currentTimeMillis();
137: }
138: }
139:
140: /** just generate some logger messages
141: */
142: private int generateMessages(final Logger logger, final int i,
143: final long totalSize, final long diffTime) {
144: final String message = "Message " + i + ": total size "
145: + totalSize + " diff time " + diffTime;
146: logger.debug(message);
147: logger.info(message);
148: logger.warn(message);
149: logger.error(message);
150: logger.fatalError(message);
151:
152: return message.length();
153: }
154:
155: private Logger getLogger(final FileStrategy fileStrategy,
156: final RotateStrategy rotateStrategy) throws Exception {
157: final RotatingFileTarget target = new RotatingFileTarget(
158: m_formatter, rotateStrategy, fileStrategy);
159: final Hierarchy hierarchy = new Hierarchy();
160: final Logger logger = hierarchy.getLoggerFor("myCat");
161:
162: logger.setLogTargets(new LogTarget[] { target });
163:
164: return logger;
165: }
166:
167: public static void main(final String args[]) throws Exception {
168: TestRotatingFileOutputLogTarget trfolt = new TestRotatingFileOutputLogTarget();
169: trfolt.testSizeUnique();
170: trfolt.testSizeRevoling();
171: trfolt.testTimeUnique();
172: trfolt.testTimeRevolving();
173: }
174: }
|