Hallo Leute,
hier mal etwas zum Thema "Logfiles und Lotus Script"
Bei umfangreichen Scripten ist es oftmal notwendig, den Ablauf zu protokollieren. Auch wenn der Agent im Debugger einwandfrei läuft ( was beim Einsatz von testdaten eigentlich
immer der Fall ist ) kann es sein, daß im produktiven betrieb Fehler auftreten.
Die folgende Klasse bietet eine einfache Möglichkeit schon bei der Erstellung des Agenten entsprechenden Code für die Protokollierung der Agentenausführung zu implementieren.
Es gibt unterschiedliche Tiefen der Protokollierung, die über Parameter gesteuert werden.
LOG_DEBUG protokolliert ALLE LogAction, Subtasks, LogError Einträge im Code
LOG_NORMAL beispielsweise protokolliert nur die Zeilen, die ebenfalls ein LOG_NORMAL als Protokolltiefe enthalten.
Die DefineTask Methode ermöglicht es im Log anzuzeigen, in welcher Sub oder Function man sich gerade befindet.
Die Protokolleinträge werden wie im normalen Log in ein oder Mehrere Dokumente geschrieben, die wiederum mehrere Einträge enthalten können.
Benötigt wird eine Datenbank mit folgenden Designelementen:
Maske "Log" mit einem Textfeld "Log" , Mehrfachwerte, Anzeige mit neuer Zeile
View "Log"
Den folgenden Code in die DECLARATION section eine neuen ScriptLib ( LibLog) kopieren:
'=======================================================================
' CLASS LOGPROFILE
'=======================================================================
Const TASKLENGTH = 15
Const LOG_NONE = 00
Const LOG_ERROR = 01
Const LOG_NORMAL = 02
Const LOG_EXTENDED = 03
Const LOG_DEBUG = 99
Const NORMALLOG = 1
Const ERRORLOG = 2
Dim logview As NotesView
Dim logdc As NotesDocumentCollection
Class LOGProfile
Public LogLevel As Integer
Public logdoc As NotesDocument
Public logerrordoc As NotesDocument
Public Subtask As String * TASKLENGTH
Public AgentName As String * TASKLENGTH
Public Oldtask As String
Sub New(db As NotesDatabase,scriptname As String,niveau As Integer,username As String)
Dim special_item As NotesItem
Dim agent As NotesAgent
Dim serverdoc As NotesDocument
Dim serverprofile As NotesDocument
Dim onserver As String
Dim currentservername As String
Subtask = ""
Me.loglevel = niveau
Me.AgentName = scriptname ' truncated if to long
Set logview = db.GetView("Log")
If logview Is Nothing Then
Error 1000,"View for logging not existing"
End If
Call logview.Refresh 'added to avoid that a new log is created but not seen by this routine
Set logdc = logview.GetAllDocumentsByKey(Date$ & username & scriptname)
Select Case logdc.Count
Case 0:
Set logdoc = CreateNewLog(db,username, scriptname,NORMALLOG) ' changed 23/3/99 added scriptname
Call logview.Refresh
Case 1:
Set logdoc = logdc.GetFirstDocument
Case Else: 'close them all off and create a new log
For i = 1 To logdc.Count
Set logdoc = logdc.GetNthDocument(i)
logdoc.LogEnd = Format$(logdoc.LASTMODIFIED,"hh:mm:ss")
Call logdoc.Save(True,True)
Set logdoc = CreateNewLog(db,username, scriptname,NORMALLOG) ' changed 23/3/99 added scriptname
Call logview.Refresh
Next
End Select
Set logdc = logview.GetAllDocumentsByKey(Date$ & "ERROR" & username & scriptname)
Select Case logdc.Count
Case 0:
Set logerrordoc = CreateNewLog(db,username, scriptname,ERRORLOG)
Call logview.Refresh
Case 1:
Set logerrordoc = logdc.GetFirstDocument
Case Else: 'close them all off and create a new log
For i = 1 To logdc.Count
Set logerrordoc = logdc.GetNthDocument(i)
logerrordoc.LogEnd = Format$(logerrordoc.LASTMODIFIED,"hh:mm:ss")
Call logerrordoc .Save(True,True)
Set logerrordoc = CreateNewLog(db,username, scriptname,ERRORLOG)
Call logview.Refresh
Next
End Select
End Sub
Sub LogAction(message As String,level As Integer)
Dim item As NotesItem
Dim db As NotesDatabase
Dim size As Long
If Me.LogLevel >= level Then
If Me.LogLevel = LOG_DEBUG Then
If level = LOG_DEBUG Then
levelstr$ = " ( " & Trim(Str$(level)) & ") " & Subtask
Else
levelstr$ = " ( " & Trim(Str$(level)) & ") " & Subtask
End If
Else
levelstr$ = ""
End If
Set item = logdoc.Getfirstitem("Log")
If Not item Is Nothing Then 'check if the document does not get to large
size = item.VALUELENGTH
Else
Set item = New NotesItem(logdoc,"Log","") ' if there is no logitem create a new one and the size will then not be to large so we can presume 0
size = 0
End If
If size + Len(message) > 32000 Then 'create a new logdoc if the old one becomes to large
Set db = logdoc.ParentDatabase
logdoc.LogEnd = Format$(Time$,"hh:mm:ss")
Call logdoc.Save(True,True)
Set logdoc = CreateNewLog(db,logdoc.LogUser(0),logdoc.Agentname(0),NORMALLOG) ' changed 23/3/99 added scriptname
Set item = New NotesItem(logdoc,"Log","")
End If
If message = "" Then
Call item.AppendToTextList("")
Else
Call item.AppendToTextList(Format$(Time$,"hh:mm:ss") & " " & Me.AgentName & " " & levelstr$ & " : " & message )
End If
Call logdoc.Save(True,True)
End If
End Sub
Sub LogError(errorcode As Integer,message As String,level As Integer)
Dim item As NotesItem
Dim db As NotesDatabase
Dim size As Long
If Me.LogLevel >= level Then
If Me.LogLevel = LOG_DEBUG Then
If level = LOG_DEBUG Then
levelstr$ = " ( " & Trim(Str$(level)) & ") " & Subtask
Else
levelstr$ = " ( " & Trim(Str$(level)) & ") " & Subtask
End If
Else
levelstr$ = ""
End If
Set item = logdoc.Getfirstitem("Log")
If Not item Is Nothing Then 'check if the document does not get to large
size = item.VALUELENGTH
Else
Set item = New NotesItem(logdoc,"Log","") ' if there is no logitem create a new one and the size will then not be to large so we can presume 0
size = 0
End If
If size + Len(message) > 32000 Then 'create a new logdoc if the old one becomes to large
Set db = logdoc.ParentDatabase
logdoc.LogEnd = Format$(Time$,"hh:mm:ss")
Call logdoc.Save(True,True)
Set logdoc = CreateNewLog(db,logdoc.LogUser(0),logdoc.Agentname(0),NORMALLOG) ' changed 23/3/99 added scriptname
Set item = New NotesItem(logdoc,"Log","")
End If
If message = "" Then
Call item.AppendToTextList("")
Else
Call item.AppendToTextList(Format$(Time$,"hh:mm:ss") & " " & Me.AgentName & " " & levelstr$ & " *** Error *** :" & Str(errorcode) & " : " &
message )
End If
Call logdoc.Save(True,True)
If level = LOG_ERROR Then ' error messages are logged seperate
Set item = logerrordoc.Getfirstitem("Log")
If Not item Is Nothing Then 'check if the document does not get to large
size = item.VALUELENGTH
Else
Set item = New NotesItem(logerrordoc,"Log","") ' if there is no logitem create a new one and the size will then not be to large so we
can presume 0
size = 0
End If
If size + Len(message) > 32000 Then 'create a new logdoc if the old one becomes to large
Set db = logerrordoc.ParentDatabase
logerrordoc.LogEnd = Format$(Time$,"hh:mm:ss")
Call logerrordoc.Save(True,True)
Set logerrordoc = CreateNewLog(db,logdoc.LogUser(0),logdoc.Agentname(0),ERRORLOG)
Set item = New NotesItem(logerrordoc,"Log","")
End If
If message = "" Then
Call item.AppendToTextList("")
Else
Call item.AppendToTextList(Format$(Time$,"hh:mm:ss") & " " & Me.AgentName & " " & levelstr$ & " *** Error *** :" & Str(errorcode) &
" : " & message )
End If
Call logerrordoc.Save(True,True)
End If
End If
End Sub
Sub DefineTask(task As String)
Subtask = task
Oldtask = Subtask & Oldtask
Call Me.LogAction(">> " & Subtask,99)
End Sub
Sub ResetTask()
Dim taskname As String * TASKLENGTH
If Len(Oldtask) < TASKLENGTH Then
Oldtask = ""
Else
Call Me.LogAction("<< " & Left(Oldtask,TASKLENGTH ),99)
Oldtask = Right(Oldtask,Len(Oldtask) - TASKLENGTH )
End If
Subtask = Oldtask
End Sub
End Class
Function CreateNewLog(LogDb As NotesDatabase,username As String, scriptname As String,logtype As Integer) As NotesDocument ' changed 23/3/99 added scriptname
Set logdoc = New NotesDocument(Logdb)
logdoc.Form = "Log"
logdoc.Type = logtype
logdoc.AgentName = scriptname
logdoc.LogUser = userName
logdoc.LogDate = Date$
Set notesItem = New NotesItem( logdoc, "author", username, AUTHORS)
Set CreateNewLog = logdoc
End Function
'=======================================================================
' END CLASS
'=======================================================================
und hier dann noch ein kleines Beispiel:
Sub Click(Source As Button)
Dim db As New NotesDatabase ("","alog.nsf")
Dim L As New LogProfile (db, "testagent",LOG_NORMAL, "Ulrich Krause")
Call L.DefineTask ("SUB: XYZ")
Call L.LogAction("macht das, was er machen soll",LOG_DEBUG)
Call L.ResetTask
Call L.DefineTask ("FUNCTION GetDocs")
Call L.LogAction("macht nur Mist",LOG_DEBUG)
Call L.ResetTask
Call L.LogError ( 123, "da haben wir mal wieder Mist gemacht" , LOG_NORMAL)
End Sub