Skip to content

Commit fb45971

Browse files
committed
add support for XZ compression
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 31c1f55 commit fb45971

File tree

7 files changed

+40
-52
lines changed

7 files changed

+40
-52
lines changed

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void rollover() throws RolloverFailure {
151151
util.rename(getActiveFileName(), fileNamePattern.convertInt(minIndex));
152152
break;
153153
case GZ:
154+
case XZ:
154155
compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex), null);
155156
break;
156157
case ZIP:

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/RollingPolicyBase.java

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ protected void determineCompressionMode() {
5151
} else if (fileNamePatternStr.endsWith(".zip")) {
5252
addInfo("Will use zip compression");
5353
compressionMode = CompressionMode.ZIP;
54+
} else if (fileNamePatternStr.endsWith(".xz")) {
55+
addInfo("Will use xz compression");
56+
compressionMode = CompressionMode.XZ;
5457
} else {
5558
addInfo("No compression will be used");
5659
compressionMode = CompressionMode.NONE;

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionStrategy.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
import ch.qos.logback.core.spi.ContextAware;
1818

19+
/**
20+
* This interface was introduced in order to support for pluggable
21+
* compression methods.
22+
*
23+
* @author Ceki G&uuml;lc&uuml
24+
* @since 1.5.18
25+
*/
1926
public interface CompressionStrategy extends ContextAware {
2027

2128
void compress(String originalFileName, String compressedFileName, String innerEntryName);
22-
23-
//String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr);
2429
}

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java

-31
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,6 @@ private CompressionStrategy dynamicInstantiation(String className) {
8282
}
8383
}
8484

85-
// http://jira.qos.ch/browse/LBCORE-98
86-
// The name of the compressed file as nested within the zip archive
87-
//
88-
// Case 1: RawFile = null, Pattern = foo-%d.zip
89-
// nestedFilename = foo-${current-date}
90-
//
91-
// Case 2: RawFile = hello.txt, Pattern = = foo-%d.zip
92-
// nestedFilename = foo-${current-date}
93-
//
94-
// in both cases, the strategy consisting of removing the compression
95-
// suffix of zip file works reasonably well. The alternative strategy
96-
// whereby the nested file name was based on the value of the raw file name
97-
// (applicable to case 2 only) has the disadvantage of the nested files
98-
// all having the same name, which could make it harder for the user
99-
// to unzip the file without collisions
100-
// ZipEntry computeZipEntry(File zippedFile) {
101-
// return computeZipEntry(zippedFile.getName());
102-
// }
103-
//
104-
// ZipEntry computeZipEntry(String filename) {
105-
// String nameOfFileNestedWithinArchive = computeFileNameStrWithoutCompSuffix(filename, compressionMode);
106-
// return new ZipEntry(nameOfFileNestedWithinArchive);
107-
// }
108-
10985
static public String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr, CompressionMode compressionMode) {
11086
int len = fileNamePatternStr.length();
11187
switch (compressionMode) {
@@ -130,13 +106,6 @@ static public String computeFileNameStrWithoutCompSuffix(String fileNamePatternS
130106
throw new IllegalStateException("Execution should not reach this point");
131107
}
132108

133-
// void createMissingTargetDirsIfNecessary(File file) {
134-
// boolean result = FileUtil.createMissingParentDirectories(file);
135-
// if (!result) {
136-
// addError("Failed to create parent directories for [" + file.getAbsolutePath() + "]");
137-
// }
138-
// }
139-
140109
@Override
141110
public String toString() {
142111
return this.getClass().getName();

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/helper/GZCompressionStrategy.java

-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
public class GZCompressionStrategy extends CompressionStrategyBase {
2727

2828

29-
3029
@Override
3130
public void compress(String originalFileName, String compressedFileName, String innerEntryName) {
3231

@@ -70,15 +69,6 @@ public void compress(String originalFileName, String compressedFileName, String
7069
if (!file2gz.delete()) {
7170
addStatus(new WarnStatus("Could not delete [" + originalFileName + "].", this));
7271
}
73-
7472
}
7573

76-
// @Override
77-
// public String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr) {
78-
// int len = fileNamePatternStr.length();
79-
// if (fileNamePatternStr.endsWith(".gz"))
80-
// return fileNamePatternStr.substring(0, len - 3);
81-
// else
82-
// return fileNamePatternStr;
83-
// }
8474
}

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/helper/XZCompressionStrategy.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2025, 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 v1.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+
*/
114
package ch.qos.logback.core.rolling.helper;
215

316
import java.io.BufferedInputStream;
@@ -7,6 +20,15 @@
720
import org.tukaani.xz.LZMA2Options;
821
import org.tukaani.xz.XZOutputStream;
922

23+
/**
24+
* Compresses files using {@link org.tukaani.xz xz} library.
25+
*
26+
* <p>Note that </p>
27+
*
28+
* @author Marian Kazimir
29+
* @author Ceki G&uuml;lc&uuml;
30+
* @since 1.5.18
31+
*/
1032
public class XZCompressionStrategy extends CompressionStrategyBase {
1133

1234
@Override

Diff for: logback-core/src/main/java/ch/qos/logback/core/rolling/helper/ZipCompressionStrategy.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
import java.util.zip.ZipEntry;
2525
import java.util.zip.ZipOutputStream;
2626

27+
/**
28+
* Compresses files using JDK's Zip compression algorithm.
29+
*
30+
* @author Ceki G&uuml;lc&uuml;
31+
* @since 1.5.18
32+
*/
2733
public class ZipCompressionStrategy extends CompressionStrategyBase {
2834
static final int BUFFER_SIZE = 8192;
2935

@@ -80,14 +86,6 @@ public void compress(String originalFileName, String compressedFileName, String
8086
}
8187
}
8288

83-
private String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr) {
84-
int len = fileNamePatternStr.length();
85-
if (fileNamePatternStr.endsWith(".zip"))
86-
return fileNamePatternStr.substring(0, len - 4);
87-
else
88-
return fileNamePatternStr;
89-
}
90-
9189
// http://jira.qos.ch/browse/LBCORE-98
9290
// The name of the compressed file as nested within the zip archive
9391
//
@@ -108,7 +106,7 @@ private String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr) {
108106
//}
109107

110108
ZipEntry computeZipEntry(String filename) {
111-
String nameOfFileNestedWithinArchive = computeFileNameStrWithoutCompSuffix(filename);
109+
String nameOfFileNestedWithinArchive = Compressor.computeFileNameStrWithoutCompSuffix(filename, CompressionMode.ZIP);
112110
return new ZipEntry(nameOfFileNestedWithinArchive);
113111
}
114112
}

0 commit comments

Comments
 (0)