Rapid logger creation in Eclipse

In Eclipse it's possible to create easy to insert templates. Probably you have used "main" template by typing "main" and pressing Ctrl + Space to create main method. You also probably have faced the problem of logging huge amount of classes, and everywhere you needed to use logger factory, type current class name and choose imports you want to add (that is very tadioous in my opinion).

Here I'll show you how to add Eclipse template for rapid creation of slf4j loggers in your classes.

You need to do the following two steps for this purpose:

  1. Open Window -> Preferences -> Java -> Editor -> Templates -> New...
  2. Type any name you want to associate with creation of slf4j logger (say, "logger"). You will type this name and press Ctrl+Space to create logger instance.
  3. Insert the following snippet to pattern text area:

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

That's it. Save changes and enjoy logging.

Tagged as : Eclipse