diff --git a/build.gradle.kts b/build.gradle.kts index 26611dca..1065fd01 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") 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}") testImplementation("org.openrewrite:rewrite-test") testImplementation("org.openrewrite:rewrite-java-tck") diff --git a/src/main/java/org/openrewrite/java/logging/AddLogger.java b/src/main/java/org/openrewrite/java/logging/AddLogger.java index 9eb05b49..89fa831f 100644 --- a/src/main/java/org/openrewrite/java/logging/AddLogger.java +++ b/src/main/java/org/openrewrite/java/logging/AddLogger.java @@ -104,7 +104,7 @@ public static AddLogger addLog4j2Logger(J.ClassDeclaration scope, String loggerN .builder(getModifiers(scope) + " Logger #{} = LogManager.getLogger(#{}.class);") .contextSensitive() .imports("org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2.23")) + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2")) .build() ); } diff --git a/src/main/java/org/openrewrite/java/logging/ConvertConfiguration.java b/src/main/java/org/openrewrite/java/logging/ConvertConfiguration.java new file mode 100644 index 00000000..11a845d3 --- /dev/null +++ b/src/main/java/org/openrewrite/java/logging/ConvertConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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. + */ +@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 getVisitor() { + return Preconditions.check(new FindSourceFiles(filePattern), new TreeVisitor() { + @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) + .withText(utf8) + .withCharset(StandardCharsets.UTF_8); + } + return super.visit(tree, ctx); + } + }); + } +} diff --git a/src/main/java/org/openrewrite/java/logging/LoggingFramework.java b/src/main/java/org/openrewrite/java/logging/LoggingFramework.java index 9591f31a..d85cb769 100644 --- a/src/main/java/org/openrewrite/java/logging/LoggingFramework.java +++ b/src/main/java/org/openrewrite/java/logging/LoggingFramework.java @@ -64,7 +64,7 @@ public JavaTemplate getErrorTemplate(String message, ExecutionContext ctx) { case Log4J2: return JavaTemplate .builder("#{any(org.apache.logging.log4j.Logger)}.error(" + message + ", #{any(java.lang.Throwable)})") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2.23")) + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2")) .build(); case COMMONS: return JavaTemplate diff --git a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java index a6af546e..72289b78 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java @@ -205,7 +205,7 @@ public JavaTemplate getErrorTemplateNoException(ExecutionContext ctx) { case Log4J2: return JavaTemplate .builder("#{any(org.apache.logging.log4j.Logger)}.error(#{any(String)});") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2.23")) + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "log4j-api-2")) .build(); case JUL: default: diff --git a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java index edbb73b6..7e6beedf 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java @@ -147,7 +147,7 @@ private JavaTemplate getInfoTemplate(ExecutionContext ctx) { return JavaTemplate .builder("#{any(org.apache.logging.log4j.Logger)}." + levelOrDefault + "(#{any(String)})") .javaParser(JavaParser.fromJavaVersion() - .classpathFromResources(ctx, "log4j-api-2.23")) + .classpathFromResources(ctx, "log4j-api-2")) .build(); case JUL: default: diff --git a/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.23.1.jar b/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.23.1.jar deleted file mode 100644 index 0e8e3f5e..00000000 Binary files a/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.23.1.jar and /dev/null differ diff --git a/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.24.3.jar b/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.24.3.jar new file mode 100644 index 00000000..4af1681f Binary files /dev/null and b/src/main/resources/META-INF/rewrite/classpath/log4j-api-2.24.3.jar differ diff --git a/src/main/resources/META-INF/rewrite/classpath/log4j-core-2.23.1.jar b/src/main/resources/META-INF/rewrite/classpath/log4j-core-2.24.3.jar similarity index 74% rename from src/main/resources/META-INF/rewrite/classpath/log4j-core-2.23.1.jar rename to src/main/resources/META-INF/rewrite/classpath/log4j-core-2.24.3.jar index 4a5d553d..0007e4e6 100644 Binary files a/src/main/resources/META-INF/rewrite/classpath/log4j-core-2.23.1.jar and b/src/main/resources/META-INF/rewrite/classpath/log4j-core-2.24.3.jar differ diff --git a/src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.14.jar b/src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.15.jar similarity index 83% rename from src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.14.jar rename to src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.15.jar index 7e769a23..9ec674a2 100644 Binary files a/src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.14.jar and b/src/main/resources/META-INF/rewrite/classpath/logback-classic-1.3.15.jar differ diff --git a/src/main/resources/META-INF/rewrite/classpath/lombok-1.18.30.jar b/src/main/resources/META-INF/rewrite/classpath/lombok-1.18.36.jar similarity index 61% rename from src/main/resources/META-INF/rewrite/classpath/lombok-1.18.30.jar rename to src/main/resources/META-INF/rewrite/classpath/lombok-1.18.36.jar index 05e3e5ba..4341a407 100644 Binary files a/src/main/resources/META-INF/rewrite/classpath/lombok-1.18.30.jar and b/src/main/resources/META-INF/rewrite/classpath/lombok-1.18.36.jar differ diff --git a/src/main/resources/META-INF/rewrite/log4j.yml b/src/main/resources/META-INF/rewrite/log4j.yml index 23f458cc..5ad89d95 100644 --- a/src/main/resources/META-INF/rewrite/log4j.yml +++ b/src/main/resources/META-INF/rewrite/log4j.yml @@ -17,8 +17,8 @@ --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.logging.log4j.ParameterizedLogging -displayName: Parameterize Log4j 2.x logging statements -description: Use Log4j 2.x parameterized logging, which can significantly boost performance for messages that +displayName: Parameterize Log4j API 2 logging statements +description: Use Log4j API 2 parameterized logging, which can significantly boost performance for messages that otherwise would be assembled with String concatenation. Particularly impactful when the log level is not enabled, as no work is done to assemble the message. tags: @@ -57,8 +57,22 @@ recipeList: --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.logging.log4j.Log4j1ToLog4j2 -displayName: Migrate Log4j 1.x to Log4j 2.x -description: Migrates Log4j 1.x to Log4j 2.x. +displayName: Migrate Log4j 1 to Log4j API/Log4j Core 2 +description: Transforms code written using Log4j 1 to use Log4j API 2 + and switches the logging backend from Log4j 1 to Log4j Core 2. +tags: + - logging + - log4j +recipeList: + - org.openrewrite.java.logging.log4j.Log4j1ToLog4jApi + - org.openrewrite.java.logging.log4j.Log4j1ToLog4jCore2 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.logging.log4j.Log4j1ToLog4jApi +displayName: Migrate Log4j 1 to Log4j API 2 +description: "Transforms code written using Log4j 1 to use Log4j API 2. + Should be used together with a recipe that switches the logging implementation to a more modern one: + e.g. JBoss LogManager, Log4j Core 2 or Logback." tags: - logging - log4j @@ -71,7 +85,6 @@ recipeList: - org.openrewrite.java.ChangeMethodTargetToStatic: methodPattern: org.apache.log4j.Logger getRootLogger() fullyQualifiedTargetTypeName: org.apache.logging.log4j.LogManager - - org.openrewrite.java.logging.log4j.LoggerSetLevelToConfiguratorRecipe - org.openrewrite.java.ChangeMethodName: methodPattern: org.apache.log4j.Priority isGreaterOrEqual(org.apache.log4j.Priority) @@ -80,7 +93,6 @@ recipeList: - org.openrewrite.java.ChangeType: oldFullyQualifiedTypeName: org.apache.log4j.Priority newFullyQualifiedTypeName: org.apache.logging.log4j.Level - - org.openrewrite.java.ChangeMethodTargetToStatic: methodPattern: org.apache.log4j.Category getInstance(Class) fullyQualifiedTargetTypeName: org.apache.logging.log4j.LogManager @@ -93,7 +105,6 @@ recipeList: - org.openrewrite.java.ChangeType: oldFullyQualifiedTypeName: org.apache.log4j.Category newFullyQualifiedTypeName: org.apache.logging.log4j.Logger - - org.openrewrite.java.ChangePackage: oldPackageName: org.apache.log4j newPackageName: org.apache.logging.log4j @@ -103,27 +114,41 @@ recipeList: artifactId: log4j-api version: 2.x onlyIfUsing: org.apache.log4j.* +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.logging.log4j.Log4j1ToLog4jCore2 +displayName: Migrate Log4j 1 to Log4j Core 2 +description: Replaces Log4j 1 as logging implementation with Log4j Core 2. + This recipe does not replace code occurrences of Log4j 1 and should be only used if Log4j 1 is not used + directly as logging API. +recipeList: + # Guarantees alignment between Log4j API and Log4j Core version, regardless of the Maven conflict resolution + # algorithm used. + - org.openrewrite.maven.AddManagedDependency: + groupId: org.apache.logging.log4j + artifactId: log4j-bom + version: 2.x + type: pom + scope: import - org.openrewrite.java.dependencies.AddDependency: groupId: org.apache.logging.log4j artifactId: log4j-core version: 2.x - onlyIfUsing: org.apache.log4j.* + scope: runtime + # Removes Log4j 1 and replacements - org.openrewrite.java.dependencies.RemoveDependency: groupId: log4j artifactId: log4j + - org.openrewrite.java.dependencies.RemoveDependency: + groupId: org.apache.logging.log4j + artifactId: log4j-1.2-api + - org.openrewrite.java.dependencies.RemoveDependency: + groupId: org.slf4j + artifactId: log4j-over-slf4j - org.openrewrite.java.dependencies.RemoveDependency: groupId: ch.qos.reload4j artifactId: reload4j - - org.openrewrite.java.dependencies.AddDependency: - groupId: org.apache.logging.log4j - artifactId: log4j-api - version: 2.x - onlyIfUsing: org.apache.logging.log4j.* - - org.openrewrite.java.dependencies.AddDependency: - groupId: org.apache.logging.log4j - artifactId: log4j-core - version: 2.x - onlyIfUsing: org.apache.logging.log4j.* + # Switch the target of SLF4J-to-X bridges - org.openrewrite.java.dependencies.ChangeDependency: oldGroupId: org.slf4j oldArtifactId: slf4j-log4j12 @@ -136,8 +161,47 @@ recipeList: newGroupId: org.apache.logging.log4j newArtifactId: log4j-slf4j-impl newVersion: 2.x + # + - org.openrewrite.java.dependencies.UpgradeDependencyVersion: + groupId: commons-logging + artifactId: commons-logging + newVersion: latest.release - org.openrewrite.java.logging.log4j.UpgradeLog4J2DependencyVersion - + - org.openrewrite.java.logging.log4j.Log4j1ConfigurationToLog4jCore2 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.logging.log4j.Log4j1ConfigurationToLog4jCore2 +displayName: Migrate Log4j 1 configuration to Log4j Core 2 format +description: Migrates Log4j 1 configuration files to the Log4j Core 2 XML format. +recipeList: + - org.openrewrite.java.logging.log4j.V1PropertiesToV2Xml + - org.openrewrite.java.logging.log4j.V1XmlToV2Xml +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.logging.log4j.V1PropertiesToV2Xml +displayName: Migrate `log4j.properties` files to `log4j2.xml` format +description: Migrates `log4j.properties` files to the `log4j2.xml` format. +recipeList: + - org.openrewrite.java.logging.ConvertConfiguration: + filePattern: "**/log4j.properties" + inputFormat: "v1:properties" + outputFormat: "v2:xml" + - org.openrewrite.RenameFile: + fileMatcher: "**/log4j.properties" + fileName: "log4j2.xml" +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.logging.log4j.V1XmlToV2Xml +displayName: Migrate `log4j.xml` files to `log4j2.xml` format +description: Migrates `log4j.xml` files to the `log4j2.xml` format. +recipeList: + - org.openrewrite.java.logging.ConvertConfiguration: + filePattern: "**/log4j.xml" + inputFormat: "v1:xml" + outputFormat: "v2:xml" + - org.openrewrite.RenameFile: + fileMatcher: "**/log4j.xml" + fileName: "log4j2.xml" --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.logging.log4j.UpgradeLog4J2DependencyVersion diff --git a/src/main/resources/META-INF/rewrite/logback.yml b/src/main/resources/META-INF/rewrite/logback.yml index 36c25872..f6613232 100644 --- a/src/main/resources/META-INF/rewrite/logback.yml +++ b/src/main/resources/META-INF/rewrite/logback.yml @@ -17,8 +17,10 @@ --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.logging.logback.Log4jToLogback -displayName: Migrate Log4j 2.x to Logback -description: Migrates usage of Apache Log4j 2.x to using `logback` as an SLF4J implementation directly. Note, this currently does not modify `log4j.properties` files. +displayName: Migrate Log4j API/Log4j Core 2 to SLF4J/Logback +description: | + Migrates usage of Apache Log4j API with Log4j Core 2 implementation to SLF4J with Logback implementation. + Note, this currently does not modify the Log4j Core 2 configuration files. tags: - logging - log4j diff --git a/src/test/java/org/openrewrite/java/logging/ConvertConfigurationTest.java b/src/test/java/org/openrewrite/java/logging/ConvertConfigurationTest.java new file mode 100644 index 00000000..fd4210a9 --- /dev/null +++ b/src/test/java/org/openrewrite/java/logging/ConvertConfigurationTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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 org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.test.SourceSpecs.text; + +class ConvertConfigurationTest implements RewriteTest { + + @DocumentExample + @Test + void convertsLog4j1ToLog4j2Configuration() { + rewriteRun( + spec -> spec.recipe(new ConvertConfiguration("file.txt", "v1:properties", "v2:xml")), + text( + //language=properties + """ + # 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 + """, + //language=xml + """ + + + + + + + + + + + + + + + + + + + + + + + + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/logging/ParameterizedLoggingTest.java b/src/test/java/org/openrewrite/java/logging/ParameterizedLoggingTest.java index ba9299df..3588b15d 100644 --- a/src/test/java/org/openrewrite/java/logging/ParameterizedLoggingTest.java +++ b/src/test/java/org/openrewrite/java/logging/ParameterizedLoggingTest.java @@ -40,7 +40,7 @@ class ParameterizedLoggingTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1", "log4j-api-2.23", "log4j-core-2.23", "lombok")); + .classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1", "log4j-api-2", "log4j-core-2", "lombok")); } @DocumentExample diff --git a/src/test/java/org/openrewrite/java/logging/PrintStackTraceToLogErrorTest.java b/src/test/java/org/openrewrite/java/logging/PrintStackTraceToLogErrorTest.java index 0762159d..d6193dcf 100644 --- a/src/test/java/org/openrewrite/java/logging/PrintStackTraceToLogErrorTest.java +++ b/src/test/java/org/openrewrite/java/logging/PrintStackTraceToLogErrorTest.java @@ -41,7 +41,7 @@ void useSlf4j() { import org.slf4j.Logger; class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -56,7 +56,7 @@ void test() { import org.slf4j.Logger; class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -75,14 +75,14 @@ void test() { void useLog4j2() { rewriteRun( spec -> spec.recipe(new PrintStackTraceToLogError(null, "LOGGER", "Log4j2")) - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23")), + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2")), //language=java java( """ import org.apache.logging.log4j.Logger; class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -95,7 +95,7 @@ void test() { import org.apache.logging.log4j.Logger; class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -118,7 +118,7 @@ void useJul() { import java.util.logging.Logger; class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -130,10 +130,10 @@ void test() { """ import java.util.logging.Level; import java.util.logging.Logger; - + class Test { Logger logger; - + void test() { try { } catch(Throwable t) { @@ -165,10 +165,10 @@ void test() { """ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - + class Test { private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); - + void test() { try { } catch(Throwable t) { @@ -195,7 +195,7 @@ public void error(Exception e) { e.printStackTrace(); } } - + public static class Another { public void close() { try { @@ -209,19 +209,19 @@ public void close() { """ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - + public class Test { public static class MyErrorReceiver { private static final Logger LOGGER = LoggerFactory.getLogger(MyErrorReceiver.class); - + public void error(Exception e) { LOGGER.error("Exception", e); } } - + public static class Another { private static final Logger LOGGER = LoggerFactory.getLogger(Another.class); - + public void close() { try { } catch ( java.io.IOException e ) { diff --git a/src/test/java/org/openrewrite/java/logging/log4j/CommonsLoggingToLog4jTest.java b/src/test/java/org/openrewrite/java/logging/log4j/CommonsLoggingToLog4jTest.java index f148d8ae..bb48b349 100644 --- a/src/test/java/org/openrewrite/java/logging/log4j/CommonsLoggingToLog4jTest.java +++ b/src/test/java/org/openrewrite/java/logging/log4j/CommonsLoggingToLog4jTest.java @@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) { spec.recipeFromResource("/META-INF/rewrite/log4j.yml", "org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j") .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23", "commons-logging-1.3", "lombok-1.18")); + .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2", "commons-logging-1.3", "lombok-1.18")); } @DocumentExample @@ -43,7 +43,7 @@ void loggerFactoryToLogManager() { """ import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; - + class Test { Log log1 = LogFactory.getLog(Test.class); Log log2 = LogFactory.getLog("Test"); @@ -54,7 +54,7 @@ class Test { """ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; - + class Test { Logger log1 = LogManager.getLogger(Test.class); Logger log2 = LogManager.getLogger("Test"); @@ -77,7 +77,7 @@ void changeLombokLogAnnotation() { java( """ import lombok.extern.apachecommons.CommonsLog; - + @CommonsLog class Test { void method() { @@ -87,7 +87,7 @@ void method() { """, """ import lombok.extern.log4j.Log4j2; - + @Log4j2 class Test { void method() { diff --git a/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulEnteringTest.java b/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulEnteringTest.java index 1d22a3c0..d9e8843c 100644 --- a/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulEnteringTest.java +++ b/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulEnteringTest.java @@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) { spec.recipes(new ConvertJulEntering(), new ChangeType("java.util.logging.Logger", "org.apache.logging.log4j.Logger", true)) .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23")); + .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2")); } @Test diff --git a/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulExitingTest.java b/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulExitingTest.java index 88a4b51a..e38f0e02 100644 --- a/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulExitingTest.java +++ b/src/test/java/org/openrewrite/java/logging/log4j/ConvertJulExitingTest.java @@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) { spec.recipes(new ConvertJulExiting(), new ChangeType("java.util.logging.Logger", "org.apache.logging.log4j.Logger", true)) .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23")); + .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2")); } @Test diff --git a/src/test/java/org/openrewrite/java/logging/log4j/JulToLog4jTest.java b/src/test/java/org/openrewrite/java/logging/log4j/JulToLog4jTest.java index fd950e9e..8ffc7f57 100644 --- a/src/test/java/org/openrewrite/java/logging/log4j/JulToLog4jTest.java +++ b/src/test/java/org/openrewrite/java/logging/log4j/JulToLog4jTest.java @@ -30,7 +30,7 @@ class JulToLog4jTest implements RewriteTest { public void defaults(RecipeSpec spec) { spec.recipeFromResource("/META-INF/rewrite/log4j.yml", "org.openrewrite.java.logging.log4j.JulToLog4j") .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23", "lombok-1.18")); + .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2", "lombok-1.18")); } @Test diff --git a/src/test/java/org/openrewrite/java/logging/log4j/Log4j1ToLog4j2Test.java b/src/test/java/org/openrewrite/java/logging/log4j/Log4j1ToLog4j2Test.java index 3ee8ccbe..dae15f80 100644 --- a/src/test/java/org/openrewrite/java/logging/log4j/Log4j1ToLog4j2Test.java +++ b/src/test/java/org/openrewrite/java/logging/log4j/Log4j1ToLog4j2Test.java @@ -25,9 +25,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.openrewrite.java.Assertions.*; import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.properties.Assertions.properties; class Log4j1ToLog4j2Test implements RewriteTest { @@ -306,6 +308,17 @@ class Test { com.mycompany.app my-app 1 + + + + org.apache.logging.log4j + log4j-bom + 2.24.3 + pom + import + + + org.apache.httpcomponents @@ -319,13 +332,12 @@ class Test { org.apache.logging.log4j - log4j-core - %1$s + log4j-slf4j-impl org.apache.logging.log4j - log4j-slf4j-impl - %1$s + log4j-core + runtime @@ -336,4 +348,41 @@ class Test { ) ); } + + @Test + void rewriteConfigurationFile() { + rewriteRun( + mavenProject("project", + srcMainResources( + properties( + //language=properties + """ + log4j.appender.FILE = org.apache.log4j.FileAppender + log4j.appender.FILE.file = file.log + log4j.appender.FILE.layout = org.apache.log4j.SimpleLayout + log4j.rootLogger = INFO, FILE + """, + //language=xml + """ + + + + + + + + + + + + + + + """, + spec -> spec.path("log4j.properties") + .afterRecipe(s -> assertThat(s.getSourcePath()).hasFileName("log4j2.xml"))) + ) + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/logging/slf4j/Log4j2ToSlf4j1Test.java b/src/test/java/org/openrewrite/java/logging/slf4j/Log4j2ToSlf4j1Test.java index d1bb8e72..816cf3c0 100644 --- a/src/test/java/org/openrewrite/java/logging/slf4j/Log4j2ToSlf4j1Test.java +++ b/src/test/java/org/openrewrite/java/logging/slf4j/Log4j2ToSlf4j1Test.java @@ -34,7 +34,7 @@ public void defaults(RecipeSpec spec) { .build() .activateRecipes("org.openrewrite.java.logging.slf4j.Log4j2ToSlf4j1")) .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2.23", "log4j-core-2.23", "lombok-1.18")); + .classpathFromResources(new InMemoryExecutionContext(), "log4j-api-2", "log4j-core-2", "lombok-1.18")); } @DocumentExample