Skip to content

Commit

Permalink
Allow PreferSafeLogger to migrate uses with level-checks (#1842)
Browse files Browse the repository at this point in the history
Allow `PreferSafeLogger` to migrate logger uses which include level-checks
  • Loading branch information
carterkozak authored Jul 22, 2021
1 parent 779c6db commit adc6b35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ private static boolean isSafeSlf4jInteraction(MethodInvocationTree tree, Visitor
}
List<? extends Tree> arguments = tree.getArguments();
if (arguments.isEmpty()) {
return false;
// is<level>Enabled
return true;
}
if (!state.getTypes()
.isSameType(ASTHelpers.getType(arguments.get(0)), state.getTypeFromString(String.class.getName()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ void testSimpleFixWithArgAndThrowable() {
.doTest();
}

@Test
void testFixWithLevelCheck() {
fix().addInputLines(
"Test.java",
"import org.slf4j.*;",
"class Test {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
" void action(Throwable t) {",
" if (log.isInfoEnabled()) {",
" log.info(\"foo\", t);",
" }",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.palantir.logsafe.logger.SafeLogger;",
"import com.palantir.logsafe.logger.SafeLoggerFactory;",
"import org.slf4j.*;",
"class Test {",
" private static final SafeLogger log = SafeLoggerFactory.get(Test.class);",
" void action(Throwable t) {",
" if (log.isInfoEnabled()) {",
" log.info(\"foo\", t);",
" }",
" }",
"}")
.doTest();
}

@Test
void testIgnoresIncorrectlyOrderedThrowables() {
fix().addInputLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1842.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Allow `PreferSafeLogger` to migrate logger uses which include level-checks
links:
- https://github.com/palantir/gradle-baseline/pull/1842

0 comments on commit adc6b35

Please sign in to comment.