commons-logging-Log和Log4j的使用
阿新 • • 發佈:2019-01-05
先上參考----http://www.cnblogs.com/likwo/archive/2010/08/31/1813595.html 感謝~
分4部分:1.Log的單獨是用。
2.log4j.properties;
3.Log4j的單獨使用
4.Log+Log4j的組合使用
1、Log單獨使用:載入jar包--編寫程式碼(此處,沒有設定log.properties)
控制檯輸出結果:import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Log4jTest { static Log logger = LogFactory.getLog(Log4jTest.class); public static void main(String[] args){ logger.info("info log"); logger.debug("debug info"); logger.error("err log"); logger.warn("warn log"); } }
四月 15, 2015 11:21:16 上午 com.ben.test.Log4jTest main
INFO: info log
四月 15, 2015 11:21:16 上午 com.ben.test.Log4jTest main
SEVERE: err log
四月 15, 2015 11:21:16 上午 com.ben.test.Log4jTest main
WARNING: warn log
2、log4j.properties:Log4j單獨使用:——>l引入og4j的jar包
向上面那樣直接建類是不能正確使用的,控制檯輸出結果:
必須有log4j.properties配置檔案log4j:WARN No appenders could be found for logger (com.ben.test.Log4jTest). log4j:WARN Please initialize the log4j system properly.
:
log4j.rootLogger=INFO, ben1, ben2 //首行,最重要的資訊,INFO代表輸出級別,DEBUG<INFO<WARN<ERROR,即所有大於等於INFO級別的會輸出<pre name="code" class="plain"><span style="white-space:pre"> </span>//ben1,ben2分別為自己設定的輸出路徑,在下面中可見ben1.ben2的設定
log4j.appender.ben1=org.apache.log4j.ConsoleAppender//設定ben1輸出路徑為控制檯log4j.appender.ben1.layout=org.apache.log4j.PatternLayout//輸出格式log4j.appender.ben1.layout.ConversionPattern=%t %d %p [%c.%M] - %m%n //輸出語句格式log4j.appender.ben2=org.apache.log4j.RollingFileAppender//ben2的輸出路徑為filelog4j.appender.ben2.File=log/ben.log//輸出到log路徑下的ben.log檔案下log4j.appender.ben2.MaxFileSize=20480KB//file容量# Keep 20 backup files.log4j.appender.ben2.MaxBackupIndex=100# Pattern to output: date priority [category] - messagelog4j.appender.ben2.layout=org.apache.log4j.PatternLayoutlog4j.appender.ben2.layout.ConversionPattern=%d %p [%c.%M] - %m%n
按上面的語句輸出格式:輸出為具體格式講解參考:http://blog.csdn.net/guoquanyou/article/details/56896522015-04-15 12:27:59,008 INFO [com.ben.test.Log4jTest.main] - info log 2015-04-15 12:27:59,008 DEBUG [com.ben.test.Log4jTest.main] - debug info 2015-04-15 12:27:59,008 ERROR [com.ben.test.Log4jTest.main] - err log 2015-04-15 12:27:59,008 WARN [com.ben.test.Log4jTest.main] - warn log
3.Log4j的單獨使用測試類:
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jTest {
static Logger logger = Logger.getLogger(Log4jTest.class);
static{
PropertyConfigurator.configure("conf/log4j.properties");
}
public static void main(String[] args){
logger.info("info log");
logger.debug("debug info");
logger.error("err log");
logger.warn("warn log");
}
}
4.log+log4j:-->匯入log和log4j的jar包。
像1,那樣直接使用,會出現2的錯誤
必須像3一樣匯入log4j的設定檔案:
public class Log4jTest {
static Log logger= LogFactory.getLog(Log4jTest.class);
// static Logger logger = Logger.getLogger(Log4jTest.class);
// static{
// PropertyConfigurator.configure("conf/log4j.properties");
// }
public static void main(String[] args){
logger.info("info log");
logger.debug("debug info");
logger.error("err log");
logger.warn("warn log");
}
}
這樣就會正確輸出;
logging Log建立會自動查詢相應的工具,比如log4j,如果查詢到就會是用log4j作為實現類去記錄日誌,如果沒有查到
則會使用基本的SimpleLog去記錄日誌。注意,使用log4j必須使用相關的配置檔案log4j.properties:::::
PropertyConfigurator.configure("conf/log4j.properties");