xml
一、實現方法
一個書寫日誌的函數,提供幾個參數,使用者程式調用這個函數就可以實現日誌的記錄。日誌記錄到xml檔案中,記錄檔按日期產生,每天建立立一個記錄檔,檔案名稱為:yyyy_mm_dd.xml,分別用了年月日。而查看日誌也日常簡單,使用者想看哪天的日誌,只要直接調用該xml檔案即可。因為xml檔案已經預設了一個xsl檔案來格式化輸出。
二、書寫日誌的方法
'記錄日誌的程式
'作者:塞北的雪
'日期:2004.11.20
'username :使用者資訊(標示進行該操作的人員)
'operate :操作(標示使用者進行了什麼操作)
'userip :使用者IP(標示使用者用於登入系統的電腦的IP地址)
'opdate :使用者操作發生的日期
'日誌寫入一個xml檔案,第一次寫入時如果xml檔案不存在,則建立。
'傳回值:1 表示開啟記錄檔時出錯
'傳回值:9 表示正確完成寫入記錄檔
function WriteSysLog(sys_userid,sys_username,operate)
dim op_username
if trim(sys_userid)="" and trim(sys_username)="" then
op_username="匿名"
else
op_username = sys_userid & "/" & sys_username
end if
xmlPath="/" & getRoot() & "/log/SysLog/"
xmlFile=replace(cstr(ConvertDate(date())),"-","_") & ".xml"
RootNode="syslog" '記錄檔根節點名字
LogFile=server.mappath(xmlPath & xmlFile) '記錄檔路徑
set fso=server.CreateObject("scripting.filesystemobject")
'如果記錄檔不存在,就建立一個,並寫入頭資訊和根資訊
if not fso.FileExists(LogFile) then
fso.CreateTextFile LogFile
set fff=fso.GetFile(LogFile)
set mmm=fff.openastextstream(2)
mmm.write "<?xml version=""1.0"" encoding=""gb2312"" ?>" & vbcrlf & "<?xml-stylesheet type='text/xsl' href='../logInfo.xsl'?>" & vbcrlf & "<" & rootnode & "></" & rootnode & ">"
set mmm=nothing
set fff=nothing
end if
set fso=nothing
Set xd = Server.CreateObject("msxml2.domdocument")
xd.async = false
xd.load(LogFile)
if xd.parseError.errorcode<>0 then
WriteSysLog=1 '開啟記錄檔出錯
exit function
end if
'建立新節點資訊
set et=xd.documentElement
set cnode=xd.createElement("log")
et.appendchild(cnode)
set node2=xd.createElement("username")
node2.text=op_username
cnode.appendchild(node2)
set node2=xd.createElement("operate")
node2.text=operate
cnode.appendchild(node2)
set node2=xd.createElement("userip")
node2.text=Request.ServerVariables("Remote_Addr")
cnode.appendchild(node2)
set node2=xd.createElement("opdate")
node2.text=cstr(now())
cnode.appendchild(node2)
xd.save LogFile '寫入記錄檔
set cnode=nothing
set node2=nothing
set xd=nothing
writeSysLog=9 '說明正常寫入了日誌資訊
end function
'獲得當前虛擬目錄的名字
function getRoot()
url=Request.ServerVariables("URL")
url=right(url,len(url)-1)
getRoot= mid(url,1,instr(url,"/")-1)
end function
'將一個一位的數字前面加零
function FillZero(str)
ttt=str
if len(str)=1 then
ttt="0" & str
end if
FillZero=ttt
end function
'轉化日期,將 一位補上零 2003-1-2 --> 2003-01-02
function ConvertDate(tDate)
ttt=tDate
if isdate(tDate) then
ttt=year(tDate) & "-" & FillZero(month(tDate)) & "-" & FillZero(day(tDate))
end if
ConvertDate=ttt
end function
三、使用者格式化的xsl檔案:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://www.cccar.com.cn/"
exclude-result-prefixes="msxsl user">
<!-- localized strings -->
<xsl:variable name='ColumnHeader_UserName'>使用者</xsl:variable>
<xsl:variable name='ColumnHeader_Time'>時間</xsl:variable>
<xsl:variable name='ColumnHeader_Operate'>操作</xsl:variable>
<xsl:variable name='ColumnHeader_Address'>IP地址</xsl:variable>
<!-- variables -->
<xsl:variable name='TableStyle'>background-color:#DAE6D8;font-family:Simsun, Verdana; font-size:75%; text-align:left; vertical-align:top</xsl:variable>
<xsl:variable name='HeaderStyle'>background:a0b0a8;color:#000000;border-bottom:1 solid black;border-top:1 solid black</xsl:variable>
<msxsl:script language="javascript" implements-prefix="user">
function xmlDateTime(nodelist) {
return Date.parse(nodelist.replace(/-/g,"/"));
}
</msxsl:script>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="syslog">
<html>
<head>
<title>
日誌查看
</title>
</head>
<body style='margin:10;background-color:#DAE6D8'>
<div align="center">
<table style="{$TableStyle}" width="100%" align="center" cellspacing='0'>
<thead>
<tr height="23">
<th width="15%" style="{$HeaderStyle}">
<xsl:value-of select="$ColumnHeader_UserName"/>
</th>
<th width="20%" style="{$HeaderStyle}">
<xsl:value-of select="$ColumnHeader_Time"/>
</th>
<th width="50%" style="{$HeaderStyle}">
<xsl:value-of select="$ColumnHeader_Operate"/>
</th>
<th width="15%" style="{$HeaderStyle}">
<xsl:value-of select="$ColumnHeader_Address"/>
</th>
</tr>
</thead>
<tbody style='vertical-align:top'>
<tr ><td colspan="4" height="5"></td></tr>
<xsl:for-each select="log">
<xsl:sort order='ascending' select="user:xmlDateTime(string(opdate))" data-type="number"/>
<tr height="23">
<td valign="bottom"><xsl:value-of select="username"/></td>
<td valign="bottom" ><xsl:value-of select="opdate"/></td>
<td valign="bottom" ><xsl:value-of select="operate"/></td>
<td valign="bottom" ><xsl:value-of select="userip"/></td>
</tr>
<tr bgcolor="#999999"><td colspan="4" height="1"></td></tr>
</xsl:for-each>
<tr><td colspan="4" align="right">合計:<xsl:value-of select="count(log)"/> 條 </td></tr>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>