1/19/2015

Automatic logger generation using IntelliJ Idea's Live Templates (use it with log4j, slf4j, commons-logging, etc by changing the template line accordingly)

In IntelliJ Idea, go to File -> Settings -> Live Templates, and click the + sign. Fill in the details:

  • Abbreviation: log
  • Description: log
  • Check "Reformat according to style" will indent the generated line appropriately.
  • Checking "Shorten FQ names" will remove "org.apache.log4j." and replace it with an import.

"Template text" is

private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);

The same for slf4j is

private final static Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);

Now click "Edit Variables", and in the CLASS_NAME row, enter className() in the Expression column.

Almost done - we still need to tell IDEA that we need this for Java stuff. Where it says "No applicable contexts", click "Define" - click JAVA, and OK.

Open any of your classes, and into a newline under "public class Blah", type "log" and press tab - and the logger line will be created appropriately:

private static final Logger LOGGER = Logger.getLogger(HSQLSequenceGenerator.class);