KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > test > TestRotatingFileOutputLogTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.test;
18
19 import java.io.File JavaDoc;
20 import org.apache.log.Hierarchy;
21 import org.apache.log.LogTarget;
22 import org.apache.log.Logger;
23 import org.apache.log.format.RawFormatter;
24 import org.apache.log.output.io.rotate.FileStrategy;
25 import org.apache.log.output.io.rotate.RevolvingFileStrategy;
26 import org.apache.log.output.io.rotate.RotateStrategy;
27 import org.apache.log.output.io.rotate.RotateStrategyBySize;
28 import org.apache.log.output.io.rotate.RotateStrategyByTime;
29 import org.apache.log.output.io.rotate.RotatingFileTarget;
30 import org.apache.log.output.io.rotate.UniqueFileStrategy;
31
32 /**
33  *
34  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
35  */

36 public class TestRotatingFileOutputLogTarget
37 {
38     private RawFormatter m_formatter = new RawFormatter();
39
40     /** test file rotation by size, using unique filenames
41      */

42     public void testSizeUnique()
43         throws Exception JavaDoc
44     {
45         final File JavaDoc file = new File JavaDoc( "test/size-unique.log" );
46         final FileStrategy fileStrategy = new UniqueFileStrategy( file );
47         final RotateStrategy rotateStrategy = new RotateStrategyBySize( 128 * 1024 );
48         final Logger logger = getLogger( fileStrategy, rotateStrategy );
49
50         doTest( logger );
51     }
52
53     /** test file rotation by size, using revolving filenames
54      */

55     public void testSizeRevoling()
56         throws Exception JavaDoc
57     {
58         final File JavaDoc file = new File JavaDoc( "test/size-revolve.log" );
59         final FileStrategy fileStrategy = new RevolvingFileStrategy( file, 20 );
60         final RotateStrategy rotateStrategy = new RotateStrategyBySize( 128 * 1024 );
61         final Logger logger = getLogger( fileStrategy, rotateStrategy );
62
63         doTest( logger );
64     }
65
66     /** test file rotation by time, using unique filenames
67      */

68     public void testTimeUnique()
69         throws Exception JavaDoc
70     {
71         final File JavaDoc file = new File JavaDoc( "test/time-unique.log" );
72         final FileStrategy fileStrategy = new UniqueFileStrategy( file );
73         final RotateStrategy rotateStrategy = new RotateStrategyByTime( 3 * 1000 );
74         final Logger logger = getLogger( fileStrategy, rotateStrategy );
75
76         doTest( logger );
77     }
78
79     /** test file rotation by time, using revolving filenames
80      */

81     public void testTimeRevolving()
82         throws Exception JavaDoc
83     {
84         final File JavaDoc file = new File JavaDoc( "test/time-revolve.log" );
85         final FileStrategy fileStrategy = new RevolvingFileStrategy( file, 5 );
86         final RotateStrategy rotateStrategy = new RotateStrategyByTime( 3 * 1000 );
87         final Logger logger = getLogger( fileStrategy, rotateStrategy );
88
89         doTest( logger );
90     }
91
92     private void doTest( final Logger logger )
93     {
94         final long startTime = System.currentTimeMillis();
95         final long diffTime = 10 * 1000;
96         long endTime = startTime;
97
98         int size = 0;
99         for( int i = 0; ( endTime - startTime ) < diffTime; i++ )
100         {
101             size += generateMessages( logger, i, size, ( endTime - startTime ) );
102             endTime = System.currentTimeMillis();
103         }
104     }
105
106     /** just generate some logger messages
107      */

108     private int generateMessages( final Logger logger,
109                                   final int i,
110                                   final long totalSize,
111                                   final long diffTime )
112     {
113         final String JavaDoc message =
114             "Message " + i + ": total size " + totalSize + " diff time " + diffTime;
115         logger.debug( message );
116         logger.info( message );
117         logger.warn( message );
118         logger.error( message );
119         logger.fatalError( message );
120
121         return message.length();
122     }
123
124     private Logger getLogger( final FileStrategy fileStrategy,
125                               final RotateStrategy rotateStrategy )
126         throws Exception JavaDoc
127     {
128         final RotatingFileTarget target =
129             new RotatingFileTarget( m_formatter, rotateStrategy, fileStrategy );
130         final Hierarchy hierarchy = new Hierarchy();
131         final Logger logger = hierarchy.getLoggerFor( "myCat" );
132
133         logger.setLogTargets( new LogTarget[]{target} );
134
135         return logger;
136     }
137
138     public static void main( final String JavaDoc args[] )
139         throws Exception JavaDoc
140     {
141         TestRotatingFileOutputLogTarget trfolt = new TestRotatingFileOutputLogTarget();
142         trfolt.testSizeUnique();
143         trfolt.testSizeRevoling();
144         trfolt.testTimeUnique();
145         trfolt.testTimeRevolving();
146     }
147 }
148
Popular Tags