Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3577

FP in S3457 when slf4j log arguments contains a concatenation and a single Throwable

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.10
    • Component/s: Rules
    • Labels:
      None

      Description

      org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("MyClass");
      // ...
      catch (Exception e) {
        log.error("message: " + e.getMessage(), new Exception()); // FP
      }
      

      In this code, error(String, Throwable) will be called, and the rule suggests to use format specifier. The resulting code would look like:

      catch (Exception e) {
        log.error("message: {}", e.getMessage(), new Exception());
      }
      

      It compiles, log.error(String, Object, Object) will be called and the resulting log will be exactly the same for SimpleLogger/Logback. However, we can not assume that any other underlying log framework will correctly handle the case where the last Object argument is a Throwable, as it is not explicitly stated in the documentation.

      We should not report an issue when slf4j contains a string concatenation and a Throwable as second argument.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              quentin.jaquier Quentin Jaquier
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: