Advantages of Log4j Logarithmic Wrapper?

I recently inherited some Java code and have to integrate it into the project I'm working on. My project is a service agent that processes and converts XML messages. Looking through the new code, I discovered the following logging class:

import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class MyLogger { private static MyLogger instance = null; protected final static Logger log = Logger.getLogger(MyLogger.class); private MyLogger() { super(); } public static MyLogger getInstance(){ if(instance == null){ instance = new MyLogger(); BasicConfigurator.configure(); log.setLevel(Level.ALL); } return instance; } public void info(String myclass, String msg) { log.info("[" + myclass + "] " + msg); } public void error(String myclass, String msg, Exception ce) { log.error("[" + myclass + "] " + msg, ce); } public void warning(String myclass, String msg) { log.warn("[" + myclass + "] " + msg); } } 

This class basically wraps log4j with a (different) singleton. All the entries in the classes that I need for integration look something like this:

 public class MyClass { private final static MyLogger log = MyLogger.getInstance(); private final static String myclass = MyClass.class.getName(); ... log.info(myclass, "Information message..."); } 

I don’t see the obvious benefit of using an additional class for logging, so I am considering reorganizing this code to remove the MyLogger class and log in as follows:

 import org.apache.log4j.Logger; public class MyClass { private static Logger log = Logger.getLogger(MyClass.class); ... log.info("Information Message..."); } 

This will make the logical mechanism consistent in the design. Before I do this, I would like to know if there are any advantages to wrapping Log4j with a singleton class that might be missing. Thank you

EDIT: Thanks to everyone for the helpful answers - I am recruiting several new ideas from each. He accepted the answer of Nathan Hughes, pointing to the lost functionality, leaving the class intact - I assumed that the biggest drawback for exiting a singleton is simply bloating the code. I will destroy the class.

+8
java singleton log4j wrapper
source share
4 answers

Get rid of it. Using this monster means that all records that pass through it will be listed with the same log (MyLogger) and method (therefore, the arguments of its methods include the class of the registered thing). This means that you do not need to add information about each class, method and line number for each log call, but you cannot filter at the log levels for different packages in the same way you could use the typical log4j approach with classes as loggers.

This thing is a piece of trash, and you would be better off without it.

+11
source share

The only advantage I could see was that it would be easy to replace the log4j implementation with another logging implementation or get a log to do something much more customizable, like logging into one of your own databases.

However, I will still reorganize the code to use log4j directly. Or, more likely, in my case, use SLF4J .

+4
source share

One thing that your legacy code does, what log4j does, does things non-threaded security. Since getInstance() does not have a lock, you can potentially pass more than one instance and break the singleton code intent.

You also lose the ability to set logging levels for each class depending on what you do.

+3
source share

The only drawback I can say is this declaration:

 protected final static Logger log = Logger.getLogger(MyLogger.class); 

The registrar, in fact, is connected to the MyLogger object, and all registration information, errors / warnings, etc. will be tied to MyLogger . You do not know which object added the logging information, nothing.

The only advantage I see in this singleton is this:

  • One set-up: you never have to worry about declaring static final Logger implementations all the time.
  • You do not need to worry about what type of registrar you are using. You can only change the type of Logger in this Singleton class. Any further changes to the registration are made only in Singleton.

I also saw this in my company, but I do not offer this type of customization. Rather, use SLF4J or the Java Logging Framework.

+2
source share

All Articles