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 @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
72
73
74
75
76
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
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
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 }