DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Class Rename Friendly Log Instance

04.12.2005
| 2503 views |
  • submit to reddit
        Use the following to automatically use the current class name for the log category (example is for commons-logging, works with log4j, too):

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

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.

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