1. 程式人生 > >Web客戶端寫日誌javascript元件

Web客戶端寫日誌javascript元件

在java web開發中,在服務端我們比較常用Log4j來進行寫日誌的控制,而在客戶卻多用alert來將中間的結果show出來,十分不方便,為此,我整理了一個在客戶端通過javascript來進行寫日誌指令碼,其使用方式與Log4j相似.
1.javascript寫日誌元件指令碼,可將其放在一個js檔案中,如:clientlog.js,其程式碼如下:
/**
 * write log at web client ,use javascript
 * @author Felix liang
 * @email:[email protected]
 */
function Logger(FileName,logFile){
if(logFile!=null)
 this.logFileName=logFile;
else
 this.logFileName="c://clien_log.txt"; //default log file
this.Prior=0;//0:ALL,1:DEBUG,2:INFO,3:WARN,4:ERROR,5:FATAL,6:OFF
this.FileName=FileName;
this.debug=writeDebug;
this.info=writeInfo;
this.warn=writeWarn;
this.error=writeError;
this.fatal=writeFatal;
this.writeLogFuc=writeLog;
this.isDebugEnabled =function(){return checkDebug(this.Prior,1);}
this.isInfoEnabled  =function(){return checkDebug(this.Prior,2);} 
this.isWarnEnabled  =function(){return checkDebug(this.Prior,3);} 
this.isErrorEnabled =function(){return checkDebug(this.Prior,4);}
this.isFatalEnabled =function(){return checkDebug(this.Prior,5);} 

function checkDebug(DPri,pri){
if(pri>=DPri)
 return true; 
else
 return false;
}

function writeDebug(info,ex){
if(!this.isDebugEnabled())
 return;
this.writeLogFuc("Debug",info,ex);
}

function writeInfo(info,ex){
if(!this.isDebugInfo())
 return;
this.writeLogFuc("Info",info,ex);
}

function writeWarn(info,ex){
if(!this.isDebugWarn())
 return;
this.writeLogFuc("Warn",info,ex);
}

function writeError(info,ex){
if(!this.isErrorEnabled())
 return; 
this.writeLogFuc("Error",info,ex);
}

function writeFatal(info,ex){
if(!this.isFatalEnabled())
 return; 
this.writeLogFuc("Fatal",info,ex);
}

function writeLog(prids,info,ex){

try{
if(this.fso==null)
 this.fso=new ActiveXObject("Scripting.FileSystemObject");
}catch(ex2){
 alert(ex2);
 }
if(!this.fso.FileExists(this.logFileName)){
this.logFile=a;
var a = this.fso.CreateTextFile(this.logFileName);
a.Close(); 
}
var a = this.fso.OpenTextFile(this.logFileName,8);
var s="";
d = new Date();
s += d.getYear() +"-";
s += (d.getMonth() + 1) +"-";           
s += d.getDate() + " ";                  
var c = ":";
s += d.getHours() + c;
s += d.getMinutes() + c;
s += d.getSeconds() + c;
s += d.getMilliseconds();

a.WriteLine(s+" "+prids+" ("+this.FileName+") - ["+(info==null?"":info)+"]");
if(ex!=null)
 a.WriteLine("- "+ex+"");

a.Close(); 

}

function errortrap(msg,url,line){
    glog = new Logger("Globe");
    if(glog.isErrorEnabled())
     glog.error("URL:"+url+"; Line:"+line+"; Msg:"+msg);
    return true;
}

window.onerror=errortrap;


2.對Client Logger 的使用
引入clientlog.js:<script type="text/javascript" language="JavaScript" charset="GBK" src="clientlog.js"></script>
在需要寫log的檔案中加入如下程式碼:
<script language="javascript">
//建立客戶段日誌物件
var log=new Logger("test.html");//預設日誌到c:/client_log.txt,如果設定第二個引數將會到指定的路徑
</scirpt>
...
在程式碼中需要新增日誌的的地方可進行寫日誌:
   var before_amt=10;
   if(log.isDebugEnabled()){
    log.debug("此前修改中新增費:_before_amt:"+before_amt);
   } 
   ....
3.執行完之後檢視日誌檔案即可看到執行過程中的中間結果
4.在此拋磚引玉,有何高見,歡迎mail me:[email protected]