Logging using platform events
In Chapter 4, Exceptions and Exception Handling, we constructed a simple logging system that would create Log__c
records from our logging class. One issue with such a setup, however, is that if the application errors and the correct save points are not put in place, then the logs will not save and persist to the database. We may also just want a stream of events that can be consumed by an external system or a developer could subscribe to and view in their terminal. To achieve these aims, we could augment our logging setup so that it utilizes platform events for logging.
Our Log__c
custom object had three fields – Stack_Trace__c
, Log_Message__c
, and Type__c
. Let’s replicate them onto a new platform event, Log__e
.
In our original Logger
class, we had the method log, which inserted the Log__c
record. This method can be updated to the code shown here so that it publishes a Log__e
event instead:
public static void log(String stackTrace...