KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > io > rotate > RotateStrategyBySize


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.io.rotate;
18
19 import java.io.File JavaDoc;
20
21 /**
22  * Rotation strategy based on size written to log file.
23  * The strategy will signal that a rotation is needed if the
24  * size goes above a set limit. Due to performance reasons
25  * the limit is not strictly enforced, however, the strategy has
26  * at most an error of the longest single data message written to the
27  * logging system. The error will occur immediately after a rotation,
28  * when the strategy is reset and the data that triggered the
29  * rotation is written. The strategy's internal counter will then
30  * be off with data.length() bytes.
31  *
32  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
33  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
34  */

35 public class RotateStrategyBySize
36     implements RotateStrategy
37 {
38     private long m_maxSize;
39     private long m_currentSize;
40
41     /**
42      * Rotate logs by size.
43      * By default do log rotation before writing approx. 1MB of messages
44      */

45     public RotateStrategyBySize()
46     {
47         this( 1024 * 1024 );
48     }
49
50     /**
51      * Rotate logs by size.
52      *
53      * @param maxSize rotate before writing maxSize [byte] of messages
54      */

55     public RotateStrategyBySize( final long maxSize )
56     {
57         m_currentSize = 0;
58         m_maxSize = maxSize;
59     }
60
61     /**
62      * Reset log size written so far.
63      */

64     public void reset()
65     {
66         m_currentSize = 0;
67     }
68
69     /**
70      * Check if now a log rotation is neccessary.
71      *
72      * @param data the message about to be written to the log system
73      * @return boolean return true if log rotation is neccessary, else false
74      * @param file not used
75      */

76     public boolean isRotationNeeded( final String JavaDoc data, final File JavaDoc file )
77     {
78         m_currentSize += data.length();
79
80         return m_currentSize >= m_maxSize;
81     }
82 }
83
84
Popular Tags