Skip to content

New Logger.exception() methods for Apex developers

Compare
Choose a tag to compare
@jongpie jongpie released this 08 Aug 20:13
· 56 commits to main since this release
a08e1d6

Core Unlocked Package Changes

New Logger.exception() methods for Apex developers

This release provides some syntactic sugar for Apex developers when logging exceptions. A common pattern for using Nebula Logger is to log exceptions in try-catch blocks. Once an exception has been caught, Apex developers have to add 3 lines of code in order to log the exception & re-throw it, as shown below:

try {
    insert new Account();
} catch (System.Exception ex) {
    Logger.error('something broke 😥', ex);
    Logger.saveLog(Logger.SaveMethod.EVENT_BUS);
    throw ex;
}

Although 3 lines of code isn't a huge amount of code, it's still tedious to have to repeat the same code in any relevant try-catch blocks. Now, Apex developers can use the new method overloads Logger.exception() to consolidate down to 1 line in a catch block. The end result of the below snippet is identical to the snippet above - both result in a new ERROR log entry being generated, automatically saved, and then the exception is thrown.

try {
    insert new Account();
} catch (System.Exception ex) {
    Logger.exception('something broke 😥', ex);
}

The full list of new Logger.exception() method overloads is shown below. Each of the methods will log an entry with ERROR logging level, save the log, and then throw the provided exception. Since an exception is always thrown, the method overloads all have a void return type.

global static void exception(LogMessage logMessage, System.Exception apexException);
global static void exception(LogMessage logMessage, Id recordId, System.Exception apexException);
global static void exception(LogMessage logMessage, SObject record, System.Exception apexException);
global static void exception(LogMessage logMessage, List<SObject> records, System.Exception apexException);
global static void exception(String message, System.Exception apexException);
global static void exception(String message, Id recordId, System.Exception apexException);
global static void exception(String message, SObject record, System.Exception apexException);
global static void exception(String message, List<SObject> records, System.Exception apexException);

Bugfixes

  • Bugfix for issue #529 where the optional integration with api.status.salesforce.com would cause exceptions in Nebula Logger if the remote site setting was disabled and the callout was still enabled via LoggerParameter__mdt record CallStatusApi. Now, Nebula Logger internally catches any callout exceptions, which should prevent any downstream issues from occurring.

Apex Test Improvements

  • Scope creep: finished a few TODO items in the Apex class Logger_Tests (part of the logger engine layer) so that it's not aware of the log-management layer by rewriting several test methods to remove references to Log__c and LogEntry__c. These tests now also use mocks for the System.EventBus class, so they're now much faster 🏎️

Pipeline Enhancements

  • @JMercie closed issue #527 by updating Nebula Logger's pipeline & build scripts to use the new sf CLI, instead of the sfdx cli` (PR #532). Thanks to @JMercie for another amazing contribution! 🥳

Installation Info

Core Unlocked Package - no namespace

Full Changelog: v4.11.1...v4.11.2