-
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
base: main
Are you sure you want to change the base?
Changes from all commits
367362b
a33dea0
0bd6cf9
81ce466
9cc4a8b
e1a4b99
599234e
608f1c9
4d9a3bc
5339812
6cb469a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,21 @@ recipeDependencies { | |||||
parserClasspath("org.projectlombok:lombok:1.18.+") | ||||||
} | ||||||
|
||||||
repositories { | ||||||
mavenCentral() | ||||||
mavenLocal() | ||||||
// Temporarily add Apache Snapshot repository for Log4j artifacts | ||||||
maven { | ||||||
setUrl("https://repository.apache.org/snapshots") | ||||||
mavenContent { | ||||||
// Excessive 404 result codes in `repository.apache.org` will ban the worker IP | ||||||
// https://infra.apache.org/infra-ban.html | ||||||
snapshotsOnly() | ||||||
excludeGroupAndSubgroups("org.openrewrite") | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
dependencies { | ||||||
compileOnly("org.projectlombok:lombok:latest.release") | ||||||
annotationProcessor("org.projectlombok:lombok:latest.release") | ||||||
|
@@ -27,12 +42,14 @@ 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("org.apache.logging.log4j:log4j-core:2.+") | ||||||
implementation("org.slf4j:slf4j-api:2.+") | ||||||
implementation("org.apache.logging.log4j:log4j-converter-config:0.3.0-SNAPSHOT") | ||||||
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. Indeed likely best to merge this once released; thanks for creating the recipe before then!
Suggested change
|
||||||
|
||||||
annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
implementation("org.openrewrite:rewrite-templating:$rewriteVersion") | ||||||
|
@@ -50,7 +67,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") | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* 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.openrewrite.java.logging; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.apache.logging.converter.config.ConfigurationConverter; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.*; | ||
import org.openrewrite.quark.Quark; | ||
import org.openrewrite.remote.Remote; | ||
import org.openrewrite.text.PlainTextParser; | ||
import org.openrewrite.tree.ParseError; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* 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.", | ||
example = "**/log4j.properties", | ||
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; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Convert logging configuration"; | ||
} | ||
|
||
@Override | ||
public 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."; | ||
} | ||
|
||
private static final ConfigurationConverter converter = ConfigurationConverter.getInstance(); | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(new FindSourceFiles(filePattern), new TreeVisitor<Tree, ExecutionContext>() { | ||
@Override | ||
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
if (tree instanceof ParseError || tree instanceof Quark || tree instanceof Remote) { | ||
return tree; | ||
} | ||
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); | ||
|
||
return PlainTextParser.convert(sourceFile) | ||
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. It looks like the output files are actually XML; should we use the XML parser here to read the converted contents such that subsequent recipes can optionally target their contents as part of the same recipe run? 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 is exactly the main question I had for you. The In the "Log4j 1 to Log4j Core 2" recipe I only use the XML output, but the |
||
.withText(utf8) | ||
.withCharset(StandardCharsets.UTF_8); | ||
} | ||
return super.visit(tree, ctx); | ||
} | ||
}); | ||
} | ||
} |
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.
Similarly best removed before a merge.