1. Introduction
Slf4j is not a specific log solution. It only serves a variety of log systems. According to the official statement, slf4j is a simple facade for the log system, allowing end users to use the desired log system when deploying their applications.
In fact, the core API provided by slf4j is some interfaces and a loggerfactory class. To some extent, slf4j is a bit similar to JDBC, but it is simpler than JDBC. In JDBC, You need to specify the driver. When using slf4j, you do not need to specify the specific log system you intend to use in the code or configuration file. Just like using JDBC without considering a specific database, slf4j provides a unified log recording interface, as long as it is recorded according to the provided method, the final log format, record level, and output mode are implemented through the configuration of the specific log system. Therefore, you can flexibly switch the log system in the application.
2. When to use
If you are developing a class library or embedded component, you should consider using slf4j, because it is impossible to decide which log system the final user chooses. On the other hand, if it is a simple or independent application and only one log system is identified, there is no need to use slf4j. If you want to sell your log4j product to users who require JDK 1.4 logging, it is definitely not easy to handle thousands of changes to log4j calls. However, if slf4j is used at the beginning, this conversion will be very easy.
3. source code and jar packages
Http://www.slf4j.org/download.html
4. Call example
Import org. slf4j. Logger;
Import org. slf4j. loggerfactory;
Public class wombat {
Final logger = loggerfactory. getlogger (Wombat. Class );
Integer T;
Integer oldt;
Public void settemperature (integer temperature ){
Oldt = T;
T = temperature;
Object [] objs = {New java. util. Date (), oldt, t };
Logger.info ("Today is {}, temperature set to {}. old temperature was {}.", objs );
If (temperature. intvalue ()> 50 ){
Logger. Warn ("temperature ({}) has risen above 50 degrees.", t );
}
}
Public static void main (string [] ARGs ){
Wombat wombat = new wombat ();
Wombat. settemperature (10 );
Wombat. settemperature (60 );
}
}
5. Log binding principles
In the application, obtain the logger through the static getlogger () of the loggerfactory class. By viewing the code of this class, we can see that loggerfactory is obtained through the staticloggerbinder. Singleton. getloggerfactory () method, and then logger is obtained through the specific loggerfactory. Org. slf4j. impl. staticloggerbinder is not in the slf4j-api-1. *. *. in the jar package, carefully check each jar package corresponding to the specific log system, and you will find that the corresponding jar package has an org. slf4j. impl. different implementations of staticloggerbinder return the loggerfactory corresponding to the log system. Therefore, the so-called static binding is realized to achieve simple and flexible configuration as long as different jar packages are selected.
6. log system switching
Compile the above program and add the slf4j-api-1.4.1.jar file to classpath.
At runtime, you need to add the slf4j-simple-1.4.1.jar in classpath.
Switch to the log style of jdk14: just remove the slf4j-simple-1.4.1.jar from the classpath while adding the slj4j-jdk14-1.4.1.jar to the classpath
Switch to log4j: remove the slj4j-jdk14-1.4.1.jar, add the slf4j-log4j12-1.4.1.jar, and add log4j. properties to the log4j-1.2.x.jar.
Reprinted from: http://chenming47.iteye.com/blog/923658
Reference: http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2013/0114/205903.html