/**
* Extends SizeBasedTriggeringPolicy to trigger based on size or time in minutes
*
* Example:
* <triggeringPolicy class="com.foo.bar.TimeSizeBasedTriggeringPolicy">
* <MaxFileSize>10MB</MaxFileSize>
* <RolloverInMinutes>35</RolloverInMinutes>
* </triggeringPolicy>
*
* This would rollover every 35min or 10MB, whichever comes first. Because isTriggeringEvent()
* is only called when a log line comes in, these limits will not be obeyed perfectly.
*/
public class TimeSizeBasedTriggeringPolicy<E> extends SizeBasedTriggeringPolicy<E> {
private long MILLIS_IN_A_MINUTE = 60 * 1000;
private long rolloverPeriod;
private long nextRolloverTime;
@Override
public void start() {
nextRolloverTime = System.currentTimeMillis() + rolloverPeriod;
super.start();
}
@Override
public boolean isTriggeringEvent(File activeFile, final E event) {
long currentTime = System.currentTimeMillis();
boolean isRolloverTime = super.isTriggeringEvent(activeFile, event) ||
currentTime >= nextRolloverTime;
// Reset the rollover period regardless of why we're rolling over.
if (isRolloverTime) {
nextRolloverTime = currentTime + rolloverPeriod;
}
return isRolloverTime;
}
public long getRolloverInMinutes() {
return rolloverPeriod / MILLIS_IN_A_MINUTE;
}
public void setRolloverInMinutes(long rolloverPeriodInMinutes) {
this.rolloverPeriod = rolloverPeriodInMinutes * MILLIS_IN_A_MINUTE;
}
}
1条答案
按热度按时间332nm8kg1#
这是一个很长的时间,有人要求的代码,所以我想我会张贴它作为一个答案。我没有测试过,但它应该作为一个合理的例子。