David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler

    Web app developer since 1994 and Google Cloud Platform Instructor now residing in Colorado. Besides tech, I enjoy landscape photography and share my work at ColoradoPhoto.gallery.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 224 other followers

  • Sleepless Nights…

    March 2010
    S M T W T F S
  • Blog Stats

    • 1,034,589 hits

Coding getLogger() quickly with an Eclipse template

Posted by David Chandler on March 12, 2010

The AppEngine admin console log viewer is so powerful that it makes me want to log anything and everything. One obstacle to this (however minor) is that it’s a pain to write this for every class in which I want to log:

private static final Logger LOG = Logger.getLogger(MyLongClassName.class.getName());

Eclipse code templates to the rescue! Open Window | Preferences | Java | Editor | Templates and create a new template. Name it “log”, set the context to “Java type members”, and enter this pattern:

private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getName());

Click OK to save it. Now open any class in the editor, and type “log” followed by Ctrl-Space. Press Enter. Presto!

I feel a template for AsyncCallbacks coming on…

3 Responses to “Coding getLogger() quickly with an Eclipse template”

  1. Eric Jablow said

    Perhahaps you should consider using AOP to log server calls. AspectJ is pretty useful here. Spring AOP is quite simple.

    • Thanks, Eric. I didn’t say it in the post, but the context here is an AppEngine project, and many of the reflection-like operations needed for AOP are not in the JRE whitelist, so I haven’t put a lot of energy into getting it to work. Plus, I need ultra-lean startups due to AppEngine cold starts, so startup-time bytecode enhancement, etc. are undesirable. But if anyone’s done lazy init AOP on AppEngine, I’m all ears.

      • Eric Jablow said

        My projects may not use AppEngine, so I have not bothered to learn about it. However, Guice uses aopalliance.jar and has method interception, so there may be a way to do it. I have no idea if it could be done fast. If I were programming a non-AppEngine application, I certainly would use AOP for logging.

        I have no idea what is suitable for your situation. I just don’t like boilerplate code. I have used similar Eclipse templates myself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: