1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core.rolling;
15
16 import java.io.File;
17
18 import ch.qos.logback.core.util.FileSize;
19 import ch.qos.logback.core.util.DefaultInvocationGate;
20 import ch.qos.logback.core.util.InvocationGate;
21
22 /**
23 * SizeBasedTriggeringPolicy looks at size of the file being currently written
24 * to. If it grows bigger than the specified size, the FileAppender using the
25 * SizeBasedTriggeringPolicy rolls the file and creates a new one.
26 *
27 * For more information about this policy, please refer to the online manual at
28 * http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
29 *
30 * @author Ceki Gülcü
31 *
32 */
33 public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
34
35 public static final String SEE_SIZE_FORMAT = "http://logback.qos.ch/codes.html#sbtp_size_format";
36 /**
37 * The default maximum file size.
38 */
39 public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
40
41 FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
42
43 public SizeBasedTriggeringPolicy() {
44 }
45
46 InvocationGate invocationGate = new DefaultInvocationGate();
47
48 public boolean isTriggeringEvent(final File activeFile, final E event) {
49 long now = System.currentTimeMillis();
50 if (invocationGate.isTooSoon(now))
51 return false;
52
53 return (activeFile.length() >= maxFileSize.getSize());
54 }
55
56 public FileSize getMaxFileSize() {
57 return this.maxFileSize;
58 }
59
60 public void setMaxFileSize(FileSize aMaxFileSize) {
61 this.maxFileSize = aMaxFileSize;
62 }
63
64 }