Java Logging - Wrapper?

I start with a new project and do some thoughts on registration. I always used a template in which each class in which logging is performed has its own static logger:

private static final Logger logger = Logger.getLogger(LoggingInterceptor.class); 

I do not like this approach, since I have to copy this line to each class where I will write something. I was considering using the Android approach, where there is a log class with its static methods for logging. I began to search the Internet for a similar approach made by someone else, but found nothing.

So my question is: what could be the disatvantages of this approach?

I can’t think of anything, but some advantages, because it follows the DRY-pattern. Different categories can be processed as in Android with "tags", which are parameters of static log methods. For example:

  Log.debug(tag, message, exception); 

The log class itself will use a common logging structure such as Log4j or even SLF4J.

Therefore, I am interested in your opinions.

+7
java logging
source share
1 answer

Based on the java.util.logging.Logger API and in this article , the main reason for getLogger() is to ensure that the same set of Logger and Handler used independently between the subsystems.

The recommended Java solution is to get a Logger at the top of each file, and then each time you need to access this object.

Using static Log.debug would require the tag be processed every time, so the correct Handler set was used. Thus, this would be less efficient than having a Log object ready to go.

However, if you do not use handlers or distinguish between subsystems, then static functions will be a reasonable shortcut if the library used is suitable for your needs.

+2
source share

All Articles