Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Logger.exception() methods for Apex developers #535

Merged

Conversation

jongpie
Copy link
Owner

@jongpie jongpie commented Aug 2, 2023

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);

I've also included a bugfix to resolve #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.

@jongpie jongpie added Type: Enhancement New feature or request Logging Source: Apex Items related to using Logger within Apex Layer: Logger Engine Items related to the core logging engine labels Aug 2, 2023
@jongpie jongpie had a problem deploying to Experience Cloud Scratch Org August 2, 2023 16:53 — with GitHub Actions Failure
@jongpie jongpie had a problem deploying to Experience Cloud Scratch Org August 2, 2023 17:04 — with GitHub Actions Failure
@jongpie jongpie force-pushed the feature/new-logger-exception-methods-for-apex-developers branch from 3c4fe2d to bc1de52 Compare August 2, 2023 17:18
@jongpie jongpie temporarily deployed to Base Scratch Org August 2, 2023 17:22 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Experience Cloud Scratch Org August 2, 2023 17:22 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Demo Org August 2, 2023 17:37 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Demo Org August 2, 2023 17:51 — with GitHub Actions Inactive
@jongpie jongpie marked this pull request as ready for review August 3, 2023 03:26
@jongpie jongpie force-pushed the feature/new-logger-exception-methods-for-apex-developers branch 2 times, most recently from 3e41f51 to 4f16b64 Compare August 7, 2023 21:42
@jongpie jongpie had a problem deploying to Experience Cloud Scratch Org August 7, 2023 21:48 — with GitHub Actions Failure
@jongpie jongpie force-pushed the feature/new-logger-exception-methods-for-apex-developers branch from 4f16b64 to 1a6f002 Compare August 7, 2023 23:01
@jongpie jongpie had a problem deploying to Experience Cloud Scratch Org August 7, 2023 23:04 — with GitHub Actions Failure
@jongpie jongpie force-pushed the feature/new-logger-exception-methods-for-apex-developers branch from 1a6f002 to f779b98 Compare August 7, 2023 23:25
@jongpie jongpie temporarily deployed to Base Scratch Org August 7, 2023 23:29 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Experience Cloud Scratch Org August 7, 2023 23:29 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Experience Cloud Scratch Org August 7, 2023 23:51 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Base Scratch Org August 7, 2023 23:51 — with GitHub Actions Inactive
@jongpie jongpie force-pushed the feature/new-logger-exception-methods-for-apex-developers branch from f779b98 to 826cb27 Compare August 8, 2023 01:53
@jongpie jongpie temporarily deployed to Experience Cloud Scratch Org August 8, 2023 01:57 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Base Scratch Org August 8, 2023 01:57 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Experience Cloud Scratch Org August 8, 2023 12:13 — with GitHub Actions Inactive
@jongpie jongpie temporarily deployed to Base Scratch Org August 8, 2023 12:13 — with GitHub Actions Inactive
@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +1.11% 🎉

Comparison is base (c4d3987) 94.84% compared to head (826cb27) 95.96%.
Report is 1 commits behind head on main.

❗ Current head 826cb27 differs from pull request most recent head e18ee7c. Consider uploading reports for the commit e18ee7c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #535      +/-   ##
==========================================
+ Coverage   94.84%   95.96%   +1.11%     
==========================================
  Files          59       47      -12     
  Lines        6072     4927    -1145     
  Branches      137        0     -137     
==========================================
- Hits         5759     4728    -1031     
+ Misses        307      199     -108     
+ Partials        6        0       -6     
Flag Coverage Δ
Apex 95.96% <100.00%> (+0.41%) ⬆️
LWC ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
...-logger/core/main/logger-engine/classes/Logger.cls 96.25% <ø> (+0.14%) ⬆️
...-management/classes/LoggerHomeHeaderController.cls 100.00% <100.00%> (ø)
...ger/core/main/logger-engine/classes/FlowLogger.cls 100.00% <100.00%> (ø)

... and 13 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jongpie jongpie merged commit a08e1d6 into main Aug 8, 2023
@jongpie jongpie deleted the feature/new-logger-exception-methods-for-apex-developers branch August 8, 2023 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Layer: Logger Engine Items related to the core logging engine Logging Source: Apex Items related to using Logger within Apex Type: Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

View Release Notes Button on Homepage
1 participant