KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > io > rotate > RotateStrategyBySize


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output.io.rotate;
9
10 import java.io.File JavaDoc;
11
12 /**
13  * Rotation stragety based on size written to log file.
14  *
15  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
16  */

17 public class RotateStrategyBySize
18         implements RotateStrategy {
19     private long m_maxSize;
20     private long m_currentSize;
21
22     /**
23      * Rotate logs by size.
24      * By default do log rotation after writing approx. 1MB of messages
25      */

26     public RotateStrategyBySize() {
27         this(1024 * 1024);
28     }
29
30     /**
31      * Rotate logs by size.
32      *
33      * @param maxSize rotate after writing max_size [byte] of messages
34      */

35     public RotateStrategyBySize(final long maxSize) {
36         m_currentSize = 0;
37         m_maxSize = maxSize;
38     }
39
40     /**
41      * reset log size written so far.
42      */

43     public void reset() {
44         m_currentSize = 0;
45     }
46
47     /**
48      * Check if now a log rotation is neccessary.
49      *
50      * @param data the last message written to the log system
51      * @return boolean return true if log rotation is neccessary, else false
52      */

53     public boolean isRotationNeeded(final String JavaDoc data, final File JavaDoc file) {
54         m_currentSize += data.length();
55         if (m_currentSize >= m_maxSize) {
56             m_currentSize = 0;
57             return true;
58         }
59         else {
60             return false;
61         }
62     }
63 }
64
65
Popular Tags