KickJava   Java API By Example, From Geeks To Geeks.

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


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 stragety based when log writting started.
23  *
24  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
25  */

26 public class RotateStrategyByTime
27     implements RotateStrategy
28 {
29     ///time interval when rotation is triggered.
30
private long m_timeInterval;
31
32     ///time when logging started.
33
private long m_startingTime;
34
35     ///rotation count.
36
private long m_currentRotation;
37
38     /**
39      * Rotate logs by time.
40      * By default do log rotation every 24 hours
41      */

42     public RotateStrategyByTime()
43     {
44         this( 1000 * 60 * 60 * 24 );
45     }
46
47     /**
48      * Rotate logs by time.
49      *
50      * @param timeInterval rotate before time-interval [ms] has expired
51      */

52     public RotateStrategyByTime( final long timeInterval )
53     {
54         m_startingTime = System.currentTimeMillis();
55         m_currentRotation = 0;
56         m_timeInterval = timeInterval;
57     }
58
59     /**
60      * reset interval history counters.
61      */

62     public void reset()
63     {
64         m_startingTime = System.currentTimeMillis();
65         m_currentRotation = 0;
66     }
67
68     /**
69      * Check if now a log rotation is neccessary.
70      * If
71      * <code>(current_time - m_startingTime) / m_timeInterval &gt; m_currentRotation </code>
72      * rotation is needed.
73      *
74      * @param data the last message written to the log system
75      * @param file not used
76      * @return boolean return true if log rotation is neccessary, else false
77      */

78     public boolean isRotationNeeded( final String JavaDoc data, final File JavaDoc file )
79     {
80         final long newRotation =
81             ( System.currentTimeMillis() - m_startingTime ) / m_timeInterval;
82
83         if( newRotation > m_currentRotation )
84         {
85             m_currentRotation = newRotation;
86             return true;
87         }
88         else
89         {
90             return false;
91         }
92     }
93 }
94
95
96
Popular Tags