Skip to content
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 JCL to Log4j API migration #129

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
"""
)
);
}
}