Tip

LotusScript Logging Class

This class can be used to create a log comparable to the standard Notes server log. It allows to log more or less depending on a log level. It requires a database, with a view (Log) with the first column :

LogDate +LogEnd + @If(Type=2;"ERROR";"") + LOGUSER + AgentName

The logging is defined as
Dim logging As LOGProfile
Set logging = New LogProfile(logdatabase As Notesdatabase,scriptName$,LOG_NORMAL,session.Username)

And can be called with
Call logging.LogAction("in the ProcRtn",LOG_DEBUG) or
Call logging.LogError(1000,"security failed",LOG_ERROR)

Code:

'HELogging:

Option Public

' Option Declare
Const TASKLENGTH = 8
' trace level
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 ' used to display the task for all messages
Public AgentName As String * TASKLENGTH
Public Oldtask As String
Sub New(db As NotesDatabase,scriptname As String,niveau As Integer,username As String)
' username must be the name of the user that updates the document or a group or role, this value is set in the author field
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) ' changed 23/3/99 added 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) ' changed 23/3/99 added scriptname
Select Case logdc.Count
Case 0:
Set logerrordoc = CreateNewLog(db,username, scriptname,ERRORLOG) ' changed 23/3/99 added scriptname
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) ' changed 23/3/99 added scriptname
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 ' save TASKLENGTH characters
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

Sub Initialize

End Sub
Sub Terminate

End Sub
Function CreateNewLog(LogDb As NotesDatabase,username As String, scriptname As String,logtype As Integer) As NotesDocument ' changed 23/3/99 added scriptname
' username must be the name of the user that updates the document or a group or role, this value is set in the author field
Set logdoc = New NotesDocument(Logdb)
logdoc.Form = "Log"
logdoc.Type = logtype
logdoc.AgentName = scriptname ' changed 23/3/99 added scriptname
logdoc.LogUser = userName
logdoc.LogDate = Date$
Set notesItem = New NotesItem( logdoc, "author", username, AUTHORS)
Set CreateNewLog = logdoc
End Function

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.