Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move compression into new log4j-compress module #2921

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions log4j-compress/.log4j-plugin-processing-activator
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is here to activate the `plugin-processing` Maven profile.
177 changes: 177 additions & 0 deletions log4j-compress/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>${revision}</version>
<relativePath>../log4j-parent</relativePath>
</parent>

<artifactId>log4j-compress</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loudly: Shouldn't we rather rename this to log4j-compress-commons, in case we want to support another 3rd party library in the future? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be prudent, but I would prefer 3rd party compression libraries to integrate with Commons Compress instead of us. I did add a custom CompressorStreamProvider to prove that 3rd party extensions to Commons Compress can be used by this module.

While writing this PR I also looked if we can integrate CLP through this mechanism. The answer is no: CLP would require us to compress each log event separately, it is not useful to compress already written log files (at least without parsing them again).

<name>Apache Log4j Core: Extended Compression Support</name>
<description>Add additional compression formats to Log4j Core.</description>

<properties>
<commons-compress.version>1.27.1</commons-compress.version>
<jimfs.version>1.3.0</jimfs.version>
<xz.version>1.10</xz.version>
<zstd.version>1.5.6-5</zstd.version>
</properties>

<dependencyManagement>
<dependencies>

<!-- Dummy dependency to force Dependabot to upgrade this entry -->
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>${xz.version}</version>
</dependency>

<!-- Dummy dependency to force Dependabot to upgrade this entry -->
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>${zstd.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-plugins</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<excludes>
<exclude>org/apache/logging/log4j/compress/commons/xz/*</exclude>
<exclude>org/apache/logging/log4j/compress/commons/zstd/*</exclude>
</excludes>
</configuration>
</execution>
<!--
~ The XZ algorithm requires additional Commons Compress dependencies
-->
<execution>
<id>test-xz</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<additionalClasspathDependencies>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>${xz.version}</version>
</dependency>
</additionalClasspathDependencies>
<includes>
<include>org/apache/logging/log4j/compress/commons/xz/*Test.class</include>
ppkarwasz marked this conversation as resolved.
Show resolved Hide resolved
</includes>
</configuration>
</execution>
<!--
~ The XZ algorithm requires additional Commons Compress dependencies
-->
<execution>
<id>test-zstd</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<additionalClasspathDependencies>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>${zstd.version}</version>
</dependency>
</additionalClasspathDependencies>
<includes>
<include>org/apache/logging/log4j/compress/commons/zstd/*Test.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.compress.commons;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Objects;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamProvider;
import org.apache.logging.log4j.core.appender.rolling.action.AbstractCompressAction;

/**
* Compresses a file using bzip2 compression.
*/
final class CommonsCompressAction extends AbstractCompressAction {

private final CompressorStreamProvider provider;

/**
* Compressor name. One of "gz", "bzip2", "xz", "pack200" or "deflate".
*/
private final String name;

/**
* Creates new instance of Bzip2CompressAction.
*
* @param name The compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
* @param source The file to compress, may not be null.
* @param destination The compressed file, may not be null.
*/
CommonsCompressAction(
final CompressorStreamProvider provider, final String name, final Path source, final Path destination) {
super(source, destination);
this.provider = Objects.requireNonNull(provider);
this.name = Objects.requireNonNull(name, "name");
}

@Override
protected OutputStream wrapOutputStream(OutputStream stream) throws IOException {
try {
return new BufferedOutputStream(provider.createCompressorOutputStream(name, stream), BUF_SIZE);
} catch (final CompressorException error) {
final String message = String.format("failed to wrap the output stream with the `%s` compressor", name);
throw new IOException(message, error);
}
}

@Override
protected String getAlgorithmName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.compress.commons;

import java.nio.file.Path;
import java.util.Map;
import org.apache.commons.compress.compressors.CompressorStreamProvider;
import org.apache.logging.log4j.core.appender.rolling.action.Action;
import org.apache.logging.log4j.core.appender.rolling.action.CompressActionFactory;
import org.jspecify.annotations.NullMarked;

@NullMarked
final class CommonsCompressActionFactory implements CompressActionFactory {

private final CompressorStreamProvider provider;

private final String name;

CommonsCompressActionFactory(final String name, final CompressorStreamProvider provider) {
this.name = name;
this.provider = provider;
}

@Override
public Action createCompressAction(final Path source, final Path destination, final Map<String, String> ignored) {
return new CommonsCompressAction(provider, name, source, destination);
}

@Override
public String getAlgorithmName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.compress.commons;

import static org.apache.logging.log4j.util.Strings.toRootLowerCase;
import static org.apache.logging.log4j.util.Strings.toRootUpperCase;

import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.compress.compressors.CompressorStreamProvider;
import org.apache.commons.compress.compressors.lzma.LZMAUtils;
import org.apache.commons.compress.compressors.xz.XZUtils;
import org.apache.commons.compress.compressors.zstandard.ZstdUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.appender.rolling.action.CompressActionFactory;
import org.apache.logging.log4j.core.appender.rolling.action.CompressActionFactoryProvider;
import org.apache.logging.log4j.plugins.Namespace;
import org.apache.logging.log4j.plugins.Ordered;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.status.StatusLogger;
import org.jspecify.annotations.Nullable;

@Plugin
@Namespace(CompressActionFactoryProvider.NAMESPACE)
@Ordered(0)
public final class CommonsCompressActionFactoryProvider implements CompressActionFactoryProvider {

private static final Logger LOGGER = StatusLogger.getLogger();
private static final String COMMONS_COMPRESS_DOCUMENTATION =
"See https://commons.apache.org/proper/commons-compress/index.html for more information.";

private final CompressorStreamFactory factory = new CompressorStreamFactory();

private static void missingDependencyWarning(final String algorithm) {
LOGGER.warn(
"{} compression is not available due to a missing dependency. {}",
algorithm,
COMMONS_COMPRESS_DOCUMENTATION);
}

private @Nullable CompressorStreamProvider getCompressorStreamProvider(String algorithm) {
switch (toRootLowerCase(algorithm)) {
case CompressorStreamFactory.LZMA:
if (!LZMAUtils.isLZMACompressionAvailable()) {
missingDependencyWarning("LZMA");
return null;
}
break;
case CompressorStreamFactory.PACK200:
LOGGER.warn("Pack200 compression is not suitable for log files and will not be used.");
return null;
case CompressorStreamFactory.XZ:
if (!XZUtils.isXZCompressionAvailable()) {
missingDependencyWarning("XZ");
return null;
}
break;
case CompressorStreamFactory.ZSTANDARD:
if (!ZstdUtils.isZstdCompressionAvailable()) {
missingDependencyWarning("Zstd");
return null;
}
break;
}
// Commons Compress uses upper case keys.
return CompressorStreamFactory.findAvailableCompressorOutputStreamProviders()
.get(toRootUpperCase(algorithm));
}

@Override
public @Nullable CompressActionFactory createFactoryForAlgorithm(String algorithm) {
CompressorStreamProvider provider = getCompressorStreamProvider(algorithm);
if (provider != null) {
return new CommonsCompressActionFactory(algorithm, provider);
}
return null;
}
}
Loading
Loading