Skip to content

Add a recipe to migrate from JBoss Logging to SLF4J #241

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ dependencies {
}
testRuntimeOnly("log4j:log4j:1.+") // Necessary to match for now; explore alternatives for Refaster classpath in the future
testRuntimeOnly("ch.qos.logback:logback-classic:1.3.+")
testImplementation("org.jboss.logging:jboss-logging:latest.release")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.+")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.+")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.13.3")

testImplementation("org.openrewrite:rewrite-kotlin")
testImplementation("org.openrewrite:rewrite-maven")
testImplementation("org.openrewrite:rewrite-gradle")
testImplementation("org.openrewrite.gradle.tooling:model:${rewriteVersion}")
testImplementation("org.openrewrite:rewrite-test")

testImplementation("org.assertj:assertj-core:latest.release")
testRuntimeOnly(gradleApi())
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

String originalFormatString = Objects.requireNonNull((String) ((J.Literal) messageArgument).getValue());
List<Integer> originalIndices = originalLoggedArgumentIndices(originalFormatString);
List<Expression> originalParameters = originalParameters(originalArguments.get(2));
List<Expression> originalParameters = originalParameters(originalArguments.get(2), originalIndices.size());

List<Expression> targetArguments = new ArrayList<>(2);
targetArguments.add(buildStringLiteral(originalFormatString.replaceAll("\\{\\d*}", "{}")));
Expand All @@ -133,13 +133,29 @@ private List<Integer> originalLoggedArgumentIndices(String strFormat) {
return loggedArgumentIndices;
}

private static List<Expression> originalParameters(Expression logParameters) {
private static List<Expression> originalParameters(Expression logParameters, int indiceCount) {
if (logParameters instanceof J.NewArray) {
final List<Expression> initializer = ((J.NewArray) logParameters).getInitializer();
if (initializer == null || initializer.isEmpty()) {
return Collections.emptyList();
}
return initializer;
} else if (logParameters instanceof J.Identifier && logParameters.getType() instanceof JavaType.Array) {
List<Expression> arrayAccessParams = new ArrayList<>(indiceCount);
for (int i = 0; i < indiceCount; i++) {
arrayAccessParams.add(
new J.ArrayAccess(randomId(), Space.EMPTY, Markers.EMPTY, logParameters.withPrefix(Space.EMPTY),
new J.ArrayDimension(randomId(), Space.EMPTY, Markers.EMPTY,
new JRightPadded<>(
new J.Literal(
randomId(), Space.EMPTY, Markers.EMPTY, i, String.valueOf(i), null, JavaType.Primitive.Int
), Space.EMPTY, Markers.EMPTY
)
), logParameters.getType()
)
);
}
return arrayAccessParams;
}
return Collections.singletonList(logParameters);
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/resources/META-INF/rewrite/slf4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,36 @@ recipeList:
- org.openrewrite.java.logging.slf4j.JulLevelAllToTraceRecipe
- org.openrewrite.java.logging.log4j.JulToLog4j
- org.openrewrite.java.logging.slf4j.Log4j2ToSlf4j1

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.logging.slf4j.JBossLoggingToSlf4j
displayName: Migrate JBoss Logging to SLF4J
description: Migrates usage of the JBoss Logging facade to using SLF4J directly.
tags:
- logging
- jboss
- slf4j
recipeList:
- org.openrewrite.java.logging.slf4j.AddJBossLogManagerSlf4jProvider
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: org.jboss.logging
artifactId: jboss-logging

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.logging.slf4j.AddJBossLogManagerSlf4jProvider
displayName: Add JBoss LogManager's SLF4J provider
description: When JBoss LogManager is the logging backend, add its SLF4J provider so we can migrate to SLF4J as a logging facade.
tags:
- logging
- jboss
- slf4j
preconditions:
- org.openrewrite.java.dependencies.DependencyInsight:
groupIdPattern: org.jboss.logmanager
artifactIdPattern: jboss-logmanager
recipeList:
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.jboss.slf4j
artifactId: slf4j-jboss-logmanager
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2025 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.slf4j;

import org.junit.jupiter.api.Test;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;

import org.openrewrite.config.Environment;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

public class JBossLoggingToSlf4jTest implements RewriteTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class JBossLoggingToSlf4jTest implements RewriteTest {
class JBossLoggingToSlf4jTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(Environment.builder()
.scanRuntimeClasspath("org.openrewrite.java.migrate.javax")
.build()
.activateRecipes("org.openrewrite.java.logging.slf4j.JBossLoggingToSlf4j")
);
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Test
@DocumentExample
@Test
void gradle() {
rewriteRun(recipeSpec -> recipeSpec.beforeRecipe(withToolingApi()),
//language=gradle
buildGradle(
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.logging:jboss-logging:latest.release")
}
""",
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.slf4j:slf4j-jboss-logmanager:latest.release")
}
"""
)
);
}
@Test

void mavenPom() {
rewriteRun(
mavenProject(
"project",
//language=xml
pomXml(
"""
<project>
<groupId>org.example</groupId>
<artifactId>example-lib</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>latest</version>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<version>3.1.2.Final</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<groupId>org.example</groupId>
<artifactId>example-lib</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<version>3.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.slf4j</groupId>
<artifactId>slf4j-jboss-logmanager</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}

@Test
void gradle() {
rewriteRun(recipeSpec -> recipeSpec.beforeRecipe(withToolingApi()),
//language=gradle
buildGradle(
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.logging:jboss-logging:latest.release")
}
""",
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.slf4j:slf4j-jboss-logmanager:latest.release")
}
"""
)
);
}
Comment on lines +86 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
@Test
void gradle() {
rewriteRun(recipeSpec -> recipeSpec.beforeRecipe(withToolingApi()),
//language=gradle
buildGradle(
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.logging:jboss-logging:latest.release")
}
""",
"""
plugins { id "java" }
repositories { mavenCentral() }
dependencies {
implementation("org.jboss.logmanager:jboss-logmanager:latest.release")
implementation("org.jboss.slf4j:slf4j-jboss-logmanager:latest.release")
}
"""
)
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ void method(Logger logger, String param1, String param2) {
);
}

@Test
void arrayIdentifierArgument() {
rewriteRun(
// language=java
java(
"""
import java.util.logging.Level;
import java.util.logging.Logger;

class Test {
void method(Logger logger, String[] params) {
logger.log(Level.INFO, "INFO Log entry, param2: {1}, param1: {0}, etc", params);
logger.log(Level.INFO, "INFO Log entry, param1: {0}, param2: {1}, etc", params);
}
}
""",
"""
import org.slf4j.Logger;

class Test {
void method(Logger logger, String[] params) {
logger.info("INFO Log entry, param2: {}, param1: {}, etc", params[1], params[0]);
logger.info("INFO Log entry, param1: {}, param2: {}, etc", params[0], params[1]);
}
}
"""
)
);
}

@Test
void repeatLoggedArgumentAsNeeded() {
rewriteRun(
Expand Down
Loading