What is the problem with Apache Commons Logging Runtime Detection Algorithm

Dave Sier (SpringSource) writes on his blog:

Unfortunately, the worst thing about maintaining public records and what made it unpopular with new tools is also a runtime detection algorithm.

Why? What is the problem with its runtime detection algorithm? Performance?

+41
java spring logging apache-commons-logging
Jul 11 '10 at 11:33
source share
4 answers

Why? What is the problem with its runtime detection algorithm? Performance?

No, this is not performance, the pain of loading classes . The JCL discovery process relies on hackers of the loader class to find the logging structure at runtime, but this mechanism leads to numerous problems, including unexpected behavior, it is difficult to debug problems with loading classes, which leads to increased complexity. This is well received by Ceki (author of Log4J, SLF4J, and Logback) in Think Again before adopting a public API (which also mentions memory leak problems observed with JCL).

And that is why SLF4J, which uses static bindings, was created.

Ceki, the author of SLF4J, may seem that his articles are biased, but, believe me, they are not, and he provides many links (evidence) to prove his point.

Summarizing:

  • Yes, JCL is known to be broken, it is better to stay away from it.
  • If you want to use the facade of the magazine (not all projects need this), use SLF4J.
  • SLF4J provides a JCL-to-SLF4J bridge for frameworks still using JCL, such as Spring :(
  • I find Logback, the predictor of Log4J, is an excellent version of logging.
  • Logback initially implements the SLF4J API. This means that if you use Logback, you are actually using the SLF4J API.

see also

+71
Jul 11 '10 at 16:05
source share

Commons logging is a lightweight slave facade that sits on top of the heavy weight logging API: log4j , java.util.logging, or another supported logging API.

a discovery algorithm is what you use for logging communities to determine which logging API you use at run time so that it can route log calls through its API to the underlying logging API. The advantage of this is that if you want to create a library that does registration, you do not want to bind users of your library to any particular heavy weight registration system. Subscribers of your code can configure logging through log4j, java.util.logging, etc., And commons logging will be redirected to this API at runtime.

Common issues for registering in communities:

  • Although you are not using it, you can depend on the library, so you will have to include it in your classpath.
  • Runs a discovery algorithm for each classloader that you want to execute, and See the article for a convincing argument against using commons-logging.
+11
Jul 11 2018-10-11T00:
source share

I can’t talk about the “treacherous unpopular” aspect, I can only speak for myself:

Commons Logging is a facade on top of any of your "real" logging structures: Log4j, Logback, or something else.

The idea behind the logging façade is that your application gets the flexibility to decide at run time which logical implementation it wants to work with. The facades are smart enough to find runtime implementations.

My old Java applications use Log4j directly. It works well, I do not see the need to change them. My new Java applications are likely to use Logback. I think the ability to dynamically select a logging structure is something that none of my applications will ever need. Of course, other people's mileage may vary.




EDIT: Looks like I was mistaken in the rationale for keeping Commons magazine. The links given by @Pascal Thivent, especially the first one, explain this much better.

+2
Jul 11 2018-10-11T00:
source share

Commons logging contains logic for determining at runtime whether to use log4j or java.util.logging. *.

This code was seriously violated, essentially only with JUL.

Based on experience with this, slf4j was written that uses static binding (or is used for, I'm not sure about version 1.6) to select a suitable structure to use log4j, JUL or log4j fork logback (and more) and it includes a bridge allowing Commons' existing logging code uses slf4j transparently.

If you can, go to slf4j.

+1
Jul 11 '10 at 13:16
source share



All Articles