-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add Log4j 1 to Log4j 2 configuration file conversion #202
Draft
ppkarwasz
wants to merge
11
commits into
openrewrite:main
Choose a base branch
from
ppkarwasz:feature/log4j1-to-log4j-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
367362b
Add Log4j 1 to Log4j 2 configuration file conversion
ppkarwasz a33dea0
Fix licenses
ppkarwasz 0bd6cf9
Fix imports
ppkarwasz 81ce466
Use `repository.apache.org` only for snapshots
ppkarwasz 9cc4a8b
Limit requests to `repository.apache.org`
ppkarwasz e1a4b99
Try guessing bot suggestions
ppkarwasz 599234e
Reverts changes to `Slf4jToLog4jTest`
ppkarwasz 608f1c9
Merge branch 'main' into feature/log4j1-to-log4j-core
timtebeek 4d9a3bc
Run `./gradlew downloadRecipeDependencies` to update jars
timtebeek 5339812
Light polish
timtebeek 6cb469a
Update classes to expect a newer version of the log4j jars
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,12 @@ recipeDependencies { | |||||
parserClasspath("org.projectlombok:lombok:1.18.+") | ||||||
} | ||||||
|
||||||
repositories { | ||||||
mavenCentral() | ||||||
mavenLocal() | ||||||
maven("https://repository.apache.org/snapshots") | ||||||
} | ||||||
|
||||||
dependencies { | ||||||
compileOnly("org.projectlombok:lombok:latest.release") | ||||||
annotationProcessor("org.projectlombok:lombok:latest.release") | ||||||
|
@@ -27,13 +33,15 @@ dependencies { | |||||
|
||||||
implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}")) | ||||||
implementation("org.openrewrite:rewrite-java") | ||||||
implementation("org.openrewrite:rewrite-maven") | ||||||
implementation("org.openrewrite.recipe:rewrite-java-dependencies:${rewriteVersion}") | ||||||
implementation("org.openrewrite.recipe:rewrite-static-analysis:${rewriteVersion}") | ||||||
runtimeOnly("org.openrewrite:rewrite-java-17") | ||||||
|
||||||
implementation("log4j:log4j:1.+") | ||||||
implementation("org.apache.logging.log4j:log4j-core:2.+") | ||||||
implementation("org.apache.logging.log4j:log4j-core:2.24.3") | ||||||
implementation("org.slf4j:slf4j-api:2.+") | ||||||
implementation("org.apache.logging.log4j:log4j-converter-config:0.3.0-SNAPSHOT") | ||||||
|
||||||
annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
implementation("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
|
@@ -46,7 +54,7 @@ dependencies { | |||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:latest.release") | ||||||
|
||||||
testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}") | ||||||
testImplementation("org.openrewrite:rewrite-maven") | ||||||
testImplementation("org.openrewrite:rewrite-properties:${rewriteVersion}") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rewrite-bom manages the version here, so we can leave that out already.
Suggested change
|
||||||
testImplementation("org.openrewrite:rewrite-test") | ||||||
testImplementation("org.openrewrite:rewrite-java-tck") | ||||||
|
||||||
|
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; | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import lombok.AllArgsConstructor; | ||
import org.apache.logging.converter.config.ConfigurationConverter; | ||
import org.jspecify.annotations.Nullable; | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
*/ | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@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) | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@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"; | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@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); | ||
} | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@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); | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
} | ||
} |
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; | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.junit.jupiter.api.Test; | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(""" | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed likely best to merge this once released; thanks for creating the recipe before then!