SLF4J: Bridges vs. Migrator JARs

SLF4J (1.7.5, but really any modern version) comes with several "more" (migratory) JARs:

  • jcl-over-slf4j-1.7.5.jar
  • log4j-over-slf4j-1.7.5.jar

... as well as the "to" (bridge) JAR:

  • jul-to-slf4j-1.7.5.jar

According to their docs , migrator:

... ease of [s] switching to SLF4J from [JCL / log4j]. This jar file is intended to be a replacement for [JCL / log4j]. It implements the open API [ir], but uses SLF4J at the bottom, hence the name "over" SLF4J.

While the JUL bridge:

redirects all incoming jul entries to the SLF4j API.

  • I use jcl-over-slf4j-1.7.5.jar when I have code that is being registered using JCL, but I want it to use SLF4J? Or something else?
  • When will I use jul-to-slf4j-1.7.5.jar ? How is the word "to" used here differently than "over"?
  • Why is there no JAR for JUL? Why is there no "for" JAR for JCL and log4j?
+4
source share
2 answers

First of all, these banks are intended for situations when your project has dependencies out of your control, and these dependencies use JUL ( java.util.logging ), JCL ( Jakarta Commons Logging ) or log4j , and you want to route all registration operations through slf4j-api Think of it as dynamically replacing all calls with those old logging apis with slf4j-api equivalents.

Each of these 3 cans does the same for its respective journal structure. The difference in name (over vs to) is related to how this translation is performed.

Based on the foregoing, here are the answers to your questions:

  • If the code is under your control, you can also replace all JCL calls with the correct slf4j-api calls (just like with any other obsolete structure). If the source code is out of control or you cannot replace it, you can include jcl-over-slf4j-1.7.5.jar in your classpath and exclude commons-logging.jar . This is because jcl-over-slf4j-1.7.5.jar contains the same classes (or a subset of them) commons-logging.jar rewritten to send all logging activity to slf4j-api . Therefore, above the name.

  • jul-to-slf4j-1.7.5.jar works its magic a little differently - hence the name. JUL uses handlers. A handler is any class that extends java.util.logging.Handler and is designed to process (guess) registration messages (or entries in JUL terminology). Therefore, in this case, in order to route all JUL to slf4j-api we just need to make sure that we register only one such handler - SLF4JBridgeHandler (which is the only class contained in jul-to-slf4j-1.7.5.jar ). Configuration options for this can be found here .

  • Now the difference between is over and should be obvious. More sophisticated jars work by replacing the same classes of source jars with those that route all entries to slf4j-api . JUL to jar does not need to do the same class JUL due to the way JUL works with handlers (and you only need to configure one handler that will route all entries in slf4j-api ).

For older notes, check out the excellent slf4j legacy documentation, and be sure to check out the larger image (also related to the main legacy article).

Hope this helps.

+11
source

I use jcl-over-slf4j-1.7.5.jar when I have code that is being registered using JCL, but I want it to use SLF4J? Or something else?

You must use jcl-over-slf4j.jar if you want to port your application using JCL to SLF4J. Both JCL and SLF4J serve as a simple facade or abstraction for various registration frameworks such as Log4J. SLF4J is considered a better solution than JCL because it solves various classloading problems associated with the JCL discovery mechanism. Please note that the release of JCL version 1.1 contains several changes that are designed to fix problems with loading classes.
There are several options for using JCL with SLF4J:

  • Solve Commons Logging Class Loader Issues
  • You realized that SLF4J is a better logging facade than JCL
  • Would you like to use logback , which initially implements the SLF4J API

While you can completely replace the use of JCL SLF4J by replacing JCL API calls with SLF4J API calls, the JCL implementation of SLF4J will allow you to gradually switch to SLF4J, especially if some of your programs depend on your application to use JCL (for example, Spring Framework ) .

When will I use jul-to-slf4j-1.7.5.jar? How is the word "here" used here differently than "above"?

You should use jul-to-slf4j-1.7.5.jar if your application uses java.util.logging (JUL) as its logging structure, and you would like to replace it with SLF4J as a logging facade and have freedom of choice between others logging implementations. JUL provides JavaTM 2 platform logging classes and interfaces. Unlike jcl-over-slf4j , which overrides the JCL, the jul-to-slf4j does not override java.util.logging because the packages are under the java namespace. * Cannot be replaced. Instead, jul-to-slf4j includes a java.util.logging (jul) handler, namely SLF4JBridgeHandler , which routes all incoming jul to API SLF4j entries. Check out SLF4J's performance note for this solution.

Why is there no JAR for JUL? Why is there no "for" JAR for JCL and log4j?

As described in the previous section, packages under the java namespace. * cannot be replaced, and therefore there is no way to create a module "from" the bridge, but rather use a solution that routes the JUL API to SLF4J. For other bridge modules, this overhead is simply not needed.

0
source

Source: https://habr.com/ru/post/1499846/


All Articles