Since CodeNarc 0.12
Checks for instantiating a logger for a class other than the current class. Checks for logger instantiations for Log4J, SLF4J, Logback, Apache Commons Logging and Java Logging API (java.util.logging).
Limitations:
Here are examples of Log4J or Java Logging API code that cause violations:
class MyClass { private static final LOG = Logger.getLogger(SomeOtherClass) // violation def log1 = Logger.getLogger(SomeOtherClass.class) // violation def log2 = Logger.getLogger(SomeOtherClass.class.name) // violation }
Here are examples of Commons Logging code that cause violations:
class MyClass { private static final LOG = LogFactory.getLog(SomeOtherClass) // violation Log log1 = LogFactory.getLog(SomeOtherClass.class) // violation def log2 = LogFactory.getLog(SomeOtherClass.class.getName()) // violation }
Here are examples of code that does NOT cause violations:
// Log4J or Java Logging API class MyClass { private static final LOG = Logger.getLogger(MyClass) // ok def log2 = Logger.getLogger(MyClass.class) // ok private static log3 = Logger.getLogger(MyClass.getClass().getName()) // ok private static log4 = Logger.getLogger(MyClass.getClass().name) // ok private static log5 = Logger.getLogger(MyClass.class.getName()) // ok private static log6 = Logger.getLogger(MyClass.class.name) // ok } // Commons Logging class MyClass { private static final LOG = LogFactory.getLog(MyClass) // ok def log2 = LogFactory.getLog(MyClass.class) // ok private static log3 = LogFactory.getLog(MyClass.getClass().getName()) // ok private static log4 = LogFactory.getLog(MyClass.getClass().name) // ok private static log5 = LogFactory.getLog(MyClass.class.getName()) // ok private static log6 = LogFactory.getLog(MyClass.class.name) // ok }
Since CodeNarc 0.12
If you are logging an exception then the proper API is to call error(Object, Throwable), which will log the message and the exception stack trace. If you call error(Object) then the stacktrace may not be logged.
Since CodeNarc 0.12
Logger objects should be declared private, static, and final. This rule find loggers that are not declared with these modifiers.
Since CodeNarc 0.12
This rule catches classes that have more than one logger object defined. Typically, a class has zero or one logger objects.
Checks for calls to this.print(), this.println() or this.printf(). Consider using a standard logging facility instead.
Checks for calls to printStackTrace(). Consider using a standard logging facility instead.
Checks for calls to System.err.print(), System.err.println() or System.err.printf(). Consider using a standard logging facility instead.
Checks for calls to System.out.print(), System.out.println() or System.out.printf(). Consider using a standard logging facility instead.