1,導入 slf4j-api-1.5.6 和 slf4j-log4j12-1.5.6兩個jar包java
2,測試實例api
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SLF4J { final Logger logger = LoggerFactory.getLogger(SLF4J.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) { SLF4J slf4j=new SLF4J(); slf4j.setTemperature(35); slf4j.setTemperature(60); } }
3,控制檯打印信息測試
2015-07-24 11:11:00[logger.SLF4J-16][main][0] - Today isFri Jul 24 11:10:59 CST 2015,Temperature set to null.Old temperature was 35. 2015-07-24 11:11:00[logger.SLF4J-16][main][10] -Today isFri Jul 24 11:11:00 CST 2015,Temperature set to 35.Old temperature was 60. 2015-07-24 11:11:00[logger.SLF4J-18][main][10] - Temperature(60) has risen above 50 degrees.