Home » Java » Why do you want to use SLF4J instead of Log4J

Why do you want to use SLF4J instead of Log4J

original address: http://www.importnew.com/7450.html

Every Java programmer knows the log for any Java application, especially the server program is critical, and many programmers are already familiar with various log database such as java.util.logging, Apache log4j logback., but if you don't know SLF4J (Simple logging facade for Java) if it is time in your project to study the use of SLF4J.

In this article, we will learn why the use of SLF4J than log4j or java.util.logging to be excellent. Since the 10 log technique the last time I wrote Java programmers have a long time, I don't remember me write on the log all.

Anyway, let's go back to this topic, SLF4J is different from the other log library, and the other is very different.SLF4J (Simple logging Facade for Java) is not really a log, but a layer of abstraction (
Heap memory) and most of the time they are not needed, such as when your application in a production environment to ERROR level operation when using a String in the DEBUG statement is not to be needed. By using SLF4J, you can set the delay string at runtime, which means that only need the String object are set up. And if you have to use log4j, then you have to use the debug statement to this workaround in if very familiar with, but SLF4J is better than this placeholder with much.

This is your solution in Log4j, but this is certainly not interesting and reduces code readability because of the addition of unnecessary tedious repetitive code (boiler-plate, code):

if (logger.isDebugEnabled ()) {
Logger.debug ("Processing trade with id:" + ID + "symbol:" + symbol);
on the other hand, if you use SLF4J, you can get the results in a very concise format, as shown below:

logger.debug ("Processing trade with id: and: symbol {} {}", ID, symbol);

The SLF4J, we don't need a connection string without causing the string temporarily do not need consumption. Instead, we are in a placeholder to transfer the actual value of the parameters and template format to write the log information. You may think I have to do is a parameter? Well, then you can choose to log method using variable parameters or transfer to the version of the Object array. This is a log method quite a convenient and efficient method. Remember, before the final string production log information, this method will check a particular log level is not opened, which not only reduces the memory consumption and reduced in advance CPU to handle the connection string command time. Here is the use of SLF4J log method code, from slf4j-log4j12-1.6.1.jar Log4j Log4jLoggerAdapter.
public, void, debug (String, format, Object, arg1, Object, arg2) {
If (logger.isDebugEnabled ()) {
FormattingTuple ft = MessageFormatter.format (format, arg1, arg2);
Logger.log (FQCN, Level.DEBUG, ft.getMessage (), ft.getThrowable ());

At the same time, it is also worth knowing that logging is a major factor in the performance of an application, and that only the necessary logging is needed in the production process, which we recommend as.

how to use SLF4J to do Log4J logging

In addition to the above advantages, I think there's a caveat, is to use SLF4J, you not only need to include the SLF4J API jar package, such as slf4j-api-1.6.1.jar, but also related to the Jar package, depending on your log in the background using the library. If you want to use with Log4J SLF4J, Simple Logging Facade for Java, and you need to include the following Jar package in your classpath, depending on which version you use in SLF4J and Log4J. For example:

  • slf4j-api-1.6.1.jar - JAR, for, SLF4J, API
  • log4j-1.2.16.jar - JAR, for, Log4J, APIslf4j-log4j12-1.6.1.jar, - Log4J, SLF4J
, Adapter, for

If you are using Maven to manage your project, you only need to include the SLF4J JAR package, Maven will contain its dependence on relevant package. In order to use Log4J and SLF4J together, you can include the following dependencies in your project pom.xml.



Also, if you are interested in the logging method using the variable parameter Version (variable, argument, version), then import the SLF4J 1.7 version,.


To sum up this, I suggest using SLF4J instead of using Log4j directly. Commons, logging, logback, or java.util.logging are enough,.

  1. in your open source or internal libraries in the use of SLF4J makes it independent of any specific log, which means you do not need to manage multiple log configuration or multiple log library, your client will be very grateful for this.
  2. SLF4J provides a method based on the log placeholder by removing the isDebugEnabled (check. (isInfoEnabled), and so on), improve the readability of.
  3. by log using the SLF4J method, you can delay the construction of log information (Srting) overhead, until you really need, for the memory and CPU are efficient.
  4. as string notes, temporary means less garbage collector (Garbage Collector) need to do a better job, which means that your application for.
  5. better throughput and performance of these benefits is the tip of the iceberg, you will start using SL4 J knows more about the API.
and when you read the code. I strongly recommend that any new Java programmer should use SLF4J as a log instead of using other logs, including Log4J