log4j logging made easier with Eclipse templates

Logging statements have taken up a nice chunk of my programming life. Info, debug and warning statements are common, especially since some team standards require that logging be done consistently (eg. at the start and end of all methods). Take the following code:

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

    public void someMethod() {
        ...
        logger.debug("Doing something here");
        ...
    }

To get the statement on line 8 you first have to insert line 4, generate the import on line 1 (eg. with Ctrl+Shift+O) and then move back to line 8 to type out the statement. That’s a lot of time for just one statement.

So here are some templates to help with this. The templates assume log4j since most people use that (right?), but can easily be adapted for Java Logging or other framework. If you’re new to templates, then see this tip to get an overview.

Import the templates (recommended)

Download the relevant template below then import it into Eclipse.

  • log4j-templates – This bundle includes:
    logger: Creates a private static field of type Logger with the default name logger with an import statement. You have to stand outside a method when you run this because it creates a private static variable.
    loginfo: Creates an info statement with logger.info().See the notes below for usage.
    logdebug: Creates a debug statement with logger.debug(). See the notes below for usage.
    logerror: Creates an error statement with logger.error(). See the notes below for usage.

Notes:

  • The 3 logging statements are generated with quotes to type the message. You can use Tab to move into the quotes and Tab again to move to the end of the line.
  • Unfortunately I haven’t found a template variable for creating a new static field automatically, so you have to stand outside a method when you invoke the logger template. Ah well…

Create the templates manually

Alternatively, create the templates with the following settings.

Name logger
Context Java
Pattern
${:import(org.apache.log4j.Logger)}private static final Logger ${logger} = Logger.getLogger(${enclosing_type}.class);
Name loginfo
Context Java statements
Pattern
${logger:var(org.apache.log4j.Logger)}.info("${}");${cursor}
Name logdebug
Context Java statements
Pattern
${logger:var(org.apache.log4j.Logger)}.debug("${}");${cursor}
Name logerror
Context Java statements
Pattern
${logger:var(org.apache.log4j.Logger)}.error("${}");${cursor}

Related Tips

Support Eclipse On E

5 thoughts on “log4j logging made easier with Eclipse templates

  1. Don’t shoot the messenger…
    … but Log4j is dead (no development in 4 years). The creator of it started slf4j with logback which should have been named log4j 2 (but wasn’t due to political reasons at the time). It’s much cleaner and must faster.

    • Don’t worry, no shots fired here. And I wouldn’t argue with your points about slf4j & logback – it does look good. But I wouldn’t agree that log4j is dead (not yet). I reckon there are still hundreds of projects out there still using log4j and the mailing lists are still active, so there’s some life in the old logger. And it still does the job most of the time and a whole lot better than Java Logging, even after all these years.

      But slf4j is certainly growing fast. So you might just see some slf4j templates here soon…

  2. Pingback: Plantillas de Eclipse « Java Mania

  3. Thx 4 tht post, your hint still works in 2015 using Log4J 2.4., just needs some tiny adaptions.

    Imported the XML-File “log4j-templates.xml” to Eclipse Mars via >Eclipse >Window >Preferences >Java >Editor >Templates >Import. Then changed template “logger” from “private static final Logger logger = Logger.getLogger(${enclosing_type}.class);” to “private static final Logger logger = LogManager.getLogger({enclosing_type}.class);”.

    Also, we need these two imports now instead of one: “org.apache.logging.log4j.LogManager”;
    “org.apache.logging.log4j.Logger” instead of “org.apache.log4j.Logger”.

    • Thanks for that Michael. I haven’t worked heavily with that recent a version of log4j so thanks for the update.

Leave a Reply

Your email address will not be published. Required fields are marked *