本篇內(nèi)容主要講解“l(fā)og4j的操作方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“l(fā)og4j的操作方法”吧!
1 log4j的配置文件
2 log4j的調(diào)用
[@more@]
1 log4j.properties
### 設(shè)置org.zblog域?qū)?yīng)的級別DEBUG、INFO、WARN、ERROR和FATAL和輸出地A1,A2,A3 ##
log4j.category.org.consoleLog=ERROR,A1
log4j.category.org.fileLog=WARN,A3
##指定采用文本方式輸出
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] %m%n %c
##指定采用文本方式輸出
log4j.appender.A3=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Threshold=INFO
log4j.appender.A3.File=./log/INFORMATION.log
### 文件大小##
log4j.appender.A3.MaxFileSize=1024KB
### 文件個(gè)數(shù)##
log4j.appender.A3.MaxBackupIndex=3
log4j.appender.A3.Append=true
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] %m%n %c
2 log4j的調(diào)用
public class Log4jPrinter {
// 文件輸出的log
private Logger fileLog4j = null;
// 在控制臺輸出的log
private Logger consoleLog4j = null;
private static Log4jPrinter instance = null;
private Log4jPrinter(){
PropertyConfigurator.configure("./log4j.properties");
consoleLog4j = Logger.getLogger("org.consoleLog");
fileLog4j = Logger.getLogger("org.fileLog");
}
public static synchronized Log4jPrinter getLog4jPrinter(){
if(instance == null){
instance = new Log4jPrinter();
}
return instance;
}
public void debug(String logInfo,Throwable ex){
fileLog4j.debug(logInfo,ex);
}
public void info(String logInfo,Throwable ex){
fileLog4j.info(logInfo,ex);
}
public void error(String logInfo,Throwable ex){
consoleLog4j.error(logInfo,ex);
fileLog4j.error(logInfo,ex);
}
public void warn(String logInfo,Throwable ex){
fileLog4j.warn(logInfo,ex);
}
public void debug(Throwable ex){
fileLog4j.debug(ex);
}
public void info(Throwable ex){
fileLog4j.info(ex);
}
public void error(Throwable ex){
fileLog4j.error(ex);
consoleLog4j.error(ex);
}
public void warn(Throwable ex){
fileLog4j.warn(ex);
}
public static void main(String[] args){
Log4jPrinter log4j = Log4jPrinter.getLog4jPrinter();
try{
log4j.info("test....",null);
Integer.parseInt("sss");
}catch(Exception ex){
log4j.error(ex.getMessage(),ex);
}
}
}
到此,相信大家對“l(fā)og4j的操作方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:log4j的操作方法-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://sd-ha.com/article42/djpcec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、Google、商城網(wǎng)站、用戶體驗(yàn)、微信小程序、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容