View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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
28      RollingPolicy {
29    protected CompressionMode compressionMode = CompressionMode.NONE;
30    protected FileNamePattern fileNamePattern;
31    // fileNamePatternStr is always slashified, see setter
32    protected String fileNamePatternStr;
33  
34    private FileAppender parent;
35  
36    private boolean started;
37  
38    /**
39     * Given the FileNamePattern string, this method determines the compression
40     * mode depending on last letters of the fileNamePatternStr. Patterns ending
41     * with .gz imply GZIP compression, endings with '.zip' imply ZIP compression.
42     * Otherwise and by default, there is no compression.
43     * 
44     */
45    protected void determineCompressionMode() {
46      if (fileNamePatternStr.endsWith(".gz")) {
47        addInfo("Will use gz compression");
48        compressionMode = CompressionMode.GZ;
49      } else if (fileNamePatternStr.endsWith(".zip")) {
50        addInfo("Will use zip compression");
51        compressionMode = CompressionMode.ZIP;
52      } else {
53        addInfo("No compression will be used");
54        compressionMode = CompressionMode.NONE;
55      }
56    }
57  
58    public void setFileNamePattern(String fnp) {
59      fileNamePatternStr = fnp;
60    }
61  
62    public String getFileNamePattern() {
63      return fileNamePatternStr;
64    }
65  
66    public CompressionMode getCompressionMode() {
67      return compressionMode;
68    }
69  
70    public boolean isStarted() {
71      return started;
72    }
73  
74    public void start() {
75      started = true;
76    }
77  
78    public void stop() {
79      started = false;
80    }
81  
82    public void setParent(FileAppender appender) {
83      this.parent = appender;
84    }
85  
86    public boolean isParentPrudent() {
87      return parent.isPrudent();
88    }
89  
90    public String getParentsRawFileProperty() {
91      return parent.rawFileProperty();
92    }
93  }