This is a simple logger class i use to help with debugging. It has the advantage of the entries being time stamped to see when they happen as well as creating a permanent record in a txt file. When debugging the entries are also written to the log window.
The log file uses the day date as part of its file name so the files can be viewed for up to a month after their creation. Log files can become quite long but at the same time can be used to record what the app is doing and help with debugging difficult to find faults.
Suggested use;
declare and create in the starter service
Initialize parameters:
File.DirDefaultExternal and "TheLogFile" determine where the file will be stored and its name.
The file name created will be "TheLogFile20.txt" if it is the 20th of the month - etc.
The log file (and log window) looks like
Examples of use (here in Main but can be just about anywhere because Starter.pLogger is global)
The main logging methods (LogInformation, LogWarning...) differ only in the letter placed after the time stamp (I=information in the example, W=warning, F=fault, T=Test result). All methods take a source and message string.
log entries created by the logging methods are initially put in a list when the method is called. Every 250ms this list is written to the log txt file and the list cleared. This is done to prevent the logging methods slowing the user code with txt file activities.
The pLogger.Flush call causes the list to be immediately written to the txt file.
I welcome any enhancements, optimizations etc.
The log file uses the day date as part of its file name so the files can be viewed for up to a month after their creation. Log files can become quite long but at the same time can be used to record what the app is doing and help with debugging difficult to find faults.
Suggested use;
declare and create in the starter service
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Public pLogger As TrsLogClass
B4X:
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
pLogger.Initialize(File.DirDefaultExternal , "TheLogFile" )
pLogger.LogInformation("Logger Test","V0.01 11FEB17")
Initialize parameters:
File.DirDefaultExternal and "TheLogFile" determine where the file will be stored and its name.
The file name created will be "TheLogFile20.txt" if it is the 20th of the month - etc.
The log file (and log window) looks like
B4X:
12:21:16:194::I:TrsLogClass:Log file is TheLogFile20.txt
12:21:16:194::I:Logger Test:V0.01 11FEB17
12:21:16:272::I:Main:Activity resume
12:21:19:506::I:Main:Activity pause - user closed
Examples of use (here in Main but can be just about anywhere because Starter.pLogger is global)
B4X:
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Starter.pLogger.LogInformation("Main", "Activity pause - user closed")
Else
Starter.pLogger.LogInformation("Main", "Activity pause - user closed = false")
End If
Starter.pLogger.Flush
End Sub
The main logging methods (LogInformation, LogWarning...) differ only in the letter placed after the time stamp (I=information in the example, W=warning, F=fault, T=Test result). All methods take a source and message string.
log entries created by the logging methods are initially put in a list when the method is called. Every 250ms this list is written to the log txt file and the list cleared. This is done to prevent the logging methods slowing the user code with txt file activities.
The pLogger.Flush call causes the list to be immediately written to the txt file.
I welcome any enhancements, optimizations etc.