Wednesday, May 12, 2010

Creating a log4j logger in an Eclipse template

private static Logger logger = Logger.getLogger( MyClass.class );
Are you tired of writing the same line of code over and over again?

No more!


Using the Eclipse templates you can generate this code easily.
Create the template by clicking Window -> Preferences -> Java -> Editor -> Templates -> New

Enter the template name
logger


Enter the pattern
private static Logger logger = Logger.getLogger( ${enclosing_type}.class );
${:import(org.apache.log4j.Logger)}
This pattern also takes care of the import statement.


Creating an Eclipse template




Now see it in action:




7 comments:

  1. Thanks for this one...... I really appreciate these small improvements.

    ReplyDelete
  2. small tip but very useful thanks very muhc

    ReplyDelete
  3. Thanks a lot ............

    ReplyDelete
  4. Great post! Very nice addition :-)

    If someone prefers Commons Logging the template could look like this:

    private static final Log log = LogFactory.getLog( ${enclosing_type}.class );
    ${:import(org.apache.commons.logging.Log,org.apache.commons.logging.LogFactory)}

    ReplyDelete
  5. If you want this to happen automatically when you create a new class http://javatechtalkies.blogspot.com/2010/05/eclipse-create-class-with-logger-member.html

    ReplyDelete
  6. Thanks. Slf4j is:

    private static Logger logger = LoggerFactory.getLogger(${enclosing_type}.class);
    ${:import(org.slf4j.Logger, org.slf4j.LoggerFactory)}

    ReplyDelete