Re: Is the Checkstyle project interested in a Log4J Best Practice checker?
"Jeff Jensen" <[email protected]> Sat, 13 Dec 2008 13:36:03 -0600
| Newsgroups | gmane.comp.java.audit.checkstyle.user |
|---|---|
| Message-ID | <000601c95d5a$0d302a70$27907f50$@org> |
I am interested in this kind of check. How Log4j dependent are they? We use Slf4j and Logback - would they work with that? -----Original Message----- From: Klaus-Peter Berg [mailto:[email protected]] Sent: Tuesday, December 09, 2008 2:22 AM To: [email protected] Subject: [Checkstyle-user] Is the Checkstyle project interested in a Log4J Best Practice checker? Hello, I have written a Checkstyle-4.4 compatible Log4jCheck (together with some JUnit tests) to address some logging, i.e., Log4J, "best practices": + Logger category according to Fully Qualified Class Name(FQCN) + Logging performance: isDebugEnabled() etc. + Avoid logging inside loops (but logs inside a catch block are allowed) + Avoid logging together with exception propagation (?log & propagate?) + Avoid usage of System.out/err.println() & e.printStackTrace() when you decided to use logging I want to explain these rules shortly: * You should decide on a naming scheme that assigns each log message to a particular Logger (Category). Common practice: Use the fully qualified name of a class as the logger name, and declare the logger as private static final, e.g. private static final Logger LOGGER = Logger.getLogger(MyClass.class); This allows developers to fine-tune log settings for each class (this kind of Category name is also ?refactoring-safe?). The downside: beware of copy/paste errors that could lead to a wrong class name as the argument of Logger.getLogger(..)! So, it should be checked that this class is the same as the source file tells us. * Avoid using parameter construction within the logging request ==> check if the log level is enabled before trying to log! (at least for info/debug/trace messages), e.g., using "if (LOGGER.isDebugEnabled() {?}" (of course, if you are logging only a String constant this rule does not fit) *Info, debug, and trace logs should not be included in loops with potentially large number of iterations (from the Log4J manual: ?Inserting logging statements in tight loops or very frequently invoked code is a lose-lose proposal?); such log statements can pollute your log file! Even be careful to log ERROR/WARNING messages in loops; maybe it is better to produce a ?summary? to be logged outside the loop *Avoid log-and-propagate Sometimes exceptions are logged AND propagated to the caller. But the caller (e.g., another developer) is not aware of the logging process already performed. So he/she may repeat this process (?log-and-propagate?) what leads to multiple entries in the log file for the same problem! Additional rules that fall into this category a looking for System.out.println/System.err.println and e.printStackTrace() that can easily be checked with a slightly modified Checkstyle "Generic regex" rule to avoid "false positives" when such println calls are made inside comments. The current module is tailored to check Log4J but I think it could be extended to look at Sun's Java util logging as well. For more information see my blog entry at http://www.javaworld.com/community/node/1882. Please contact me if the Checkstyle project is interesed in this additional module... --Klaus _______________________________________________________________________ Sensationsangebot verlängert: WEB.DE FreeDSL - Telefonanschluss + DSL für nur 16,37 Euro/mtl.!* http://dsl.web.de/?ac=OM.AD.AD008K13805B7069a ---------------------------------------------------------------------------- -- SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada. The future of the web can't happen without you. Join us at MIX09 to help pave the way to the Next Web now. Learn more and register at http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/ _______________________________________________ Checkstyle-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/checkstyle-user ------------------------------------------------------------------------------ SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada. The future of the web can't happen without you. Join us at MIX09 to help pave the way to the Next Web now. Learn more and register at http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/