Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reduce Java logging boilerplate code?

Tags:

java

logging

Every class that wants to use java.util.logging generally needs to declare a logger like this:

public class MyClass {
    private static Logger _log = Logger.getLogger(MyClass.class.getName());
}

How do you avoid this MyClass.class.getName() boilerplate code?

like image 909
Steven Huwig Avatar asked Oct 17 '08 15:10

Steven Huwig


People also ask

How does spring boot reduce boilerplate code?

Spring boot avoids all these boilerplate codes. Boilerplate code: refers to the lots of code which programmer must write to do minimal jobs. So, spring boot increase productivity by removing the boilerplate code and minimizing the programmer's effort.

What is boilerplate code in Java?

In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered verbose, the programmer must write a lot of boilerplate code to accomplish only minor functionality.

Which logging framework is best for Java?

One of the most popular solutions for the Java world is the Apache Log4j 2 framework.


1 Answers

I have a template set up in Eclipse so that I only have to type a portion of the declaration, and then Eclipse will auto-complete the rest for me.

${:import(org.apache.log4j.Logger)}
private final static Logger log = Logger.getLogger(${enclosing_type}.class);
${cursor}

So, I only have to type logger, hit Ctrl+Space, followed by Enter, and Eclipse fills in the rest for me and adds the import declaration as well.

This won't cut down on the amount of boilerplate code, but at least it cuts down on the amount of keystrokes.

like image 180
matt b Avatar answered Sep 30 '22 10:09

matt b