01: /*
02: * Copyright (C) The Apache Software Foundation. All rights reserved.
03: *
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08: package org.jivesoftware.util.log.output.io.rotate;
09:
10: import java.io.File;
11:
12: /**
13: * rotation stragety based when log writting started.
14: *
15: * @author <a href="mailto:bh22351@i-one.at">Bernhard Huber</a>
16: */
17: public class RotateStrategyByTime implements RotateStrategy {
18: ///time interval when rotation is triggered.
19: private long m_timeInterval;
20:
21: ///time when logging started.
22: private long m_startingTime;
23:
24: ///rotation count.
25: private long m_currentRotation;
26:
27: /**
28: * Rotate logs by time.
29: * By default do log rotation every 24 hours
30: */
31: public RotateStrategyByTime() {
32: this (1000 * 60 * 60 * 24);
33: }
34:
35: /**
36: * Rotate logs by time.
37: *
38: * @param timeInterval rotate after time-interval [ms] has expired
39: */
40: public RotateStrategyByTime(final long timeInterval) {
41: m_startingTime = System.currentTimeMillis();
42: m_currentRotation = 0;
43: m_timeInterval = timeInterval;
44: }
45:
46: /**
47: * reset interval history counters.
48: */
49: public void reset() {
50: m_startingTime = System.currentTimeMillis();
51: m_currentRotation = 0;
52: }
53:
54: /**
55: * Check if now a log rotation is neccessary.
56: * If
57: * <code>(current_time - m_startingTime) / m_timeInterval > m_currentRotation </code>
58: * rotation is needed.
59: *
60: * @param data the last message written to the log system
61: * @return boolean return true if log rotation is neccessary, else false
62: */
63: public boolean isRotationNeeded(final String data, final File file) {
64: final long newRotation = (System.currentTimeMillis() - m_startingTime)
65: / m_timeInterval;
66:
67: if (newRotation > m_currentRotation) {
68: m_currentRotation = newRotation;
69: return true;
70: } else {
71: return false;
72: }
73: }
74: }
|