Skip to content

Remove .toString() called on parameterized logging statement arguments #223

@timtebeek

Description

@timtebeek

What problem are you trying to solve?

Remove .toString() called on parameterized logging statement arguments.

The logger already calls .toString() on any non string argument, so there's a potential simplification & performance improvement.

What precondition(s) should be checked before applying this recipe?

Uses logging method and uses toString()

Describe the situation before/after applying the recipe

@Test
void stripToStringFromArguments() {
    rewriteRun(
      //language=java
      java(
        """
          import org.slf4j.Logger;

          class Test {
              static void method(Logger logger, Object person) {
                  logger.warn("Hello {}", person.toString());
              }
          }
          """,
        """
          import org.slf4j.Logger;

          class Test {
              static void method(Logger logger, Object person) {
                  logger.warn("Hello {}", person);
              }
          }
          """
      )
    );
}

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions