View Javadoc
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.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  import static ch.qos.logback.core.util.Loader.isClassLoadable;
22  
23  /**
24   * Implements methods common to most, it not all, rolling policies. Currently
25   * such methods are limited to a compression mode getter/setter.
26   *
27   * @author Ceki Gülcü
28   */
29  public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
30      protected CompressionMode compressionMode = CompressionMode.NONE;
31  
32      FileNamePattern fileNamePattern;
33      // fileNamePatternStr is always slashified, see setter
34      protected String fileNamePatternStr;
35  
36      private FileAppender<?> parent;
37  
38      // use to name files within zip file, i.e. the zipEntry
39      FileNamePattern zipEntryFileNamePattern;
40      private boolean started;
41  
42  
43      /**
44       * @deprecated replaced by #fileNamePatternStrToCompressionMode(String) followed by #outputCompressionModeMessage(CompressionMode)
45       */
46      @Deprecated
47      protected void determineCompressionMode() {
48          compressionMode = fileNamePatternStrToCompressionMode(fileNamePatternStr);
49          outputCompressionModeMessage(compressionMode);
50      }
51  
52      protected void outputCompressionModeMessage(CompressionMode compressionMode) {
53  
54          switch (compressionMode) {
55              case GZ:
56                  addInfo("Will use gz compression");
57                  break;
58              case ZIP:
59                  addInfo("Will use zip compression");
60                  break;
61              case XZ:
62                  addInfo("Will use xz compression");
63                  break;
64              case NONE:
65                  addInfo("No compression will be used");
66                  break;
67          }
68      }
69  
70      /**
71       * Given the FileNamePattern string, this method determines the compression mode
72       * depending on last letters of the fileNamePatternStr. Patterns ending with .gz
73       * imply GZIP compression, endings with '.zip' imply ZIP compression, endings with
74       * .xz imply XZ compression. Otherwise and by default, there is no compression.
75       *
76       * @since 1.6.1
77       */
78      protected CompressionMode fileNamePatternStrToCompressionMode(String aFileNamePatternStr) {
79          if (aFileNamePatternStr.endsWith(CompressionMode.GZ_SUFFIX)) {
80              return CompressionMode.GZ;
81          } else if (aFileNamePatternStr.endsWith(CompressionMode.ZIP_SUFFIX)) {
82              return CompressionMode.ZIP;
83          } else if (aFileNamePatternStr.endsWith(CompressionMode.XZ_SUFFIX)) {
84              return CompressionMode.XZ;
85          } else {
86              return CompressionMode.NONE;
87          }
88      }
89  
90  
91      /**
92       * If compression mode is XZ but the XZ library is missing, then fallback to GZ compression.
93       */
94      protected void adjustCompressionModeAndFileNamePatternStrIfNecessary() {
95          if (compressionMode == compressionMode.XZ) {
96              boolean xzLibraryLoadable = isClassLoadable("org.tukaani.xz.XZOutputStream", getContext());
97              if (!xzLibraryLoadable) {
98                  addWarn("XZ library missing, falling back to GZ compression");
99                  compressionMode = CompressionMode.GZ;
100                 fileNamePatternStr = replaceSuffix(fileNamePatternStr, CompressionMode.XZ_SUFFIX, CompressionMode.GZ_SUFFIX);
101             }
102         }
103     }
104 
105     private String replaceSuffix(String input, String existingSuffix, String newSuffix) {
106         int existingSuffixLen = existingSuffix.length();
107         if (input.endsWith(existingSuffix)) {
108             return input.substring(0, input.length() - existingSuffixLen) + newSuffix;
109         } else {
110             // unreachable code
111             throw new IllegalArgumentException("[" + input + "] should end with "+existingSuffix);
112         }
113     }
114 
115     public void setFileNamePattern(String fnp) {
116         fileNamePatternStr = fnp;
117     }
118 
119     public String getFileNamePattern() {
120         return fileNamePatternStr;
121     }
122 
123     public CompressionMode getCompressionMode() {
124         return compressionMode;
125     }
126 
127     public boolean isStarted() {
128         return started;
129     }
130 
131     public void start() {
132         started = true;
133     }
134 
135     public void stop() {
136         started = false;
137     }
138 
139     public void setParent(FileAppender<?> appender) {
140         this.parent = appender;
141     }
142 
143     public boolean isParentPrudent() {
144         return parent.isPrudent();
145     }
146 
147     public String getParentsRawFileProperty() {
148         return parent.rawFileProperty();
149     }
150 }