View Javadoc
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 ch.qos.logback.core.FileAppender;
17  import ch.qos.logback.core.rolling.helper.CompressionMode;
18  import ch.qos.logback.core.rolling.helper.FileNamePattern;
19  import ch.qos.logback.core.spi.ContextAwareBase;
20  
21  /**
22   * Implements methods common to most, it not all, rolling policies. Currently
23   * such methods are limited to a compression mode getter/setter.
24   * 
25   * @author Ceki Gülcü
26   */
27  public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
28      protected CompressionMode compressionMode = CompressionMode.NONE;
29  
30      FileNamePattern fileNamePattern;
31      // fileNamePatternStr is always slashified, see setter
32      protected String fileNamePatternStr;
33  
34      private FileAppender<?> parent;
35  
36      // use to name files within zip file, i.e. the zipEntry
37      FileNamePattern zipEntryFileNamePattern;
38      private boolean started;
39  
40      /**
41       * Given the FileNamePattern string, this method determines the compression
42       * mode depending on last letters of the fileNamePatternStr. Patterns ending
43       * with .gz imply GZIP compression, endings with '.zip' imply ZIP compression.
44       * Otherwise and by default, there is no compression.
45       * 
46       */
47      protected void determineCompressionMode() {
48          if (fileNamePatternStr.endsWith(".gz")) {
49              addInfo("Will use gz compression");
50              compressionMode = CompressionMode.GZ;
51          } else if (fileNamePatternStr.endsWith(".zip")) {
52              addInfo("Will use zip compression");
53              compressionMode = CompressionMode.ZIP;
54          } else {
55              addInfo("No compression will be used");
56              compressionMode = CompressionMode.NONE;
57          }
58      }
59  
60      public void setFileNamePattern(String fnp) {
61          fileNamePatternStr = fnp;
62      }
63  
64      public String getFileNamePattern() {
65          return fileNamePatternStr;
66      }
67  
68      public CompressionMode getCompressionMode() {
69          return compressionMode;
70      }
71  
72      public boolean isStarted() {
73          return started;
74      }
75  
76      public void start() {
77          started = true;
78      }
79  
80      public void stop() {
81          started = false;
82      }
83  
84      public void setParent(FileAppender<?> appender) {
85          this.parent = appender;
86      }
87  
88      public boolean isParentPrudent() {
89          return parent.isPrudent();
90      }
91  
92      public String getParentsRawFileProperty() {
93          return parent.rawFileProperty();
94      }
95  }