This is an automated email from the ASF dual-hosted git repository.
ckozak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 70b0f41 LOG4J2-2598: GzCompressAction supports configurable
compression levels
new efa64bf Merge pull request #267 from carterkozak/LOG4J2-2598
70b0f41 is described below
commit 70b0f41d46c5416bc5d9627945b810d067e22794
Author: Carter Kozak <[email protected]>
AuthorDate: Tue Apr 30 13:06:39 2019 -0400
LOG4J2-2598: GzCompressAction supports configurable compression levels
Updated the implementation to use the BUF_SIZE constant for the
GZIPOutputStream output buffer as well.
---
.../log4j/core/appender/rolling/FileExtension.java | 2 +-
.../appender/rolling/action/GzCompressAction.java | 65 ++++++++++++++++++++--
src/changes/changes.xml | 6 ++
3 files changed, 67 insertions(+), 6 deletions(-)
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileExtension.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileExtension.java
index 9d0016f..d12d26a 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileExtension.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/FileExtension.java
@@ -39,7 +39,7 @@ public enum FileExtension {
@Override
Action createCompressAction(final String renameTo, final String
compressedName, final boolean deleteSource,
final int compressionLevel) {
- return new GzCompressAction(source(renameTo),
target(compressedName), deleteSource);
+ return new GzCompressAction(source(renameTo),
target(compressedName), deleteSource, compressionLevel);
}
},
BZIP2(".bz2") {
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/GzCompressAction.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/GzCompressAction.java
index 1c9b082..5ee0ff3 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/GzCompressAction.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/GzCompressAction.java
@@ -21,7 +21,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.util.Objects;
+import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;
/**
@@ -47,20 +49,41 @@ public final class GzCompressAction extends AbstractAction {
private final boolean deleteSource;
/**
+ * GZIP compression level to use.
+ *
+ * @see Deflater#setLevel(int)
+ */
+ private final int compressionLevel;
+
+ /**
* Create new instance of GzCompressAction.
*
* @param source file to compress, may not be null.
* @param destination compressed file, may not be null.
* @param deleteSource if true, attempt to delete file on completion.
Failure to delete
* does not cause an exception to be thrown or affect
return value.
+ * @param compressionLevel
+ * Gzip deflater compression level.
*/
- public GzCompressAction(final File source, final File destination, final
boolean deleteSource) {
+ public GzCompressAction(
+ final File source, final File destination, final boolean
deleteSource, final int compressionLevel) {
Objects.requireNonNull(source, "source");
Objects.requireNonNull(destination, "destination");
this.source = source;
this.destination = destination;
this.deleteSource = deleteSource;
+ this.compressionLevel = compressionLevel;
+ }
+
+ /**
+ * Prefer the constructor with compression level.
+ *
+ * @deprecated Prefer {@link GzCompressAction#GzCompressAction(File, File,
boolean, int)}.
+ */
+ @Deprecated
+ public GzCompressAction(final File source, final File destination, final
boolean deleteSource) {
+ this(source, destination, deleteSource, Deflater.DEFAULT_COMPRESSION);
}
/**
@@ -71,7 +94,7 @@ public final class GzCompressAction extends AbstractAction {
*/
@Override
public boolean execute() throws IOException {
- return execute(source, destination, deleteSource);
+ return execute(source, destination, deleteSource, compressionLevel);
}
/**
@@ -83,13 +106,38 @@ public final class GzCompressAction extends AbstractAction
{
* does not cause an exception to be thrown or affect
return value.
* @return true if source file compressed.
* @throws IOException on IO exception.
+ * @deprecated In favor of {@link #execute(File, File, boolean, int)}.
*/
+ @Deprecated
public static boolean execute(final File source, final File destination,
final boolean deleteSource)
throws IOException {
+ return execute(source, destination, deleteSource,
Deflater.DEFAULT_COMPRESSION);
+ }
+
+ /**
+ * Compress a file.
+ *
+ * @param source file to compress, may not be null.
+ * @param destination compressed file, may not be null.
+ * @param deleteSource if true, attempt to delete file on completion.
Failure to delete
+ * does not cause an exception to be thrown or affect
return value.
+ * @param compressionLevel
+ * Gzip deflater compression level.
+ * @return true if source file compressed.
+ * @throws IOException on IO exception.
+ */
+ public static boolean execute(
+ final File source,
+ final File destination,
+ final boolean deleteSource,
+ final int compressionLevel) throws IOException {
if (source.exists()) {
try (final FileInputStream fis = new FileInputStream(source);
- final BufferedOutputStream os = new
BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(
- destination)))) {
+ final OutputStream fos = new FileOutputStream(destination);
+ final OutputStream gzipOut = new
ConfigurableLevelGZIPOutputStream(
+ fos, BUF_SIZE, compressionLevel);
+ // Reduce native invocations by buffering data into
GZIPOutputStream
+ final OutputStream os = new BufferedOutputStream(gzipOut,
BUF_SIZE)) {
final byte[] inbuf = new byte[BUF_SIZE];
int n;
@@ -99,7 +147,7 @@ public final class GzCompressAction extends AbstractAction {
}
if (deleteSource && !source.delete()) {
- LOGGER.warn("Unable to delete " + source.toString() + '.');
+ LOGGER.warn("Unable to delete {}.", source);
}
return true;
@@ -108,6 +156,13 @@ public final class GzCompressAction extends AbstractAction
{
return false;
}
+ private static final class ConfigurableLevelGZIPOutputStream extends
GZIPOutputStream {
+
+ ConfigurableLevelGZIPOutputStream(OutputStream out, int bufSize, int
level) throws IOException {
+ super(out, bufSize);
+ def.setLevel(level);
+ }
+ }
/**
* Capture exception.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a73f951..8d31c38 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -382,6 +382,9 @@
<action issue="LOG4J2-2337" dev="ggregory" type="add" due-to="Arvind
Sahare, Patrice Ferrot">
Allow custom end-of-line with JsonLayout.
</action>
+ <action issue="LOG4J2-2598" dev="ckozak" type="add" due-to="Carter
Kozak">
+ GZIP compression on rollover supports configurable compression levels.
+ </action>
</release>
<release version="2.12.0" date="2019-MM-DD" description="GA Release
2.12.0">
<action issue="LOG4J2-2586" dev="rgoers" type="add">
@@ -402,6 +405,9 @@
<action issue="LOG4J2-2337" dev="ggregory" type="add" due-to="Arvind
Sahare, Patrice Ferrot">
Allow custom end-of-line with JsonLayout.
</action>
+ <action issue="LOG4J2-2598" dev="ckozak" type="add" due-to="Carter
Kozak">
+ GZIP compression on rollover supports configurable compression levels.
+ </action>
</release>
<release version="2.11.2" date="2018-MM-DD" description="GA Release
2.11.2">
<action issue="LOG4J2-2500" dev="rgoers" type="fix">