由于Logger是一个非常独立的体系统,他必须运行在系统之外,也就是说确保Logger无联连运行,能在程序出错时忠实的记录错误信!帮助用户发现并处理错误!
Logger使用了FSO来进行日志文件操作,由于Log的特殊性和独立性,所以在这里,我不是把Fso操作单独归为一类,而是把他做为了Logger的一部分写了进来的
在调用时,只在要根目录下建立Logs文件夹,在程序中Logger.info(...),Logger.error(...)等就可以正常使用了
在这个程序时有一个小小的BUG,就是在做的时候偷了一下懒,在日志输出时月份没有处理,所以会显示为上一月,呵呵!
由于Logger不存在记录自身发生的信息及错误,所以在Logger出错时,只能以抛出异常,然后结束程序,这一点上不知道各位兄弟姐妹们有没有什么好的意见或建议!希望可以借鉴一下!
<%
/**/ /*
**********************************************************************;
文 件: Logger.asp
作 者: hnyashiro
/**/ /*
**********************************************************************;
文 件: Logger.asp
作 者: hnyashiro
时 间: 2007年7月23日
类说明:在服务端实现程序的日志功能!可以在任意文件,任何位置调用
注:需要在调用文件顶声明字符集,以避免乱码导致程序错误[CODEPAGE="65001"]
**********************************************************************
*/
var Logger = ... {
类说明:在服务端实现程序的日志功能!可以在任意文件,任何位置调用
注:需要在调用文件顶声明字符集,以避免乱码导致程序错误[CODEPAGE="65001"]
**********************************************************************
*/
var Logger = ... {
Configuration:...{
Folder:"/Logs/",
FilePrefix:"LOG",
FilePostfix:"log",
FsoCLSID:"Scripting.FileSystemObject",
Format:"[$TYPE$][$IPADDRESS$] $INFORMATION$ $TIME$",
INFO:true,
WARN:true,
ERROR:true,
FATAL:true
},
getDateFormat:function()...{
var d = new Date();
var yy = d.getFullYear();
var mm = d.getMonth()+1;
var dd = d.getDate();
return yy+"-"+mm+"-"+dd;
},
getRealLogFilePath:function(bFlag)...{
try...{
var p = Logger.Configuration.Folder+Logger.Configuration.FilePrefix+"-"+Logger.getDateFormat()+"."+Logger.Configuration.FilePostfix;
if(bFlag==true)...{
return Server.MapPath(p);
}else...{
return p;
}
}catch(IOException)...{
//Response.Write("Logger.getRealLogFilePath:日志配置信息读取错误!");
throw IOException;
}
},
setLogPath:function(strPath)...{
Logger.Configuration.Folder = strPath || Logger.Configuration.Folder;
},
getLogPath:function()...{
return Logger.Configuration.Folder;
},
setLogFormat:function(strPattern)...{
Logger.Configuration.Format = strPattern || Logger.Configuration.Format;
},
getLogFormat:function()...{
return Logger.Configuration.Format;
},
/**//*--检查文件是否存在--*/
existsFile:function(filePath)...{
try...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var Flag = fso.FileExists(filePath);
delete fso;
return Flag;
}catch(IOException)...{
//Response.Write("Logger.existsFile:查询日志目录失败!");
throw IOException;
}
},
//--保存文件--;
saveFile:function(sContent,filePath)...{
try...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var f1 = fso.openTextFile(filePath,2,true);
f1.WriteLine(sContent);
f1.close();
delete f1;
delete fso;
}catch(IOException)...{
//Response.Write("Logger.saveFile:创建日志文件失败!");
throw IOException;
}
},
/**//*--追加文件内容--*/
appendFile:function(sContent,filePath)...{
try...{
if(Logger.existsFile(filePath)==true)...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var f1 = fso.openTextFile(filePath,8);
f1.WriteLine(sContent);
f1.close();
delete f1;
delete fso;
}else...{
Logger.saveFile.apply(this,arguments);
}
}catch(IOException)...{
//Response.Write("Logger.appendFile:日志文件追加失败!");
throw IOException;
}
},
/**//*readLogFile:function(){
}*/
/**//*=========================================================================*/
info:function(infostr)...{
try...{
if(Logger.Configuration.INFO===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","INFO");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
warn:function(infostr)...{
try...{
if(Logger.Configuration.WARN===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","WARN");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
error:function(infostr)...{
try...{
if(Logger.Configuration.ERROR===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","ERROR");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
fatal:function(infostr)...{
try...{
if(Logger.Configuration.FATAL===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","FATAL");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.fatal:日志信息写入失败!");
throw IOException;
}
}
}
%>
Folder:"/Logs/",
FilePrefix:"LOG",
FilePostfix:"log",
FsoCLSID:"Scripting.FileSystemObject",
Format:"[$TYPE$][$IPADDRESS$] $INFORMATION$ $TIME$",
INFO:true,
WARN:true,
ERROR:true,
FATAL:true
},
getDateFormat:function()...{
var d = new Date();
var yy = d.getFullYear();
var mm = d.getMonth()+1;
var dd = d.getDate();
return yy+"-"+mm+"-"+dd;
},
getRealLogFilePath:function(bFlag)...{
try...{
var p = Logger.Configuration.Folder+Logger.Configuration.FilePrefix+"-"+Logger.getDateFormat()+"."+Logger.Configuration.FilePostfix;
if(bFlag==true)...{
return Server.MapPath(p);
}else...{
return p;
}
}catch(IOException)...{
//Response.Write("Logger.getRealLogFilePath:日志配置信息读取错误!");
throw IOException;
}
},
setLogPath:function(strPath)...{
Logger.Configuration.Folder = strPath || Logger.Configuration.Folder;
},
getLogPath:function()...{
return Logger.Configuration.Folder;
},
setLogFormat:function(strPattern)...{
Logger.Configuration.Format = strPattern || Logger.Configuration.Format;
},
getLogFormat:function()...{
return Logger.Configuration.Format;
},
/**//*--检查文件是否存在--*/
existsFile:function(filePath)...{
try...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var Flag = fso.FileExists(filePath);
delete fso;
return Flag;
}catch(IOException)...{
//Response.Write("Logger.existsFile:查询日志目录失败!");
throw IOException;
}
},
//--保存文件--;
saveFile:function(sContent,filePath)...{
try...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var f1 = fso.openTextFile(filePath,2,true);
f1.WriteLine(sContent);
f1.close();
delete f1;
delete fso;
}catch(IOException)...{
//Response.Write("Logger.saveFile:创建日志文件失败!");
throw IOException;
}
},
/**//*--追加文件内容--*/
appendFile:function(sContent,filePath)...{
try...{
if(Logger.existsFile(filePath)==true)...{
var fso = Server.CreateObject(Logger.Configuration.FsoCLSID);
var f1 = fso.openTextFile(filePath,8);
f1.WriteLine(sContent);
f1.close();
delete f1;
delete fso;
}else...{
Logger.saveFile.apply(this,arguments);
}
}catch(IOException)...{
//Response.Write("Logger.appendFile:日志文件追加失败!");
throw IOException;
}
},
/**//*readLogFile:function(){
}*/
/**//*=========================================================================*/
info:function(infostr)...{
try...{
if(Logger.Configuration.INFO===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","INFO");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
warn:function(infostr)...{
try...{
if(Logger.Configuration.WARN===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","WARN");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
error:function(infostr)...{
try...{
if(Logger.Configuration.ERROR===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","ERROR");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.info:日志信息写入失败!");
throw IOException;
}
},
fatal:function(infostr)...{
try...{
if(Logger.Configuration.FATAL===true)...{
var str = Logger.Configuration.Format;
str = str.replace("$TYPE$","FATAL");
str = str.replace("$TIME$",Date().toString());
str = str.replace("$IPADDRESS$",Request.ServerVariables("REMOTE_ADDR"));
str = str.replace("$INFORMATION$",infostr);
Logger.appendFile(str,Logger.getRealLogFilePath(true));
}
}catch(IOException)...{
//Response.Write("Logger.fatal:日志信息写入失败!");
throw IOException;
}
}
}
%>