Skip to content

Commit

Permalink
Add JCL to Log4j API migration (#129)
Browse files Browse the repository at this point in the history
* Add JCL to Log4j API migration

This PR adds a recipe to migrate from Apache Commons Logging to Log4j
2.x API.

* Minor formatting changes to CommonsLoggingToLog4jTest

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
ppkarwasz and timtebeek authored Dec 11, 2023
1 parent 19563ea commit 16dd092
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/resources/META-INF/rewrite/log4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,29 @@ recipeList:
newGroupId: org.apache.logging.log4j
newArtifactId: log4j-slf4j-impl
newVersion: latest.release
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j
displayName: Migrate JCL to Log4j 2.x API.
description: Transforms code written using Apache Commons Logging to use Log4j 2.x API.
tags:
- logging
- commons-logging
- log4j
recipeList:
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain:
- org.apache.commons.logging.LogFactory getFactory()
- org.apache.commons.logging.LogFactory getInstance(..)
newMethodName: getLogger
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.apache.commons.logging.LogFactory getLog(..)
newMethodName: getLogger
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.commons.logging.LogFactory
newFullyQualifiedTypeName: org.apache.logging.log4j.LogManager
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.commons.logging.Log
newFullyQualifiedTypeName: org.apache.logging.log4j.Logger
- org.openrewrite.java.logging.ChangeLombokLogAnnotation:
loggingFramework: Log4j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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.log4j;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;

class CommonsLoggingToLog4jTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResource("/META-INF/rewrite/log4j.yml",
"org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j")
.parser(JavaParser.fromJavaVersion().classpath("log4j-api", "commons-logging", "lombok"));
}

@DocumentExample
@Test
void loggerFactoryToLogManager() {
// language=java
rewriteRun(
java(
"""
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");
Log log3 = LogFactory.getFactory().getInstance(Test.class);
Log log4 = LogFactory.getFactory().getInstance("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");
Logger log3 = LogManager.getLogger(Test.class);
Logger log4 = LogManager.getLogger("Test");
}
"""
)
);
}

@Test
void changeLombokLogAnnotation() {
// language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder()
.identifiers(false)
.methodInvocations(false)
.build()),
java(
"""
import lombok.extern.apachecommons.CommonsLog;
@CommonsLog
class Test {
void method() {
log.info("uh oh");
}
}
""",
"""
import lombok.extern.log4j.Log4j2;
@Log4j2
class Test {
void method() {
log.info("uh oh");
}
}
"""
)
);
}
}

0 comments on commit 16dd092

Please sign in to comment.