-
Notifications
You must be signed in to change notification settings - Fork 30
Add handling of convertion JUL to slf4j calls with Throwable; #155 #164
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
Merged
timtebeek
merged 5 commits into
openrewrite:main
from
woj-tek:jul-to-slf4j-calls-with-throwable
Jul 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7fcf211
Add handling of convertion JUL to slf4j calls with Throwable; #155
woj-tek 0fc6e50
Fix docs recipe docs per instance
timtebeek a26d2f7
Correctly use `Logger`, not `logger` in docs
timtebeek bd809e1
Update class level RecipeDescriptors
timtebeek 016388c
Drop excess document example; we only need one
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
src/main/java/org/openrewrite/java/logging/slf4j/JulToSlf4jLambdaSupplierWithThrowable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * Copyright 2024 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.slf4j; | ||
|
|
||
| import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
| import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
| import org.openrewrite.java.template.RecipeDescriptor; | ||
|
|
||
| import java.util.function.Supplier; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL active Level check with corresponding SLF4J method calls", | ||
| description = "Replace calls to `Logger.isLoggable(Level)` with the corresponding SLF4J method calls." | ||
| ) | ||
| public class JulToSlf4jLambdaSupplierWithThrowable { | ||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINEST, e, Supplier<String>)` with SLF4J's `Logger.atTrace().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINEST, e, Supplier<String>)` with `org.slf4j.Logger.atTrace().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFinest { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.FINEST, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atTrace().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINER, e, Supplier<String>)` with SLF4J's `Logger.atTrace().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINER, e, Supplier<String>)` with `org.slf4j.Logger.atTrace().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFiner { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.FINER, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atTrace().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINE, e, Supplier<String>)` with SLF4J's `Logger.atDebug().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINE, e, Supplier<String>)` with `org.slf4j.Logger.atDebug().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFine { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.FINE, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atDebug().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.CONFIG, e, Supplier<String>)` with SLF4J's `Logger.atInfo().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.CONFIG, e, Supplier<String>)` with `org.slf4j.Logger.atInfo().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierConfig { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.CONFIG, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atInfo().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.INFO, e, Supplier<String>)` with SLF4J's `Logger.atInfo().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.INFO, e, Supplier<String>)` with `org.slf4j.Logger.atInfo().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierInfo { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.INFO, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atInfo().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.WARNING, e, Supplier<String>)` with SLF4J's `Logger.atWarn().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.WARNING, e, Supplier<String>)` with `org.slf4j.Logger.atWarn().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierWarning { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.WARNING, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atWarn().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.SEVERE, e, Supplier<String>)` with SLF4J's `Logger.atError().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.SEVERE, e, Supplier<String>)` with `org.slf4j.Logger.atError().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierSevere { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.SEVERE, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atError().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.ALL, e, Supplier<String>)` with SLF4J's `Logger.atTrace().log(Supplier<String>)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.ALL, e, Supplier<String>)` with `org.slf4j.Logger.atTrace().log(Supplier<String>)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierAll { | ||
| @BeforeTemplate | ||
| void before(Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.log(Level.ALL, e, supplier); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, Supplier<String> supplier, Throwable e) { | ||
| logger.atTrace().setCause(e).log(supplier); | ||
| } | ||
| } | ||
|
|
||
| } | ||
157 changes: 157 additions & 0 deletions
157
src/main/java/org/openrewrite/java/logging/slf4j/JulToSlf4jSimpleCallsWithThrowable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * Copyright 2024 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.slf4j; | ||
|
|
||
| import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
| import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
| import org.openrewrite.java.template.RecipeDescriptor; | ||
|
|
||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL active Level check with corresponding SLF4J method calls", | ||
| description = "Replace calls to `Logger.isLoggable(Level)` with the corresponding SLF4J method calls." | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| public class JulToSlf4jSimpleCallsWithThrowable { | ||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINEST, e, String message)` with SLF4J's `Logger.trace(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINEST, e, String message)` with `org.slf4j.Logger.trace(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFinest { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.FINEST, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.trace(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINER, e, String message)` with SLF4J's `Logger.trace(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINER, e, String message)` with `org.slf4j.Logger.trace(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFiner { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.FINER, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.trace(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.FINE, e, String message)` with SLF4J's `Logger.debug(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.FINE, e, String message)` with `org.slf4j.Logger.debug(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierFine { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.FINE, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.debug(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.CONFIG, e, String message)` with SLF4J's `Logger.info(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.CONFIG, e, String message)` with `org.slf4j.Logger.info(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierConfig { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.CONFIG, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.info(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.INFO, e, String message)` with SLF4J's `Logger.info(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.INFO, e, String message)` with `org.slf4j.Logger.info(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierInfo { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.INFO, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.info(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.WARNING, e, String message)` with SLF4J's `Logger.warn(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.WARNING, e, String message)` with `org.slf4j.Logger.warn(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierWarning { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.WARNING, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.warn(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.SEVERE, e, String message)` with SLF4J's `Logger.error(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.SEVERE, e, String message)` with `org.slf4j.Logger.error(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierSevere { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.SEVERE, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.error(message, e); | ||
| } | ||
| } | ||
|
|
||
| @RecipeDescriptor( | ||
| name = "Replace JUL `logger.log(Level.ALL, e, String message)` with SLF4J's `Logger.trace(message, e)`", | ||
| description = "Replace calls to `java.util.logging.logger.log(Level.ALL, e, String message)` with `org.slf4j.Logger.trace(message, e)`." | ||
| ) | ||
| public static class JulToSlf4jSupplierAll { | ||
| @BeforeTemplate | ||
| void before(Logger logger, String message, Throwable e) { | ||
| logger.log(Level.ALL, message, e); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(org.slf4j.Logger logger, String message, Throwable e) { | ||
| logger.trace(message, e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.