1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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 ch.qos.logback.core.rolling.SizeAndTimeBasedFileNamingAndTriggeringPolicy.Usage;
17  import ch.qos.logback.core.util.FileSize;
18  
19  public class SizeAndTimeBasedRollingPolicy<E> extends TimeBasedRollingPolicy<E> {
20  
21      static long MAX_COMPRESSION_FACTOR = 20;
22  
23      FileSize maxFileSize;
24  
25      @Override
26      public void start() {
27          SizeAndTimeBasedFileNamingAndTriggeringPolicy<E> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFileNamingAndTriggeringPolicy<E>(Usage.EMBEDDED);
28          if (maxFileSize == null) {
29              addError("maxFileSize property is mandatory.");
30              return;
31          } else {
32              addInfo("Archive files will be limited to [" + maxFileSize + "] each.");
33          }
34  
35          sizeAndTimeBasedFNATP.setMaxFileSize(maxFileSize);
36          timeBasedFileNamingAndTriggeringPolicy = sizeAndTimeBasedFNATP;
37  
38          if (!isUnboundedTotalSizeCap() && totalSizeCap.getSize() < maxFileSize.getSize()/MAX_COMPRESSION_FACTOR) {
39              addWarn("totalSizeCap of [" + totalSizeCap + "] is much smaller than maxFileSize [" + maxFileSize
40                      + "] which is non-sensical, even taking compression into account.");
41          }
42  
43          // most work is done by the parent
44          super.start();
45      }
46  
47      public void setMaxFileSize(FileSize aMaxFileSize) {
48          this.maxFileSize = aMaxFileSize;
49      }
50  
51      @Override
52      public String toString() {
53          return "c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@" + this.hashCode();
54      }
55  }