This package provides the objects and methods to store logs generated by other Apex classes so they are logged for later analysis. Log data is stored in a custom object and can also be published as Platform Events for live monitoring.
Also included is a scheduable cleanup process that should be configured to run on a nightly basis to delete old logs, keeping the number of records down. The number of days that records should be kept is configurable via a custom metadata setting.
If you want to monitor the Platform Events that can be generated, a package that I like can be found at https://github.com/pozil/streaming-monitor, but others are also available
- DebugLog(String CallerIdentification)
- CallerIdentification is a string that identifies the process that created the entry
- saveLog(String LogEntry)
- LogEntry is a string that provides the data to be logged can be called multiple times to append to the log (but it does not commit the data)
- commitLog(String LogEntry)
- LogEntry is a string that provides data to be logged
- Calling this method saves the data in the custom object and clears the log string (but leaves the CallerIdentification intact)
- endPoint
- used to define the REST endpoint or method [optional]
You can control part of the operation of the package via several Custom Metadata (CMD) settings. The CMD is named 'Debug Logger Setting' and there are 2 fields that you can create in a record called 'Local'
- 'Days To Keep'
- defines the number of days of logs that should be kept during the cleanup processing. Logs older that the specified number of days will be deleted if you schedule the DebugLoggerCleanupScheduler to run as describe on the 'Post Install Steps' below.
- 'Publish Events'
- If this is set to true, will publish a event on the 'Debug_Log' platform event bus. This can be received by other processes for additional handling of the errors if desired.
static void myMethod() {
private static string commentString = 'random string';
DebugLog log = new DebugLog('className'); // inits the class with the caller info
log.endPoint = 'myMethod'; // define the calling method
log.saveLog('some comment '+ commentString); // saves some string (but does not commit)
[...]
log.commitLog('Exception caught '); // commits the log entry
}
Once the package is installed, you need to schedule the DebugCleanupScheduler job. Under the Setup menu, look for Custom Code -> Apex Classes. There you will see a button 'Schedule Apex'. After clicking that button, create a job name and select the Apex Class "DebugLoggerCleanupScheduler". Specify that it should run weekly, on everyday of the week, and specify a start and end date as well as a time (suggested 3:00 AM).
You can also change how many days of logs are kept during the cleanup job by modifying the Custom Metadata 'Local' settings called 'Debug Logger Settings'. If this value is changed, it will be used the next time the cleanup job runs.
You should also assign the 'Debug Log Viewer' permission set to any user that will need to be able to view the logs.
sfdx force:org:create -a logger -n -f config/project-scratch-def.json && sleep 2 ; sfdx force:source:push -u logger && sfdx force:user:permset:assign -u logger -n Debug_Log_Viewer ; sfdx force:org:open -u logger
Written by Marc D Behr in 2015 with numerous updates over the years