Simple Log4J eclipse template

Do you use eclipse and log4j? Do you have a template to add a static Logger instance in classes? Do you have to manually add the import? HA! NO MORE!

Under Preferences > Java > Editor > Templates, click New…

Give the template a name (like “logger”).

Use this for the Pattern:

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

Click OK, OK, then incant the template by typing the name of the template and invoke Content Assist (ctrl-space on most platforms).

The import line will add org.apache.log4j.Logger to your imports automatically if it doesn’t exist already. Nifty, eh?

Related posts:

  1. Simple spring integration testing
    Spring has had a really nice unit test framework available for a while now, but the documentation can be a bit daunting. Here’s a super-simple example of adding dependency injection......
  2. IDEA versus Eclipse
    IDEA has been giving me grief lately. Fed up with mysterious pauses, bugs in svn moves, and crashes even after performing cache-deletion voodoo in IDE, I’m back to trying Eclipse.......
  3. How to increase maven heapspace in hudson builds
    If your maven-built project fails in hudson (especially when you’re using the assembly plugin) and it isn’t a compile or test failure, check the console output. If it says “java.lang.OutOfMemoryError:......
  4. How to serve a transparent 1×1 pixel GIF from a servlet
    The first issue was how to build the smallest possible byte array that represents a 1×1 GIF. Using ImageMagick piped to base64 made it easy to embed into java code:......

  • http://blog.akoblov.ru adaptun

    Thanks.
    It has saved me half an hour this day.

  • http://www.plexapp.com darrin

    Nice… thanks much. Coming from intelliJ I’m still learning some of the tips and tricks related to eclipse. One change I made here was to use ‘.getCanonicalName()’ like this :

    ${:import(org.apache.log4j.Logger)}
    private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getCanonicalName());