vba全局变量_日志相关

各自操作都需要日志,做了个简单的日志模块

使用方法:addLog保存日志到全局变量loglist和设置日志要保存的文件名

  • addLog的参数logpath_和staticLogpath不一样时会保存operateLoglist到staticLogpath并清空(staticLogpath存在时)
  • operateLoglist里,日志条数>=dataCount(默认值100)时会写一次文件,并清空loglist
  • 方法执行完后需要operateLoglist(0)来保存和清空loglist里剩下的日志
  • 设置dataCount为负数会一直不清空
Public staticLoglist As New Collection
Public staticLogpath$


Public Function getLoglist(Optional ByVal logpath_ As String)
    If Len(Trim(logpath_)) = 0 And Len(staticLogpath) = 0 Then
        If MsgBox("no logfile path,will use c:/log**.txt", vbYesNo) = vbYes Then
            logpath_ = "c:/vbalog" & Format(Now(), "yyyymmdd_hhmmss") & ".txt"
        Else
            Err.Raise 666, , "please reset your path to save log"
        End If
    End If
    If Len(Trim(logpath_)) > 0 Then
        If Len(staticLogpath) > 0 And staticLogpath <> logpath_ Then operateLoglist (0)
        staticLogpath = logpath_
    End If
    Set getLoglist = staticLoglist
End Function


Public Sub addLog(Optional ByVal kw$, Optional ByVal content$, Optional ByVal addDate As Boolean = True, _
Optional ByVal isErrorlog As Boolean = False, Optional ByVal logpath_$, Optional ByVal dataCount& = 100)
    Call getLoglist(logpath_)
    Dim newlogarr$(1 To 4)
    'newlogarr(1) = kw						'kw not used now
    newlogarr(2) = content
    If addDate Then newlogarr(3) = Now()
    If isErrorlog Then
        newlogarr(4) = "1"
    Else
        newlogarr(4) = "0"
    End If
    
    staticLoglist.Add (newlogarr)
    Call operateLoglist(dataCount)
End Sub

Public Sub operateLoglist(Optional ByVal dataCount As Long = 100, Optional ByVal saveKwOnly As Boolean = False)
    If staticLoglist.Count >= dataCount And dataCount >= 0 Then
        Set fso = CreateObject("scripting.filesystemobject")
        logstr = ""
        For i = 1 To staticLoglist.Count
            'logstr = logstr & staticLoglist(i) & vbCrLf
            logstr = arrlog2str(staticLoglist(i), logstr)
        Next i
        Call write2TextFile(logstr, staticLogpath, , fso)
        'Set staticLoglist = New Collection       'if use static staticLoglist do not use this,has bug
        For i = staticLoglist.Count To 1 Step -1
            staticLoglist.Remove (i)
        Next i        
        Set fso = Nothing
    End If
End Sub

Public Function arrlog2str(ByVal arr, ByVal logstr$)      'kw,content,data,iserror
    If arr(4) = "1" Then
        logstr = logstr & "[ERROR]"
    Else
        logstr = logstr & "[INFO]"
    End If
    If Len(arr(3)) > 0 Then logstr = logstr & "[" & arr(3) & "]"
    arrlog2str = logstr & arr(2) & vbCrLf
End Function
  • testLog1根据数据不同保存到不同的日志文件,每次换文件名时自动保存
  • testLog2遍历文件夹并保存路径。找到文件夹名换行,文件名不换行
Sub testLog1()
    For i = 1 To 22
        logpath = "C:\Users\Administrator\Desktop\log" & (i Mod 3) & ".txt"
        Call addLog(, "add " & i & " item ", True, , logpath, 5)
    Next i
    Call operateLoglist(0)
End Sub

Public Function walk(ByVal fso, ByVal path1 As String)
	Call addLog(, path1, , True, , 0)
    Set folders1 = fso.getfolder(path1)
    For Each i In folders1.Files
        Call addLog(, i.Path, , , , -100)
    Next i
    For Each i In folders1.subfolders	
        Call walk(fso, i.Path)
    Next i
End Function

Sub testLog2()
    path1 = "D:\Git"
    Set fso = CreateObject("scripting.filesystemobject")
    logpath = "C:\Users\Administrator\Desktop\log1.txt"
    Call getLoglist(logpath)
    Call walk(fso, path1)
    Call operateLoglist(0)
    Set fso = Nothing
End Sub

你可能感兴趣的:(vba,excel)