步骤:
1、 格式化日期,以日期为文件名
2、判断文件是否存在,不存在就创建
3、将日志内容写入txt文本
<%
'/**
' * 写入日志到txt文本
' * sWord 日志内容
' *
Function LogResult(sWord)
txtpath = Server.MapPath("log/"&FormatDate(Now(),2)&".txt")
Set fso = Server.CreateObject("Scripting.FileSystemobject")
' 判断文件是否存在,不存在就创建
if fso.FileExists(txtpath)=false then fso.CreateTextFile(txtpath) End if
set cnrs = fso.OpenTextFile(txtpath,8)
cnrs.WriteLine(FormatDate(Now(),1)&" "&sWord)
cnrs.Close
set cnrs = nothing
set fso = nothing
End Function
'/**
' * 日期格式化
' * DateAndTime 时间戳
' * datetype 显示的日期类型,1为2021-01-13 12:01:15
' *
Function FormatDate(DateAndTime, datetype)
On Error Resume Next
Dim y, m, d, h, mi, s, strDateTime
If Not IsNumeric(datetype) Then Exit Function
If Not IsDate(DateAndTime) Then Exit Function
y = CStr(Year(DateAndTime))
m = CStr(Month(DateAndTime))
If Len(m) = 1 Then m = "0" & m
d = CStr(Day(DateAndTime))
If Len(d) = 1 Then d = "0" & d
h = CStr(Hour(DateAndTime))
If Len(h) = 1 Then h = "0" & h
mi = CStr(Minute(DateAndTime))
If Len(mi) = 1 Then mi = "0" & mi
s = CStr(Second(DateAndTime))
If Len(s) = 1 Then s = "0" & s
Select Case datetype
Case "1"
strDateTime = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
Case "2"
strDateTime = y & "-" & m & "-" & d
Case "3"
strDateTime = y & "/" & m & "/" & d
Case "4"
strDateTime = y & "年" & m & "月" & d & "日"
Case "5"
strDateTime = m & "-" & d & " " & h & ":" & mi
Case "6"
strDateTime = m & "/" & d
Case "7"
strDateTime = m & "月" & d & "日"
Case "8"
strDateTime = y & "年" & m & "月"
Case "9"
strDateTime = y & "-" & m
Case "10"
strDateTime = y & "/" & m
Case "11"
strDateTime = right(y,2) & "-" &m & "-" & d & " " & h & ":" & mi
Case "12"
strDateTime = right(y,2) & "-" &m & "-" & d
Case "13"
strDateTime = m & "-" & d
Case Else
strDateTime = DateAndTime
End Select
FormatDate = strDateTime
End Function
%>
调用示例:
<%
LogResult("元宝支付 http://18pay.net/")
%>
效果如下图: