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.FileSize;
019import ch.qos.logback.core.util.DefaultInvocationGate;
020import ch.qos.logback.core.util.InvocationGate;
021
022/**
023 * SizeBasedTriggeringPolicy looks at size of the file being currently written
024 * to. If it grows bigger than the specified size, the FileAppender using the
025 * SizeBasedTriggeringPolicy rolls the file and creates a new one.
026 * 
027 * For more information about this policy, please refer to the online manual at
028 * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
029 * 
030 * @author Ceki Gülcü
031 * 
032 */
033public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
034
035    public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
036    /**
037     * The default maximum file size.
038     */
039    public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
040
041    FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
042
043    public SizeBasedTriggeringPolicy() {
044    }
045
046    InvocationGate invocationGate = new DefaultInvocationGate();
047
048    public boolean isTriggeringEvent(final File activeFile, final E event) {
049        long now = System.currentTimeMillis();
050        if (invocationGate.isTooSoon(now))
051            return false;
052
053        return (activeFile.length() >= maxFileSize.getSize());
054    }
055
056    public FileSize getMaxFileSize() {
057        return this.maxFileSize;
058    }
059
060    public void setMaxFileSize(FileSize aMaxFileSize) {
061        this.maxFileSize = aMaxFileSize;
062    }
063
064}