diff --git a/src/main/java/org/openrewrite/java/logging/slf4j/JulGetLoggerToLoggerFactory.java b/src/main/java/org/openrewrite/java/logging/slf4j/JulGetLoggerToLoggerFactory.java index c5602063..81f365fe 100644 --- a/src/main/java/org/openrewrite/java/logging/slf4j/JulGetLoggerToLoggerFactory.java +++ b/src/main/java/org/openrewrite/java/logging/slf4j/JulGetLoggerToLoggerFactory.java @@ -15,47 +15,73 @@ */ package org.openrewrite.java.logging.slf4j; -import com.google.errorprone.refaster.annotation.AfterTemplate; -import com.google.errorprone.refaster.annotation.BeforeTemplate; -import org.openrewrite.java.template.RecipeDescriptor; -import org.slf4j.LoggerFactory; - -import java.util.logging.Logger; - -@RecipeDescriptor( - name = "Replace JUL Logger creation with SLF4J LoggerFactory", - description = "Replace calls to `Logger.getLogger` with `LoggerFactory.getLogger`." -) -public class JulGetLoggerToLoggerFactory { - @RecipeDescriptor( - name = "Replace JUL `Logger.getLogger(Some.class.getName())` with SLF4J's `LoggerFactory.getLogger(Some.class)`", - description = "Replace calls to `java.util.logging.Logger.getLogger(Some.class.getName())` with `org.slf4j.LoggerFactory.getLogger(Some.class)`." - ) - public static class GetLoggerClassNameToLoggerFactory { - @BeforeTemplate - Logger before(Class clazz) { - return Logger.getLogger(clazz.getName()); - } - - @AfterTemplate - org.slf4j.Logger after(Class clazz) { - return LoggerFactory.getLogger(clazz); - } +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; + +@Value +@EqualsAndHashCode(callSuper = false) +public class JulGetLoggerToLoggerFactory extends Recipe { + + private static final MethodMatcher GET_LOGGER = new MethodMatcher("java.util.logging.Logger getLogger(java.lang.String)"); + private static final MethodMatcher CLASS_GET_NAME = new MethodMatcher("java.lang.Class getName()"); + private static final MethodMatcher CLASS_GET_CANONICAL_NAME = new MethodMatcher("java.lang.Class getCanonicalName()"); + + @Override + public String getDisplayName() { + return "Replace JUL Logger creation with SLF4J LoggerFactory"; + } + + @Override + public String getDescription() { + return "Replace calls to `Logger.getLogger(Some.class.getName())` and " + + "`Logger.getLogger(Some.class.getCanonicalName())` with `LoggerFactory.getLogger(Some.class)`."; } - @RecipeDescriptor( - name = "Replace JUL `Logger.getLogger(Some.class.getCanonicalName())` with SLF4J's `LoggerFactory.getLogger(Some.class)`", - description = "Replace calls to `java.util.logging.Logger.getLogger(Some.class.getCanonicalName())` with `org.slf4j.LoggerFactory.getLogger(Some.class)`." - ) - public static class GetLoggerClassCanonicalNameToLoggerFactory { - @BeforeTemplate - Logger before(Class clazz) { - return Logger.getLogger(clazz.getCanonicalName()); - } - - @AfterTemplate - org.slf4j.Logger after(Class clazz) { - return LoggerFactory.getLogger(clazz); - } + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesMethod<>(GET_LOGGER), new JavaIsoVisitor() { + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); + if (!GET_LOGGER.matches(mi)) { + return mi; + } + + Expression arg = mi.getArguments().get(0); + if (!(arg instanceof J.MethodInvocation)) { + return mi; + } + + J.MethodInvocation argMethod = (J.MethodInvocation) arg; + if (!CLASS_GET_NAME.matches(argMethod) && !CLASS_GET_CANONICAL_NAME.matches(argMethod)) { + return mi; + } + + Expression classExpr = argMethod.getSelect(); + if (classExpr == null) { + return mi; + } + + maybeAddImport("org.slf4j.LoggerFactory"); + maybeRemoveImport("java.util.logging.Logger"); + + return JavaTemplate.builder("LoggerFactory.getLogger(#{any(java.lang.Class)})") + .imports("org.slf4j.LoggerFactory") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "slf4j-api-2.+")) + .build() + .apply(getCursor(), mi.getCoordinates().replace(), classExpr); + } + }); } } diff --git a/src/main/resources/META-INF/rewrite/inline-log4j-api-methods.yml b/src/main/resources/META-INF/rewrite/inline-log4j-api-methods.yml index 4bef31e4..5ded9214 100644 --- a/src/main/resources/META-INF/rewrite/inline-log4j-api-methods.yml +++ b/src/main/resources/META-INF/rewrite/inline-log4j-api-methods.yml @@ -23,7 +23,8 @@ name: org.apache.logging.log4j.InlineLog4jApiMethods displayName: Inline `log4j-api-2` methods annotated with `@InlineMe` description: >- Automatically generated recipes to inline method calls based on `@InlineMe` annotations - discovered in the type table.preconditions: + discovered in the type table. +preconditions: - org.openrewrite.Singleton recipeList: # From org.apache.logging.log4j:log4j-api:2.25.2 diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv index e8f2dcde..17e78979 100644 --- a/src/main/resources/META-INF/rewrite/recipes.csv +++ b/src/main/resources/META-INF/rewrite/recipes.csv @@ -4,9 +4,9 @@ maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.log maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.ChangeLoggersToPrivate,Change logger fields to `private`,Ensures that logger fields are declared as `private` to encapsulate logging mechanics within the class.,1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.ChangeLombokLogAnnotation,Replace any Lombok log annotations with target logging framework annotation,"Replace Lombok annotations such as `@CommonsLog` and `@Log4j` with the target logging framework annotation, or `@Sl4fj` if not provided.",17,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS""]}]" maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.ParameterizedLogging,Parameterize logging statements,"Transform logging statements using concatenation for messages and variables into a parameterized format. For example, `logger.info(""hi "" + userName)` becomes `logger.info(""hi {}"", userName)`. This 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.",1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""methodPattern"",""type"":""String"",""displayName"":""Method pattern"",""description"":""A method used to find matching statements to parameterize."",""example"":""org.slf4j.Logger info(..)"",""required"":true},{""name"":""removeToString"",""type"":""Boolean"",""displayName"":""Remove `Object#toString()` invocations from logging parameters"",""description"":""Optionally remove `toString(`) method invocations from Object parameters.""}]" -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.PrintStackTraceToLogError,Use logger instead of `printStackTrace()`,"When a logger is present, log exceptions rather than calling `printStackTrace()`.",1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS""]}]" -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.SystemErrToLogging,Use logger instead of `System.err` print statements,Replace `System.err` print statements with a logger.,1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS""]}]" -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.SystemOutToLogging,Use logger instead of `System.out` print statements,Replace `System.out` print statements with a logger.,1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS""]},{""name"":""level"",""type"":""String"",""displayName"":""Level"",""description"":""The logging level to turn `System.out` print statements into."",""valid"":[""trace"",""debug"",""info""]}]" +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.PrintStackTraceToLogError,Use logger instead of `printStackTrace()`,"When a logger is present, log exceptions rather than calling `printStackTrace()`.",1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS"",""SYSTEM""]}]" +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.SystemErrToLogging,Use logger instead of `System.err` print statements,Replace `System.err` print statements with a logger.,1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS"",""SYSTEM""]}]" +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.SystemOutToLogging,Use logger instead of `System.out` print statements,Replace `System.out` print statements with a logger.,1,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS"",""SYSTEM""]},{""name"":""level"",""type"":""String"",""displayName"":""Level"",""description"":""The logging level to turn `System.out` print statements into."",""valid"":[""trace"",""debug"",""info""]}]" maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.SystemPrintToLogging,Use logger instead of system print statements,Replace `System.out` and `System.err` print statements with a logger.,7,,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""addLogger"",""type"":""Boolean"",""displayName"":""Add logger"",""description"":""Add a logger field to the class if it isn't already present.""},{""name"":""loggerName"",""type"":""String"",""displayName"":""Logger name"",""description"":""The name of the logger to use when generating a field."",""example"":""log""},{""name"":""loggingFramework"",""type"":""String"",""displayName"":""Logging framework"",""description"":""The logging framework to use."",""valid"":[""SLF4J"",""Log4J1"",""Log4J2"",""JUL"",""COMMONS""]},{""name"":""level"",""type"":""String"",""displayName"":""Level"",""description"":""The logging level to turn `System.out` print statements into."",""valid"":[""trace"",""debug"",""info""]}]" maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jboss.FormattedArgumentsToVMethodRecipes,Replace deprecated JBoss Logging Logger formatted message invocations with the v-version of methods,"Replace `logger.level(""hello {0}"", arg)` with `logger.levelv(""hello {0}"", arg)`.",25,Jboss,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jboss.FormattedArgumentsToVMethodRecipes$DebugToVDebugRecipe,Refaster template `FormattedArgumentsToVMethod.DebugToVDebug`,"Recipe created for the following Refaster template: @@ -203,7 +203,7 @@ public static class WarnToVWarnWithThrowable { .",1,Jboss,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jboss.LoggerLevelArgumentToMethod,Replace JBoss Logging Level arguments with the corresponding eponymous level method calls,"Replace calls to `Logger.log(Level, ...)` with the corresponding eponymous level method calls. For example `Logger.log(Level.INFO, ...)` to `Logger.info(...)`.",1,Jboss,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jboss.JBossLoggingBestPractices,JBoss Logging Best Practices,"This recipe applies best practices for logging in JBoss applications. -It includes converting argument arrays to varargs for better readability and performance.",31,Jboss,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +It includes converting argument arrays to varargs for better readability and performance.",33,Jboss,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jul.LoggerLevelArgumentToMethodRecipes,Replace JUL Level arguments with the corresponding method calls,"Replace calls to `Logger.log(Level, String)` with the corresponding method calls.",29,Jul,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jul.LoggerLevelArgumentToMethodRecipes$LogLevelConfigSupplierToMethodRecipe,"Replace JUL `Logger.log(Level.CONFIG, Supplier)` with `Logger.config(Supplier)`","Replace calls to `java.util.logging.Logger.log(Level.CONFIG, Supplier)` with `Logger.config(Supplier)`.",1,Jul,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.jul.LoggerLevelArgumentToMethodRecipes$LogLevelConfigToMethodRecipe,"Replace JUL `Logger.log(Level.CONFIG, String)` with `Logger.config(String)`","Replace calls to `java.util.logging.Logger.log(Level.CONFIG, String)` with `Logger.config(String)`.",1,Jul,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., @@ -224,23 +224,21 @@ maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.log maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.LoggerSetLevelToConfiguratorRecipe,Convert Log4j `Logger.setLevel` to Log4j2 `Configurator.setLevel`,Converts `org.apache.log4j.Logger.setLevel` to `org.apache.logging.log4j.core.config.Configurator.setLevel`.,1,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.LoggingExceptionConcatenationRecipe,Log exceptions as parameters rather than as string concatenations,By using the exception as another parameter you get the whole stack trace.,1,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.PrependRandomName,Prepend a random name to each Log4J statement,To make finding the callsite of a logging statement easier in code search.,1,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.ParameterizedLogging,Parameterize Log4j 2.x logging statements,"Use Log4j 2.x 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.",29,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.Log4j1ToLog4j2,Migrate Log4j 1.x to Log4j 2.x,Migrates Log4j 1.x to Log4j 2.x.,101,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.ParameterizedLogging,Parameterize Log4j 2.x logging statements,"Use Log4j 2.x 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.",31,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.Log4j1ToLog4j2,Migrate Log4j 1.x to Log4j 2.x,Migrates Log4j 1.x to Log4j 2.x.,111,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.UpgradeLog4J2DependencyVersion,Upgrade Log4j 2.x dependency version,"Upgrades the Log4j 2.x dependencies to the latest 2.x version. -Mitigates the [Log4Shell and other Log4j2-related vulnerabilities](https://www.cisa.gov/news-events/cybersecurity-advisories/aa21-356a).",3,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j,Migrate JCL to Log4j 2.x API,Transforms code written using Apache Commons Logging to use Log4j 2.x API.,27,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.JulToLog4j,Migrate JUL to Log4j 2.x API,Transforms code written using `java.util.logging` to use Log4j 2.x API.,69,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.Slf4jToLog4j,Migrate SLF4J to Log4j 2.x API,Transforms code written using SLF4J to use Log4j 2.x API.,57,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +Mitigates the [Log4Shell and other Log4j2-related vulnerabilities](https://www.cisa.gov/news-events/cybersecurity-advisories/aa21-356a).",5,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j,Migrate JCL to Log4j 2.x API,Transforms code written using Apache Commons Logging to use Log4j 2.x API.,29,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.JulToLog4j,Migrate JUL to Log4j 2.x API,Transforms code written using `java.util.logging` to use Log4j 2.x API.,71,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.log4j.Slf4jToLog4j,Migrate SLF4J to Log4j 2.x API,Transforms code written using SLF4J to use Log4j 2.x API.,59,Log4j,Logging,Java,,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.logback.ConfigureLoggerLevel,Configure logback logger level,Within logback.xml configuration files sets the specified log level for a particular class. Will not create a logback.xml if one does not already exist.,1,Logback,Logging,Java,Recipes related to [`logback`](http://logback.qos.ch/documentation.html).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""className"",""type"":""String"",""displayName"":""Class name"",""description"":""The fully qualified class name to configure the log level for"",""example"":""com.example.MyClass"",""required"":true},{""name"":""logLevel"",""type"":""LogLevel"",""displayName"":""Log level"",""description"":""The log level to set for the class"",""example"":""off"",""valid"":[""trace"",""debug"",""info"",""warn"",""error"",""off""],""required"":true},{""name"":""filePattern"",""type"":""String"",""displayName"":""File pattern"",""description"":""A glob expression that can be used to constrain which directories or source files should be searched. Multiple patterns may be specified, separated by a semicolon `;`. If multiple patterns are supplied any of the patterns matching will be interpreted as a match. When not set, '**/logback.xml' is used."",""example"":""**/logback-spring.xml""}]" maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.logback.Log4jAppenderToLogback,Migrate Log4j 2.x Appender to logback-classic equivalents,"Migrates custom Log4j 2.x Appender components to `logback-classic`. This recipe operates on the following assumptions: 1.) The contents of the `append()` method remains unchanged. 2.) The `requiresLayout()` method is not used in logback and can be removed. 3.) In logback, the `stop()` method is the equivalent of log4j's close() method. For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).",1,Logback,Logging,Java,Recipes related to [`logback`](http://logback.qos.ch/documentation.html).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.logback.Log4jLayoutToLogback,Migrate Log4j 2.x Layout to logback-classic equivalents,"Migrates custom Log4j 2.x Layout components to `logback-classic`. This recipe operates on the following assumptions: 1. A logback-classic layout must extend the `LayoutBase` class. 2. log4j's `format()` is renamed to `doLayout()` in a logback-classic layout. 3. LoggingEvent `getRenderedMessage()` is converted to LoggingEvent `getMessage()`. 4. The log4j ignoresThrowable() method is not needed and has no equivalent in logback-classic. 5. The activateOptions() method merits further discussion. In log4j, a layout will have its activateOptions() method invoked by log4j configurators, that is PropertyConfigurator or DOMConfigurator just after all the options of the layout have been set. Thus, the layout will have an opportunity to check that its options are coherent and if so, proceed to fully initialize itself. 6. In logback-classic, layouts must implement the LifeCycle interface which includes a method called start(). The start() method is the equivalent of log4j's activateOptions() method. For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).",1,Logback,Logging,Java,Recipes related to [`logback`](http://logback.qos.ch/documentation.html).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.logback.Log4jToLogback,Migrate Log4j 2.x to Logback,"Migrates usage of Apache Log4j 2.x to using `logback` as an SLF4J implementation directly. Note, this currently does not modify `log4j.properties` files.",189,Logback,Logging,Java,Recipes related to [`logback`](http://logback.qos.ch/documentation.html).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.logback.Log4jToLogback,Migrate Log4j 2.x to Logback,"Migrates usage of Apache Log4j 2.x to using `logback` as an SLF4J implementation directly. Note, this currently does not modify `log4j.properties` files.",211,Logback,Logging,Java,Recipes related to [`logback`](http://logback.qos.ch/documentation.html).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.ChangeLogLevel,Change SLF4J log level,Change the log level of SLF4J log statements.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code.,"[{""name"":""from"",""type"":""Level"",""displayName"":""From"",""description"":""The log level to change from."",""example"":""INFO"",""required"":true},{""name"":""to"",""type"":""Level"",""displayName"":""To"",""description"":""The log level to change to."",""example"":""DEBUG"",""required"":true},{""name"":""startsWith"",""type"":""String"",""displayName"":""Starts with"",""description"":""Only change log statements that start with this string. When omitted all log statements of the specified level are changed."",""example"":""LaunchDarkly""}]" maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.CompleteExceptionLogging,Enhances logging of exceptions by including the full stack trace in addition to the exception message,"It is a common mistake to call `Exception.getMessage()` when passing an exception into a log method. Not all exception types have useful messages, and even if the message is useful this omits the stack trace. Including a complete stack trace of the error along with the exception message in the log allows developers to better understand the context of the exception and identify the source of the error more quickly and accurately. If the method invocation includes any call to `Exception.getMessage()` or `Exception.getLocalizedMessage()` and not an exception is already passed as the last parameter to the log method, then we will append the exception as the last parameter in the log method.",1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactoryRecipes,Replace JUL Logger creation with SLF4J LoggerFactory,Replace calls to `Logger.getLogger` with `LoggerFactory.getLogger`.,5,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactoryRecipes$GetLoggerClassCanonicalNameToLoggerFactoryRecipe,Replace JUL `Logger.getLogger(Some.class.getCanonicalName())` with SLF4J's `LoggerFactory.getLogger(Some.class)`,Replace calls to `java.util.logging.Logger.getLogger(Some.class.getCanonicalName())` with `org.slf4j.LoggerFactory.getLogger(Some.class)`.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactoryRecipes$GetLoggerClassNameToLoggerFactoryRecipe,Replace JUL `Logger.getLogger(Some.class.getName())` with SLF4J's `LoggerFactory.getLogger(Some.class)`,Replace calls to `java.util.logging.Logger.getLogger(Some.class.getName())` with `org.slf4j.LoggerFactory.getLogger(Some.class)`.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactory,Replace JUL Logger creation with SLF4J LoggerFactory,Replace calls to `Logger.getLogger(Some.class.getName())` and `Logger.getLogger(Some.class.getCanonicalName())` with `LoggerFactory.getLogger(Some.class)`.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulIsLoggableToIsEnabledRecipes,Replace JUL active Level check with corresponding SLF4J method calls,Replace calls to `Logger.isLoggable(Level)` with the corresponding SLF4J method calls.,17,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulIsLoggableToIsEnabledRecipes$LoggerIsLoggableLevelAllRecipe,Replace JUL `Logger.isLoggable(Level.ALL)` with SLF4J's `Logger.isTraceEnabled`,Replace calls to `java.util.logging.Logger.isLoggable(Level.ALL)` with `org.slf4j.Logger.isTraceEnabled()`.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulIsLoggableToIsEnabledRecipes$LoggerIsLoggableLevelConfigRecipe,Replace JUL `Logger.isLoggable(Level.CONFIG)` with SLF4J's `Logger.isInfoEnabled()`,Replace calls to `java.util.logging.Logger.isLoggable(Level.CONFIG)` with `org.slf4j.Logger.isInfoEnabled()`.,1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., @@ -291,13 +289,13 @@ maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.log maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Slf4jLogShouldBeConstant,SLF4J logging statements should begin with constants,"Logging statements shouldn't begin with `String#format`, calls to `toString()`, etc.",1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.StripToStringFromArguments,Strip `toString()` from arguments,"Remove `.toString()` from logger call arguments; SLF4J will automatically call `toString()` on an argument when not a string, and do so only if the log level is enabled.",1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.WrapExpensiveLogStatementsInConditionals,Optimize log statements,"When trace, debug and info log statements use methods for constructing log messages, those methods are called regardless of whether the log level is enabled. This recipe optimizes these statements by either wrapping them in if-statements (SLF4J 1.x) or converting them to fluent API calls (SLF4J 2.0+) to ensure expensive methods are only called when necessary.",1,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4jToSlf4j,Migrate Log4j to SLF4J,"Migrates usage of Apache Log4j to using SLF4J directly. Use of the traditional Log4j to SLF4J bridge can result in loss of performance, as the Log4j messages must be formatted before they can be passed to SLF4J. Note, this currently does not modify `log4j.properties` files.",175,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.ParameterizedLogging,Parameterize SLF4J's logging statements,"Use SLF4J's 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.",11,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4j2ToSlf4j1,Migrate Log4j 2.x to SLF4J 1.x,"Transforms usages of Log4j 2.x to leveraging SLF4J 1.x directly. Note, this currently does not modify `log4j.properties` files.",39,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4j1ToSlf4j1,Migrate Log4j 1.x to SLF4J 1.x,"Transforms usages of Log4j 1.x to leveraging SLF4J 1.x directly. Note, this currently does not modify `log4j.properties` files.",157,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Slf4jBestPractices,SLF4J best practices,Applies best practices to logging with SLF4J.,31,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.CommonsLogging1ToSlf4j1,Migrate Apache Commons Logging 1.x to SLF4J 1.x,Transforms usages of Apache Commons Logging 1.x to leveraging SLF4J 1.x directly.,33,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulToSlf4j,Migrate JUL to SLF4J,Migrates usage of Java Util Logging (JUL) to using SLF4J directly.,207,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JBossLoggingToSlf4j,Migrate JBoss Logging to SLF4J,Migrates usage of the JBoss Logging facade to using SLF4J.,45,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4jToSlf4j,Migrate Log4j to SLF4J,"Migrates usage of Apache Log4j to using SLF4J directly. Use of the traditional Log4j to SLF4J bridge can result in loss of performance, as the Log4j messages must be formatted before they can be passed to SLF4J. Note, this currently does not modify `log4j.properties` files.",195,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.ParameterizedLogging,Parameterize SLF4J's logging statements,"Use SLF4J's 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.",13,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4j2ToSlf4j1,Migrate Log4j 2.x to SLF4J 1.x,"Transforms usages of Log4j 2.x to leveraging SLF4J 1.x directly. Note, this currently does not modify `log4j.properties` files.",43,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Log4j1ToSlf4j1,Migrate Log4j 1.x to SLF4J 1.x,"Transforms usages of Log4j 1.x to leveraging SLF4J 1.x directly. Note, this currently does not modify `log4j.properties` files.",175,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.Slf4jBestPractices,SLF4J best practices,Applies best practices to logging with SLF4J.,35,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.CommonsLogging1ToSlf4j1,Migrate Apache Commons Logging 1.x to SLF4J 1.x,Transforms usages of Apache Commons Logging 1.x to leveraging SLF4J 1.x directly.,35,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JulToSlf4j,Migrate JUL to SLF4J,Migrates usage of Java Util Logging (JUL) to using SLF4J directly.,211,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.JBossLoggingToSlf4j,Migrate JBoss Logging to SLF4J,Migrates usage of the JBoss Logging facade to using SLF4J.,49,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.openrewrite.java.logging.slf4j.AddJBossLogManagerSlf4jProviderDependency,Add JBoss LogManager's SLF4J provider,"When JBoss LogManager is the logging backend, add its SLF4J provider so we can migrate to SLF4J as a logging facade.",5,SLF4J,Logging,Java,Recipes related to [Simple Logging Facade for Java (`SLF4J`)](http://www.slf4j.org/).,Enforce logging best practices and migrate between logging frameworks.,Basic building blocks for transforming Java code., -maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.apache.logging.log4j.InlineLog4jApiMethods,Inline `log4j-api-2` methods annotated with `@InlineMe`,Automatically generated recipes to inline method calls based on `@InlineMe` annotations discovered in the type table.,11,Log4j,Logging,Apache,,,, +maven,org.openrewrite.recipe:rewrite-logging-frameworks,org.apache.logging.log4j.InlineLog4jApiMethods,Inline `log4j-api-2` methods annotated with `@InlineMe`,Automatically generated recipes to inline method calls based on `@InlineMe` annotations discovered in the type table.,13,Log4j,Logging,Apache,,,, diff --git a/src/main/resources/META-INF/rewrite/slf4j.yml b/src/main/resources/META-INF/rewrite/slf4j.yml index 951f6905..f8953980 100644 --- a/src/main/resources/META-INF/rewrite/slf4j.yml +++ b/src/main/resources/META-INF/rewrite/slf4j.yml @@ -210,7 +210,7 @@ tags: - java-util-logging - slf4j recipeList: - - org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactoryRecipes + - org.openrewrite.java.logging.slf4j.JulGetLoggerToLoggerFactory - org.openrewrite.java.logging.slf4j.JulIsLoggableToIsEnabledRecipes - org.openrewrite.java.logging.slf4j.JulParameterizedArguments - org.openrewrite.java.logging.slf4j.JulToSlf4jLambdaSupplierRecipes