18
18
import ch .qos .logback .core .rolling .helper .FileNamePattern ;
19
19
import ch .qos .logback .core .spi .ContextAwareBase ;
20
20
21
+ import static ch .qos .logback .core .util .Loader .isClassLoadable ;
22
+
21
23
/**
22
24
* Implements methods common to most, it not all, rolling policies. Currently
23
25
* such methods are limited to a compression mode getter/setter.
24
- *
26
+ *
25
27
* @author Ceki Gülcü
26
28
*/
27
29
public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
@@ -40,18 +42,18 @@ public abstract class RollingPolicyBase extends ContextAwareBase implements Roll
40
42
/**
41
43
* Given the FileNamePattern string, this method determines the compression mode
42
44
* depending on last letters of the fileNamePatternStr. Patterns ending with .gz
43
- * imply GZIP compression, endings with '.zip' imply ZIP compression. Otherwise
44
- * and by default, there is no compression.
45
- *
45
+ * imply GZIP compression, endings with '.zip' imply ZIP compression, endings with
46
+ * .xz imply XZ compression. Otherwise and by default, there is no compression.
47
+ *
46
48
*/
47
49
protected void determineCompressionMode () {
48
- if (fileNamePatternStr .endsWith (".gz" )) {
50
+ if (fileNamePatternStr .endsWith (CompressionMode . GZ_SUFFIX )) {
49
51
addInfo ("Will use gz compression" );
50
52
compressionMode = CompressionMode .GZ ;
51
- } else if (fileNamePatternStr .endsWith (".zip" )) {
53
+ } else if (fileNamePatternStr .endsWith (CompressionMode . ZIP_SUFFIX )) {
52
54
addInfo ("Will use zip compression" );
53
55
compressionMode = CompressionMode .ZIP ;
54
- } else if (fileNamePatternStr .endsWith (".xz" )) {
56
+ } else if (fileNamePatternStr .endsWith (CompressionMode . XZ_SUFFIX )) {
55
57
addInfo ("Will use xz compression" );
56
58
compressionMode = CompressionMode .XZ ;
57
59
} else {
@@ -60,6 +62,30 @@ protected void determineCompressionMode() {
60
62
}
61
63
}
62
64
65
+ /**
66
+ * If compression mode is XZ but the XZ librarey is missing, then fallback to GZ compresison.
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
+ // unreachable code
85
+ throw new IllegalArgumentException ("[" + input + "] should end with " +existingSuffix );
86
+ }
87
+ }
88
+
63
89
public void setFileNamePattern (String fnp ) {
64
90
fileNamePatternStr = fnp ;
65
91
}
0 commit comments