Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Class rename friendly log instance (See related posts)

Use the following to automatically use the current class name for the log category (example is for commons-logging, works with log4j, too):

   1  
   2  class Foo {
   3     private final Log log = LogFactory.getLog(getClass());
   4  }


Compare to the following where the class name is refered to explicitely. If the class is renamed or parts of the codes are copied ("reused") the logging will by misleading.

   1  
   2  class Foo {
   3     private static final Log log = LogFactory.getLog(Foo.class);
   4  }




You need to create an account or log in to post comments to this site.


Click here to browse all 5350 code snippets

Related Posts