forked from openrewrite/rewrite-logging-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Log4j 1 to Log4j 2 configuration file conversion
This PR uses the [Log4j Configuration Converter API](https://logging.staged.apache.org/log4j/transform/log4j-converter-config.html) to add a `Log4j1ConfigurationToLog4jCore2` rule, which converts `log4j.properties` and `log4j.xml` configuration files into the equivalent `log4j2.xml` configuration files. It also refactors the `Log4j1toLog4j2` recipe into three parts: - `Log4j1toLog4jAPI` performs the migration from the "Log4j 1 API" to Log4j API 2, as described in [Log4j 1 API migration](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html#api-migration). This recipe performs almost exclusively Java code changes and is usually not required in applications that use JCL or SLF4J as logging interface. - `Log4j1ConfigurationToLog4jCore2` migrates configuration files, as described in the [Log4j 1 Configuration file migration](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html#configuration-file-migration) section. - `Log4j1ToLog4jCore2` migrates the runtime dependencies to use Log4j Core 2 instead of Log4j 1, as described in [Log4j 1 Backend migration](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html#backend-migration) and also calls the previous recipe. This is probably the most useful recipe for users that no longer user Log4j 1 directly, but use it as logging implementation. Closes openrewrite#154. Related to apache/logging-log4j2#3220
- Loading branch information
Showing
7 changed files
with
295 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/org/openrewrite/java/logging/ConvertConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.openrewrite.java.logging; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.logging.converter.config.ConfigurationConverter; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.FindSourceFiles; | ||
import org.openrewrite.NlsRewrite.Description; | ||
import org.openrewrite.NlsRewrite.DisplayName; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.text.PlainText; | ||
|
||
/** | ||
* Converts a logging configuration file from one format to another. | ||
*/ | ||
@AllArgsConstructor | ||
public class ConvertConfiguration extends Recipe { | ||
|
||
@Option(displayName = "Pattern for the files to convert", | ||
description = "If set, only the files that match this pattern will be converted.", | ||
required = false) | ||
@Nullable | ||
String filePattern; | ||
|
||
@Option(displayName = "Input format", description = "The id of the input logging configuration format. See [Log4j documentation](https://logging.staged.apache.org/log4j/transform/log4j-converter-config.html#formats) for a list of supported formats.", example = "v1:properties") | ||
String inputFormat; | ||
|
||
@Option(displayName = "Output format", description = "The id of the output logging configuration format. See [Log4j documentation](https://logging.staged.apache.org/log4j/transform/log4j-converter-config.html#formats) for a list of supported formats.", example = "v2:xml") | ||
String outputFormat; | ||
|
||
private static final ConfigurationConverter converter = ConfigurationConverter.getInstance(); | ||
|
||
@Override | ||
public @DisplayName String getDisplayName() { | ||
return "Convert logging configuration"; | ||
} | ||
|
||
@Override | ||
public @Description String getDescription() { | ||
return "Converts the configuration of a logging backend from one format to another. For example it can convert a Log4j 1 properties configuration file into a Log4j Core 2 XML configuration file."; | ||
} | ||
|
||
@Override | ||
public int maxCycles() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return | ||
Preconditions.check(new FindSourceFiles(filePattern), | ||
new TreeVisitor<Tree, ExecutionContext>() { | ||
|
||
@Override | ||
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) { | ||
return super.isAcceptable(sourceFile, executionContext); | ||
} | ||
|
||
@Override | ||
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) { | ||
if (tree instanceof SourceFile) { | ||
SourceFile sourceFile = (SourceFile) tree; | ||
ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceFile.printAllAsBytes()); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
converter.convert(inputStream, inputFormat, outputStream, outputFormat); | ||
|
||
String utf8 = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); | ||
if (tree instanceof PlainText) { | ||
return ((PlainText) tree).withText(utf8).withCharset(StandardCharsets.UTF_8); | ||
} | ||
return PlainText.builder() | ||
.id(sourceFile.getId()) | ||
.charsetBomMarked(sourceFile.isCharsetBomMarked()) | ||
.charsetName(StandardCharsets.UTF_8.name()) | ||
.checksum(sourceFile.getChecksum()) | ||
.fileAttributes(sourceFile.getFileAttributes()) | ||
.markers(sourceFile.getMarkers()) | ||
.sourcePath(sourceFile.getSourcePath()) | ||
.text(utf8) | ||
.build(); | ||
} | ||
return super.visit(tree, executionContext); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/org/openrewrite/java/logging/ConvertConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.openrewrite.java.logging; | ||
|
||
import static org.openrewrite.test.SourceSpecs.text; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
class ConvertConfigurationTest implements RewriteTest { | ||
|
||
@DocumentExample | ||
@Test | ||
void convertsLog4j1ToLog4j2Configuration() { | ||
rewriteRun(spec -> spec.recipe(new ConvertConfiguration("file.txt", "v1:properties", "v2:xml")), | ||
text(""" | ||
# Console appender | ||
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender | ||
log4j.appender.CONSOLE.Follow = true | ||
log4j.appender.CONSOLE.Target = System.err | ||
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout | ||
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c - %m%n%ex | ||
# Rolling file appender | ||
log4j.appender.ROLLING = org.apache.log4j.RollingFileAppender | ||
log4j.appender.ROLLING.File = file.log | ||
log4j.appender.ROLLING.MaxBackupIndex = 30 | ||
# Exactly 10 GiB | ||
log4j.appender.ROLLING.MaxFileSize = 10737418240 | ||
log4j.appender.ROLLING.layout = org.apache.log4j.SimpleLayout | ||
# Loggers | ||
log4j.rootLogger = INFO, CONSOLE | ||
log4j.logger.org.openrewrite = DEBUG, CONSOLE, ROLLING | ||
log4j.additivity.org.openrewrite = false | ||
""", | ||
""" | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<Configuration xmlns="https://logging.apache.org/xml/ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-config-2.xsd"> | ||
<Properties/> | ||
<Appenders> | ||
<RollingFile append="true" bufferSize="8192" bufferedIo="false" fileName="file.log" filePattern="file.log.%i" immediateFlush="true" name="ROLLING"> | ||
<PatternLayout alwaysWriteExceptions="false" pattern="%p - %m%n"/> | ||
<SizeBasedTriggeringPolicy size="10.00 GB"/> | ||
<DefaultRolloverStrategy fileIndex="min" max="30"/> | ||
</RollingFile> | ||
<Console follow="true" immediateFlush="true" name="CONSOLE" target="SYSTEM_ERR"> | ||
<PatternLayout pattern="%d [%t] %-5p %c - %m%n%ex"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="INFO"> | ||
<AppenderRef ref="CONSOLE"/> | ||
</Root> | ||
<Logger additivity="false" level="DEBUG" name="org.openrewrite"> | ||
<AppenderRef ref="CONSOLE"/> | ||
<AppenderRef ref="ROLLING"/> | ||
</Logger> | ||
</Loggers> | ||
</Configuration> | ||
""")); | ||
} | ||
} |
Oops, something went wrong.