Slf4jLoggerDeclaration

WARNING

Style

View source code on GitHub

Summary

SLF4J logger declarations should follow established best-practices

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("Slf4jLoggerDeclaration") to the enclosing element.

Disable this pattern completely by adding -Xep:Slf4jLoggerDeclaration:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 class A {
-  static Logger foo = LoggerFactory.getLogger(Logger.class);
+  private static final Logger LOG = LoggerFactory.getLogger(A.class);
 
   abstract static class DynamicLogger {
-    transient Logger BAR = LoggerFactory.getLogger(getClass());
+    private final Logger log = LoggerFactory.getLogger(getClass());
   }
 
   static final class StaticLogger {
-    transient Logger baz = LoggerFactory.getLogger(LoggerFactory.class);
+    private static final Logger LOG = LoggerFactory.getLogger(StaticLogger.class);
   }
 
   static final class StaticLoggerWithCustomIdentifier {
-    transient Logger qux = LoggerFactory.getLogger("custom-identifier");
+    private static final Logger LOG = LoggerFactory.getLogger("custom-identifier");
   }
 
   interface StaticLoggerForInterface {
-    public static final Logger quux = LoggerFactory.getLogger(A.class);
+    Logger LOG = LoggerFactory.getLogger(StaticLoggerForInterface.class);
   }
 }
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 class A {
-  transient Logger LOG = LoggerFactory.getLogger(Logger.class);
+  private static final Logger FOO_BAR = LoggerFactory.getLogger(A.class);
 
   abstract static class DynamicLogger {
-    transient Logger log = LoggerFactory.getLogger(getClass());
+    private final Logger fooBar = LoggerFactory.getLogger(getClass());
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import static java.lang.Class.forName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class A {
  private static final long serialVersionUID = 1L;
  private static final Logger LOG = LoggerFactory.getLogger(A.class);

  abstract static class DynamicLogger {
    private final Logger log = LoggerFactory.getLogger(getClass());
  }

  abstract static class DynamicLoggerWithExplicitThis {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
  }

  static final class StaticLogger {
    private static final Logger LOG = LoggerFactory.getLogger(StaticLogger.class);
  }

  static final class StaticLoggerWithCustomIdentifier {
    private static final Logger LOG = LoggerFactory.getLogger("custom-identifier");
  }

  interface StaticLoggerForInterface {
    Logger LOG = LoggerFactory.getLogger(StaticLoggerForInterface.class);
  }

  abstract static class DynamicLoggerForWrongTypeWithoutReceiver {
    // BUG: Diagnostic contains:
    private final Logger log = LoggerFactory.getLogger(forName("A.class"));

    DynamicLoggerForWrongTypeWithoutReceiver() throws ClassNotFoundException {}
  }

  abstract static class DynamicLoggerForWrongTypeWithoutSymbol {
    // BUG: Diagnostic contains:
    private final Logger log = LoggerFactory.getLogger("foo".getClass());
  }

  abstract static class DynamicLoggerForWrongTypeWithSymbol {
    // BUG: Diagnostic contains:
    private final Logger log = LoggerFactory.getLogger(new A().getClass());
  }

  static final class NonAbstractDynamicLogger {
    // BUG: Diagnostic contains:
    private final Logger log = LoggerFactory.getLogger(getClass());
  }

  abstract static class DynamicLoggerWithMissingModifier {
    // BUG: Diagnostic contains:
    final Logger log = LoggerFactory.getLogger(getClass());
  }

  abstract static class DynamicLoggerWithExcessModifier {
    // BUG: Diagnostic contains:
    private final transient Logger log = LoggerFactory.getLogger(getClass());
  }

  abstract static class MisnamedDynamicLogger {
    // BUG: Diagnostic contains:
    private final Logger LOG = LoggerFactory.getLogger(getClass());
  }

  static final class StaticLoggerWithMissingModifier {
    // BUG: Diagnostic contains:
    static final Logger LOG = LoggerFactory.getLogger(StaticLoggerWithMissingModifier.class);
  }

  static final class StaticLoggerWithExcessModifier {
    // BUG: Diagnostic contains:
    private static final transient Logger LOG =
        LoggerFactory.getLogger(StaticLoggerWithExcessModifier.class);
  }

  static final class MisnamedStaticLogger {
    // BUG: Diagnostic contains:
    private static final Logger log = LoggerFactory.getLogger(MisnamedStaticLogger.class);
  }

  static final class StaticLoggerWithIncorrectIdentifier {
    // BUG: Diagnostic contains:
    private static final Logger LOG = LoggerFactory.getLogger(A.class);
  }

  static final class StaticLoggerWithCustomIdentifierAndMissingModifier {
    // BUG: Diagnostic contains:
    static final Logger LOG = LoggerFactory.getLogger("custom-identifier");
  }

  static final class StaticLoggerWithCustomIdentifierAndExcessModifier {
    // BUG: Diagnostic contains:
    private static final transient Logger LOG = LoggerFactory.getLogger("custom-identifier");
  }

  static final class MisnamedStaticLoggerWithCustomIdentifier {
    // BUG: Diagnostic contains:
    private static final Logger log = LoggerFactory.getLogger("custom-identifier");
  }

  interface StaticLoggerForInterfaceWithExcessModifier {
    // BUG: Diagnostic contains:
    static Logger LOG = LoggerFactory.getLogger(StaticLoggerForInterfaceWithExcessModifier.class);
  }

  interface MisnamedStaticLoggerForInterface {
    // BUG: Diagnostic contains:
    Logger log = LoggerFactory.getLogger(MisnamedStaticLoggerForInterface.class);
  }

  interface StaticLoggerForInterfaceWithIncorrectIdentifier {
    // BUG: Diagnostic contains:
    Logger LOG = LoggerFactory.getLogger(A.class);
  }
}

Copyright © 2017-2024 Picnic Technologies BV