1
2
3
4
5
6
7
8
9
10
11
12
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
25
26
27
28
29 public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
30 protected CompressionMode compressionMode = CompressionMode.NONE;
31
32 FileNamePattern fileNamePattern;
33
34 protected String fileNamePatternStr;
35
36 private FileAppender<?> parent;
37
38
39 FileNamePattern zipEntryFileNamePattern;
40 private boolean started;
41
42
43
44
45
46
47
48
49 protected void determineCompressionMode() {
50 if (fileNamePatternStr.endsWith(CompressionMode.GZ_SUFFIX)) {
51 addInfo("Will use gz compression");
52 compressionMode = CompressionMode.GZ;
53 } else if (fileNamePatternStr.endsWith(CompressionMode.ZIP_SUFFIX)) {
54 addInfo("Will use zip compression");
55 compressionMode = CompressionMode.ZIP;
56 } else if (fileNamePatternStr.endsWith(CompressionMode.XZ_SUFFIX)) {
57 addInfo("Will use xz compression");
58 compressionMode = CompressionMode.XZ;
59 } else {
60 addInfo("No compression will be used");
61 compressionMode = CompressionMode.NONE;
62 }
63 }
64
65
66
67
68 protected void adjustCompressionModeAndFileNamePatternStrIfNecessary() {
69 if (compressionMode == compressionMode.XZ) {
70 boolean xzLibraryLoadable = isClassLoadable("org.tukaani.xz.XZOutputStream", getContext());
71 if (!xzLibraryLoadable) {
72 addWarn("XZ library missing, falling back to GZ compression");
73 compressionMode = CompressionMode.GZ;
74 fileNamePatternStr = replaceSuffix(fileNamePatternStr, CompressionMode.XZ_SUFFIX, CompressionMode.GZ_SUFFIX);
75 }
76 }
77 }
78
79 private String replaceSuffix(String input, String existingSuffix, String newSuffix) {
80 int existingSuffixLen = existingSuffix.length();
81 if (input.endsWith(existingSuffix)) {
82 return input.substring(0, input.length() - existingSuffixLen) + newSuffix;
83 } else {
84
85 throw new IllegalArgumentException("[" + input + "] should end with "+existingSuffix);
86 }
87 }
88
89 public void setFileNamePattern(String fnp) {
90 fileNamePatternStr = fnp;
91 }
92
93 public String getFileNamePattern() {
94 return fileNamePatternStr;
95 }
96
97 public CompressionMode getCompressionMode() {
98 return compressionMode;
99 }
100
101 public boolean isStarted() {
102 return started;
103 }
104
105 public void start() {
106 started = true;
107 }
108
109 public void stop() {
110 started = false;
111 }
112
113 public void setParent(FileAppender<?> appender) {
114 this.parent = appender;
115 }
116
117 public boolean isParentPrudent() {
118 return parent.isPrudent();
119 }
120
121 public String getParentsRawFileProperty() {
122 return parent.rawFileProperty();
123 }
124 }