How are Log4j, commons-logging, JDK-Logging and SLF4J related to each other?

Are they alternatives, dependencies, APIs, or implementations of each other? And why do they exist?

+6
source share
4 answers

Ah, logging frameworks in Java. Your question mixes 2 different types of libraries:

  • log4j and JDK logging are libraries for processing logging
  • Commons Logging and SLF4J are journaling facades: you still need a real logging implementation (e.g. log4j)

If you are writing a library that will be used on some other system, then you should use the facade of the journal, because you do not know what kind of logging structure they will use. In this case, use SLF4J (Commons Logging is older and has some problems with class loaders).

If you manage the entire application and can determine which logging structure to use, you can choose your own preferences. My preferred solutions (in order of preference):

  • Logback
  • log4j
  • JDK record (in my opinion, the case is "not invented here" SUN)
+5
source

I also studied this recently. I have been using Log4J in Commons Logging for many years and have recently switched to SLF4J.

Log4j

Log4j is the basis for the actual recording / distribution of the log. It is extremely flexible: you can direct it to send log messages to files, syslog, remote monitoring, etc. You can also set up multiple registrars, logging categories, include context in entries, etc. This is one of the most popular registration systems.

JDK Record

A built-in JDK log (which I never used to be honest) was added in JDK 1.4.2. From what I collect, it is not very popular because it is not as flexible as Log4j, but I would welcome comments :).

Community Journal and SLF4j

Both of these are facades on top of various registration frameworks that provide a common interface for your application. For example, you can use CL / SLF4J in your application, and they will automatically detect the basic login implementation (Log4J, JDK logging or a built-in logger that simply delegates to System.err.println() ). The advantage is that you or your end user can decide to run the main logical implementation by default, and they greatly simplify your implementation by eliminating many of the complexities of Log4J and JDK logs.

+3
source

Most often you will see them layered.

SLF4J is a purely abstraction layer and by itself is not used to actually output the log, but you use it in your code to register messages.

A typical setup is to use SLF4J to enter your code, and then use log4j as the base “output” layer using the appropriate slf4j-> log4j bridge (a jar that you just include in your class path). There are various bridges for combining magazines from different sources. For example, many application servers (such as tomcat) will use JDK logging to avoid forcing a “non-standard” logging structure in deployed applications. To this end, slf4j has a bridge that will extract all output from the JDK log. So it could be a stack

 JDK-logging <- Your app-server or framworks might log using this | (JDK->Slf4j bridge) | Slf4j <- your application logs using Slf4j | (Slf4j->log4j bridge) | log4j <- log4j is just responsible for outputting to the appenders you configure (file, console etc) 
+1
source

SLF4J is a common API with different back-end (any other logging system). Log4j and commons-logging (CL) are different logging libraries, CL is a fossil. But everyone has a fatal flaw , so the sun invented JDK logging.

As for me, I prefer SLF4J as the most flexible and logback as the backend for it. Logback is the most advanced and has many nice features.

0
source

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


All Articles