日志是开发系统时的有效工具和常见需求。它不仅可以在程序排错时提供调试信息,还可以记录系统运行的日常状况,以供需要时查询或集中起来分析。在一些主要的编程语言如Java中,都有不少日志框架可供选择。在LotusNotes里,在文档、代理、数据库和系统级别Notes本身记录和保留了很多信息,再加上Notes数据库功能和日志载体格式的限制,通用的日志程序和框架不发达。不过在OpenNtf.Org网站上,也至少有两个项目是用于日志的。一是广受好评的OpenLog,最大的特色就是只有一行调用函数就可以得到外观不错信息丰富的错误记录。另一个是鲜有人注意的Log4Dom特点是(从名称上也可以看出来)仿造Log4J的思路,创建了一套自定义类以记录日志。前者虽然方便,但是从笔者的角度看,记录的信息有些累赘,而且表单和视图都有特定的外观风格,不易整合进整个系统。如果是要记录错误信息,笔者觉得使用34. LotusScript中的错误处理程序一文中的代码就足够了。后者的思路和风格笔者却很喜欢。虽然它也有不少缺点,比如一味模仿Log4J,建立的几个日志载体类有些牵强,调用过程也不方便,并且重要的是还有几个bug影响使用。于是笔者对它作了一定的调整和修改,它遂变成开发中不可少的称手工具。
日志视图:
单个日志文档:
记录了错误信息的日志文档:
在一个函数中使用Log4Dom记录错误信息到日志的样例如下:
Private Function SendUrgentMail(doc As NotesDocument)
Dim s As New NotesSession
Dim db As NotesDatabase
Set db=s.Currentdatabase
Dim logger As log4dom
Set logger = GetLogger(Nothing)
On Error GoTo ErrorHandler
Dim sendTo As String
sendTo=GetMail(doc.PersonInCharge(0))
logger.debug("Sent an urgent PO to: " & sendTo)
If sendTo="" Then
Print "The mail of the current handler of the PO is not found."
GoTo ExitFunction
End If
Dim body As String, subject As String
body=GetMailBody(doc)
subject="An Urgent Purchasing Order Of No. " & doc.No(0) & " Is Appending For Your Handling."
Call SendHTMLMail(sendTo, subject, body)
'mark the documents
Call doc.ReplaceItemValue("SentSummary", "Y")
ExitFunction:
Call logger.Close()
Exit Function
ErrorHandler:
Call logger.LogError("")
GoTo ExitFunction
End Function
开发中更多用到日志的是定时运行的代理,为此我们可以几个自定义类,在其中可以方便地直接使用一个作为对象字段的日志对象,而无需像上面那样专门定义和初始化日志实例。
Public Class UseLog
Private logger As log4Dom
%REM
Sub New
Description: Comments for Sub
%END REM
Sub New()
Set logger=GetLogger(Nothing)
End Sub
'close the logger
Sub Delete()
Call logger.Close()
End Sub
End Class
Public Class AgentWithLog As UseLog
Private s As NotesSession
Private db As NotesDatabase
%REM
Sub New
Description: Comments for Sub
%END REM
Sub New()
Set s=New NotesSession
Set db=s.CurrentDatabase
End Sub
End Class
下面是一个使用上面的UseLog或AgentWithLog类的样例。只保留了使用日志的部分代码。代码较长,只需注意调用日志对象记录信息和错误的语句。本文上面图片中的日志大部分就是由这个代理产生的。
Private Class Reminder As UseLog
Private s As NotesSession
Private db As NotesDatabase
Private view As NotesView
Private detailCols As Dictionary
Private detailHeads As Variant
Private detailFields As Variant
Sub New()
On Error GoTo ErrorHandler
Set s=New NotesSession
Set db=s.Currentdatabase
Set view=db.Getview(VIEW_PO_BY_STATUS)
view.Autoupdate=False
Set me.detailCols=New Dictionary
With detailCols
Call .Add("Description", "Title")
Call .Add("Amount", "Amt_1")
Call .Add("Sub Expense Account", "expItem")
Call .Add("Employee", "Employee")
Call .Add("Office", "Office")
Call .Add("Department", "Dept")
End With
me.detailFields=me.detailCols.GetValues()
me.detailHeads=me.detailCols.GetKeys()
Exit Sub
ErrorHandler:
Call logger.LogError("")
Exit Sub
End Sub
Sub Delete()
view.Autoupdate=true
End Sub
Private Function Notify(status As String, handler As String )
On Error GoTo ErrorHandler
Dim keys(1) As String
Dim sendTo As Variant
keys(0)=status 'status
keys(1)=handler 'handler
sendTo=GetCustomMail(keys(1), "OA\CTHR.nsf", "bySCRO", "Staff_EMail")
If sendTo="" Then
logger.info(keys(1) & "'s mail not found.")
Else
Dim vec As NotesViewEntryCollection
Set vec=view.Getallentriesbykey(keys, True)
Dim body As String, subject As String, copyTo As Variant
body=GetMailBody(vec)
subject=vec.Count & " Expenses Of Status " & keys(0) & " Are Pending For Your Handling"
Select Case keys(0)
Case "4.2 Rejected After Final Review"
'inform all the users Brad rejected the PO
copyTo=GetCustomMail(vec.Getfirstentry().Document.FlowReaders, _
"OA\CTHR.nsf", "bySCRO", "Staff_EMail")
logger.info("Copy to: " & Join(copyTo, ","))
Case Else
copyTo=""
End Select
Call CCHTMLMail(sendTo, copyTo, "", subject, body)
If IsArray(sendTo) Then
sendTo=Join(sendTo, ", ")
End If
logger.info("Sent Expense Summary of '" & keys(0) & "' to " & sendTo)
'mark the documents
'Call dc.Stampall("SentSummary", "Y")
End If
Exit Function
ErrorHandler:
Call logger.LogError("")
Exit Function
End Function
End Class
下一片文章里,我们来详细看看这个背后的Log4Dom。