001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling;
015
016import java.io.File;
017
018import ch.qos.logback.core.util.Duration;
019import ch.qos.logback.core.util.FileSize;
020import ch.qos.logback.core.util.DefaultInvocationGate;
021import ch.qos.logback.core.util.InvocationGate;
022import ch.qos.logback.core.util.SimpleInvocationGate;
023
024/**
025 * SizeBasedTriggeringPolicy looks at size of the file being currently written
026 * to. If it grows bigger than the specified size, the FileAppender using the
027 * SizeBasedTriggeringPolicy rolls the file and creates a new one.
028 * 
029 * For more information about this policy, please refer to the online manual at
030 * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
031 * 
032 * @author Ceki Gülcü
033 * 
034 */
035public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
036
037    public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
038    /**
039     * The default maximum file size.
040     */
041    public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
042
043    FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
044    InvocationGate invocationGate = new SimpleInvocationGate();
045    Duration checkIncrement = null;
046
047    public SizeBasedTriggeringPolicy() {
048    }
049
050    public void start() {
051        if(checkIncrement != null)
052            invocationGate = new SimpleInvocationGate(checkIncrement);
053        super.start();
054    }
055
056
057    public boolean isTriggeringEvent(final File activeFile, final E event) {
058        long now = System.currentTimeMillis();
059        if (invocationGate.isTooSoon(now))
060            return false;
061
062        return (activeFile.length() >= maxFileSize.getSize());
063    }
064
065    public FileSize getMaxFileSize() {
066        return this.maxFileSize;
067    }
068
069    public void setMaxFileSize(FileSize aMaxFileSize) {
070        this.maxFileSize = aMaxFileSize;
071    }
072
073    public Duration getCheckIncrement() {
074        return checkIncrement;
075    }
076
077    public void setCheckIncrement(Duration checkIncrement) {
078        this.checkIncrement = checkIncrement;
079    }
080}