The following passage from: Baidu Encyclopedia
Assuming you are developing a class library or embedded component, you should consider using SLF4J, because it is not possible to influence what kind of log system the user chooses. On the other hand, assuming that there is a simple or standalone application, to determine that there is only one log system, then there is no need to use SLF4J. Assuming you're going to sell your log4j products to users who require JDK 1.4 logging, it's definitely not an easy task to face thousands of changes in log4j calls. But assuming that you start with slf4j, then such a conversion would be easy.
Today to test the use of slf4j and its charm.
In general, Java projects are Web projects today, so our pilot projects are tested with Web projects.
1. Create a new Web project Slf4jdemo
2. Referencing the jar package
The note here is slf4j the corresponding log4j version number, the person you can also choose other log system.
Log4j-1.2.12.jar
Servlet2.5-jsp2.1-api.jar
Slf4j-api-1.7.5.jar
Slf4j-log4j12-1.7.5.jar
3. Join the log4j configuration file log4j.properties
Here you need to explain that the default path for log4j itself is under the/web-inf/classes/folder, so don't put it in the wrong place.
#log4j logger fatal=0 error=3 warn=4 info=6 debug=7#log4j.rootlogger = DEBUG, A1log4j.rootlogger = off### Direct Log Messa Ges to stdout # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m% Nlog4j.logger.org =error, a1log4j.logger.net = ERROR, A1log4j.logger.com.qisentech = DEBUG, a1log4j.appender.a1 = Org.apache.log4j.RollingFileAppenderlog4j.appender.a1.File = ${rootpath}/ Webplus.loglog4j.appender.a1.MaxFileSize = 10000kblog4j.appender.a1.maxbackupindex = 10log4j.appender.a1.layout = Org.apache.log4j.PatternLayoutlog4j.appender.a1.layout.ConversionPattern =%d [%t] (%f:%l)%c%-5p-%m%n# Log4j.rootlogger=warn, stdout#log4j.appender.stdout=org.apache.log4j.consoleappender# log4j.appender.stdout.layout=org.apache.log4j.patternlayout#log4j.appender.stdout.layout.conversionpattern=%d% p [%c]-%m%n
Look carefully at the configuration information, you can see a ${rootpath}, select we will initialize this variable.
4. Create a new initialization servlet
/* File name: Com.qisentech.slf4j.demo.WebplusContextStart.java * Date Created: 2013-8-23 11:20:48 * Created by: Qsyang */package com. Qisentech.slf4j.demo;import Javax.servlet.servletexception;import javax.servlet.http.httpservlet;/** * <p> Title: </p> * * <p>description: </p> * * @author Qsyang * @version 1.0 */public Class Webpluscontextstar T extends HttpServlet { @Override public void Init () throws Servletexception { System.setproperty (" Log4j.configuration "," log4j.properties "); String ContextPath = This.getservletcontext (). Getrealpath ("/"); System.setproperty ("RootPath", ContextPath); }}
We set the default parameters for log4j log4j.configuration and our profile variable RootPath.
5. Using SLF4J to print logs in a class
Citation method:
Logger _log = Loggerfactory.getlogger (Slf4jtest.class);
Note that the object reference here is:
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Then look at the printing method:
_log.debug ("Debug log: XXXXX");
Attach all source code:
/* File name: Com.qisentech.slf4j.demo.Slf4jTest.java * Date Created: 2013-8-23 11:02:20 * Created by: Qsyang */package Com.qisentech. Slf4j.demo;import org.slf4j.logger;import org.slf4j.loggerfactory;/** * <p>title: </p> * * <p> Description: </p> * * @author Qsyang * @version 1.0 */public class Slf4jtest { private static Logger _log = Logg Erfactory.getlogger (slf4jtest.class); public static void Testdebug (String info) { System.out.println ("Testdebug"); _log.debug (info); } public static void TestInfo (String info) { System.out.println ("TestInfo"); _log.info (info); } public static void Testwarn (String info) { System.out.println ("Testwarn"); _log.warn (info); } public static void Testerror (String info) { System.out.println ("Testerror"); _log.error (info);} }
5. Do not forget to configure Web. xml
<!--webplus Environment initialization (must start)- <servlet> <servlet-name>webpluscontextstart</ Servlet-name> <servlet-class>com.qisentech.slf4j.demo.WebplusContextStart</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
So the whole thing worked out.
The following will be the source code attached, interested in being able to download.
http://download.csdn.net/detail/yakson/7586371
slf4j-api-1.7.5 Log Print Experiment