Re: Is the Checkstyle project interested in a Log4J Best Practice checker?

Oliver Burn <[email protected]> Thu, 18 Dec 2008 21:21:06 +1100
Newsgroups gmane.comp.java.audit.checkstyle.user
Message-ID <[email protected]>
Hi,

I also interesting in finding out more. Your link 
http://www.javaworld.com/community/node/1882 does not work.

What state is the code in - and have you read 
http://checkstyle.sourceforge.net/contributing.html ?

Cheers,
Oliver

Jeff Jensen wrote:
> Great!
>
> I am wondering about a couple of things, if you don't mind me asking...
>
> Are these individual checks, or one check that does all?  (I am wondering if we can disable one or more)
>
> With the "Avoid using parameter construction within the logging request" check, does it work with Slf4j's parametric logging?
>
>     LOGGER.debug("value1={}, value2={}", value1, value2);
>
> That log message must not fail the check with Slf4j (Slf4j does not call toString() on the message parameters (value1, value2) if the level is not active), as the message is a constant and the check should handle the additional parameters. 
>
>
> -----Original Message-----
> From: Klaus-Peter Berg [mailto:[email protected]] 
> Sent: Wednesday, December 17, 2008 8:48 AM
> To: [email protected]
> Subject: Re: [Checkstyle-user] Is the Checkstyle project interested in a Log4J Best Practice checker?
>
>
> I have extended the logging checker module to cover SLF4J as well. It can be
> configured by a property.
> Furthermore I have written a Checkstyle JUnit test to deal with both Log4J
> and SLF4J in order to improve the checker's quality. However, there is no
> Emma coverage report available right now ;-) 
>
> --Klaus
>
>
> Jeff Jensen-2 wrote:
>   
>> 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 verlngert: WEB.DE FreeDSL - Telefonanschluss + DSL
>> fr 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/
>> _______________________________________________
>> 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/

_______________________________________________
Checkstyle-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user