View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.rolling;
11  
12  import ch.qos.logback.core.FileAppender;
13  import ch.qos.logback.core.rolling.helper.CompressionMode;
14  import ch.qos.logback.core.rolling.helper.FileNamePattern;
15  import ch.qos.logback.core.spi.ContextAwareBase;
16  
17  
18  
19  /**
20   * Implements methods common to most, it not all, rolling
21   * policies. Currently such methods are limited to a compression mode
22   * getter/setter.
23   *
24   * @author Ceki Gülcü
25   */
26  public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
27    protected CompressionMode compressionMode = CompressionMode.NONE;
28    protected FileNamePattern fileNamePattern;
29    protected String fileNamePatternStr;
30    
31    private FileAppender parent;
32    
33    private boolean started;
34    
35    /**
36     * Given the FileNamePattern string, this method determines the compression
37     * mode depending on last letters of the fileNamePatternStr. Patterns
38     * ending with .gz imply GZIP compression, endings with '.zip' imply
39     * ZIP compression. Otherwise and by default, there is no compression.
40     *
41     */
42    protected void determineCompressionMode() {
43       if (fileNamePatternStr.endsWith(".gz")) {
44        addInfo("Will use gz compression");
45        compressionMode = CompressionMode.GZ;
46      } else if (fileNamePatternStr.endsWith(".zip")) {
47        addInfo("Will use zip compression");
48        compressionMode = CompressionMode.ZIP;
49      } else {
50        addInfo("No compression will be used");
51        compressionMode = CompressionMode.NONE;
52      }
53    }
54  
55    public void setFileNamePattern(String fnp) {
56      fileNamePatternStr = fnp;
57    }
58  
59    public String getFileNamePattern() {
60      return fileNamePatternStr;
61    }
62    
63    public boolean isStarted() {
64      return started;
65    }
66  
67    public void start() {
68      started = true;
69    }
70    
71    public void stop() {
72      started = false;
73    }
74    
75    public void setParent(FileAppender appender) {
76      addInfo("Adding parent to RollingPolicy: " + appender.getName());
77      this.parent = appender;
78    }
79  
80    public boolean isParentPrudent() {
81      return parent.isPrudent();
82    }
83    
84    public String getParentsRawFileProperty() {
85      return parent.rawFileProperty();
86    }
87  }