Skip to main content

Scripting - Sample on writing a log file

Article # 3037106 - Page views: 101


Are there any sample scripts on writing a log file using Kofax Transformation Modules Scripting?

How to log runtime information of KTM Server or Validation module to a file?



It is not possbile to debug KTM Server or thin client modules during runtime. So you can implement the following script in Project Builder. Afterwards you can use the command "writelog" on any document level to report anything to an external logfile. Add it to the project level so it will be available for all scripts in the project.

Public Const LOG_ENABLED As Boolean = True
Public LOGFILENAME As String

Public Sub WriteLog(ByVal sLogMessage As String)
   On Error GoTo ErrorHandler
   debug.print sLogMessage  ' Text Console

   If Not LOG_ENABLED Then Exit Sub
      LOGFILENAME= "c:\Windows\Temp\KTMscriptlog_"+Environ("COMPUTERNAME")+"_"+Format(Now,"YYYY-MM-DD")+".txt"
   End If
   Dim FileNum As Integer
   Open LOGFILENAME For Append Shared As #FileNum
   Print #FileNum,Format(Now,"hh:mm:ss") & " - " & sLogMessage
   Close #FileNum

   Exit Sub


End Sub

You can call it in your code with:

WriteLog("log this text")

Example to catch and report an error:

ON Error GOTO Errorhandler

... some code ...

exit Sub/Function

  WriteLog("Replace with your FUNCTIONNAME - Error:" & Err.Description)

End Sub/Function

Same can be used to log information in thin client server. Keep in mind that the data is logged from the running thin client server modules and not in the browser or client side.


If you want to log more information about your batch processing then you can add on project level:

Private Sub Batch_Open(ByVal pXRootFolder As CASCADELib.CscXFolder)
  WriteLog("Batch_Open Mode:"+project_mode2text+" Step:" + CStr(Project.ScriptExecutionInstance))
  WriteLog("Batch Class:    "+pXRootFolder.XValues.ItemByName("AC_BATCH_CLASS_NAME").Value)
  WriteLog("Batch Name:     "+pXRootFolder.XValues.ItemByName("AC_BATCH_NAME").Value)
  WriteLog("Project:        "+Project.FileName)
  'WriteLog("XValues:        "+get_Batch_XValues(pXRootFolder)
End Sub

Private Sub Batch_Close(ByVal pXRootFolder As CASCADELib.CscXFolder, ByVal CloseMode As CASCADELib.CscBatchCloseMode)
  WriteLog("Batch_Close  CloseMode:"+CloseMode2text(CloseMode))
  WriteLog("Batch Name:            "+pXRootFolder.XValues.ItemByName("AC_BATCH_NAME").Value)
End Sub

Private Sub Batch_InteractiveClosing(ByVal pXRootFolder As CASCADELib.CscXFolder, ByVal CloseMode As CASCADELib.CscBatchCloseMode, ByRef bCancel As Boolean, ByRef sMessage As String)
  WriteLog("Batch_InteractiveClosing CloseMode:"+CloseMode2text(CloseMode))
End Sub

Private Sub Document_BeforeExtract(ByVal pXDoc As CASCADELib.CscXDocument)
End Sub

Private Sub Document_AfterExtract(ByVal pXDoc As CASCADELib.CscXDocument)
End Sub

Private Function CloseMode2text(CloseMode As CASCADELib.CscBatchCloseMode) As String
  Select Case CloseMode
    Case CscBatchCloseChild:  CloseMode2text= "BatchCloseChild"
    Case CscBatchCloseError:  CloseMode2text= "BatchCloseError"
    Case CscBatchCloseFinal:  CloseMode2text= "BatchCloseFinal"
    Case CscBatchCloseParent: CloseMode2text= "BatchCloseParent"
    Case CscBatchCloseSuspend:CloseMode2text= "BatchCloseSuspend"
    Case Else
  End Select
End Function

Private Function project_mode2text As String
  Dim s As String
  Select Case Project.ScriptExecutionMode
    Case CscScriptModeUnknown: s="Unknown" ' This value was not initialized.
    Case CscScriptModeServer: s="Server" ' The script is executed by the server.
    Case CscScriptModeServerDesign: s="ServerDesign" ' The design application executes the server part (Project Builder)
    Case CscScriptModeValidation: s="Validation" ' The script is executed by the validation runtime.
    Case CscScriptModeValidationDesign: s="ValidationDesign" ' The design application executes the validation part (Validation Test in Project Builder)
    Case CscScriptModeVerification: s="Verification" ' The script is executed by the Verification runtime.
    Case CscScriptModeVerificationDesign: s="VerificationDesign" ' The script is executed by the Verification designer.
    Case CscScriptModeDocumentReview: s="DocumentReview" ' The script is executed by the Document Review runtime.
    Case CscScriptModeCorrection: s="Correction" '  The script Is executed by the Correction runtime.
    Case Else : s="(unknown)"
  End Select
  project_mode2text = s
End Function

Public Function get_Batch_XValues(ByVal pXRootFolder As CASCADELib.CscXFolder) As String
  Dim s As String
  Dim i As Integer
  For i=0 To pXRootFolder.XValues.Count-1
    s=s+pXRootFolder.XValues.ItemByIndex(i).Key +"="+pXRootFolder.XValues.ItemByIndex(i).Value+";"
End Function


Level of Complexity 



Applies to  

Product Version Build Environment Hardware
Kofax Transformation Module 6.3, 6.4 N/A N/A N/A



script_Logfilesimple_thread.txt  As KTM Server can use multiple instances to process a batch, it can help to log the processing of each process to a separate file.


  • Was this article helpful?