Most Salesforce orgs require some form of logging to overcome the limitations of the standard debug logs which do not persist. Logging frameworks often become some of your lowest level code, so its imperative that they are non-blocking to normal code - and equally that failures in the main execution can't block the DML of the logs. This makes Platform Events a great candidate to use, as they fire immediately, asynchronously, and in under their own execution.
- This is still a work in progress and is not production ready
- Utility class to create logs (Logger.cls)
- Batch Job to delete historic logs
- Log structure encapsulated in class, with subclasses to handle different types of logs, or indeed create different types of Platform Events (Log.cls)
- Platform Events are published for any logs created
- A Trigger on the Platform Event listens for events and persists them to a Custom Object
- Error handling for character limits (eg. stack trace too big for field)
- Improve the Log.location matching. The current implementation is largely based on this stackexchange post, but its not ideal.
- Unit tests
- Expand the types that can be logged
- Stacked exceptions - could these be logged recursively?
- Implement a basic 'JSON Viewer' in LWC to make viewing log data nicer (possibly using something already available like json-viewer-js
- Clone the repo
git clone https://github.com/tomcarman/logger.git
- Create a new scratch org
sfdx force:org:create -f config/project-scratch-def.json -a logger-demo
- Push source to the org
sfdx force:source:push -u logger-demo
- Open the org
sfdx force:org:open -u logger-demo
- Change to the App 'Logger Demo' and switch the List View to 'All'
- View some logs (you'll need to create some first - see Usage)
- Deploy metadata using your normal methods
// Create & immediately publish a simple log
Logger.get().publish('Hello world');
// Also accepts Exceptions or DMLExceptions eg.
try {
insert new Account();
} catch(DMLException dmlEx) {
Logger.get().publish(dmlEx);
}
// If you know you are going to create many logs in quick succession, they
// can be stored on the stack before publishing
Logger.get().add('Starting awesome code');
// Awesome code
// ..
// ..
Logger.get().add('Something went wrong');
// Dont forget to publish the logs!
Logger.get().publish();
This project is heavily inspired by the following -
- The recent blog post and CodeLive by @codefriar and @ca_peterson
- A demo of a similar topic by @afawcett
- apex-unified-logging framework by @rsoesemann