A logger (log4j) template for the eclipse java editor

Posted on 09/06/2009 by Charalampos Chrysikopoulos

If you are using log4j then you probably are tired of the copy/pasting the logger from on class to an other. There is a simple way to automate this by using the templates in the java editor in eclipse.

Try this out
  • open the eclipse Preferences
  • in the search field type template or follow the path: Java > Editor > Templates
  • There you can find any predefined templates you can use while editing a java file
  • press New...
  • In the name field type logger (this is the keyword you should type in the editor so that the code assistance activates the current temaplate). You could type log if you wish....
  • As pattern you can try this (of course you can change this as you wish by using the predefined variables in eclipse): privatestaticfinal Logger logger = Logger.getLogger(${enclosing_type}.class);
  • ${enclosing_type} is a variable and returns the class name of the file that is being currently edited.
  • press OK and then again OK
  • try it out in a java class. Just write logger under the class declaration and press ctrl + space. Select logger from the assistant
  • press alt + shift + O to automatically import the log4j Logger class
The result should be something like this:


import org.apache.log4j.Logger;
public class ExampleClass {
private static final Logger logger = Logger.getLogger(ExampleClass.class);

If you are working with liferay's log you can use the following template:

private static final Log logger = LogFactoryUtil.getLogger(${enclosing_type}.class);
This entry was posted in eclipse, log4j, template and tagged by Charalampos Chrysikopoulos

Add new comment