Skip to content

Commit ab92cd5

Browse files
authored
Bugfix: Updated Logger.setAsyncContext() behavior (#770)
* Updated the behavior of Logger.setAsyncContext() to only set the context the first time a non-null context value is provided * Made some optimizations in build.yml so some steps don't run on draft PRs
1 parent 1629291 commit ab92cd5

7 files changed

Lines changed: 169 additions & 19 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ env:
88

99
on:
1010
push:
11+
branches:
12+
- main
1113
paths:
1214
- .github/workflows/**
1315
- config/scratch-orgs/**
1416
- nebula-logger/**
17+
- sfdx-project.json
1518
pull_request:
1619
types: [opened, synchronize, reopened]
1720
paths:
1821
- .github/workflows/**
1922
- config/scratch-orgs/**
2023
- nebula-logger/**
24+
- sfdx-project.json
2125

2226
jobs:
2327
code-quality-tests:
@@ -50,7 +54,7 @@ jobs:
5054
- './nebula-logger/core/**'
5155
5256
- name: 'Authorize Dev Hub'
53-
if: ${{ (github.event_name == 'pull_request') && (steps.changes.outputs.core == 'true') }}
57+
if: ${{ (github.event_name == 'pull_request') && (github.event.pull_request.draft == false) && (steps.changes.outputs.core == 'true') }}
5458
shell: bash
5559
run: |
5660
npx sf version
@@ -63,7 +67,7 @@ jobs:
6367
DEV_HUB_JWT_SERVER_KEY: ${{ secrets.DEV_HUB_JWT_SERVER_KEY }}
6468

6569
- name: 'Verify package version number is updated'
66-
if: ${{ (github.event_name == 'pull_request') && (steps.changes.outputs.core == 'true') }}
70+
if: ${{ (github.event_name == 'pull_request') && (github.event.pull_request.draft == false) && (steps.changes.outputs.core == 'true') }}
6771
run: npm run package:version:number:verify
6872

6973
- name: 'Verify LWC with ESLint'

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
The most robust observability solution for Salesforce experts. Built 100% natively on the platform, and designed to work seamlessly with Apex, Lightning Components, Flow, Process Builder & integrations.
77

8-
## Unlocked Package - v4.14.10
8+
## Unlocked Package - v4.14.11
99

10-
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oTdQAI)
11-
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oTdQAI)
10+
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oUgQAI)
11+
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oUgQAI)
1212
[![View Documentation](./images/btn-view-documentation.png)](https://github.com/jongpie/NebulaLogger/wiki)
1313

14-
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y0000015oTdQAI`
14+
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y0000015oUgQAI`
1515

16-
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y0000015oTdQAI`
16+
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y0000015oUgQAI`
1717

1818
---
1919

nebula-logger/core/main/logger-engine/classes/Logger.cls

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
global with sharing class Logger {
1616
// There's no reliable way to get the version number dynamically in Apex
1717
@TestVisible
18-
private static final String CURRENT_VERSION_NUMBER = 'v4.14.10';
18+
private static final String CURRENT_VERSION_NUMBER = 'v4.14.11';
1919
private static final System.LoggingLevel FALLBACK_LOGGING_LEVEL = System.LoggingLevel.DEBUG;
2020
private static final List<LogEntryEventBuilder> LOG_ENTRIES_BUFFER = new List<LogEntryEventBuilder>();
2121
private static final String MISSING_SCENARIO_ERROR_MESSAGE = 'No logger scenario specified. A scenario is required for logging in this org.';
@@ -196,31 +196,39 @@ global with sharing class Logger {
196196
* @param batchableContext - The instance of `Database.BatchableContext` to track
197197
*/
198198
global static void setAsyncContext(Database.BatchableContext batchableContext) {
199-
setAsyncContext(new AsyncContext(batchableContext));
199+
if (batchableContext != null) {
200+
setAsyncContext(new AsyncContext(batchableContext));
201+
}
200202
}
201203

202204
/**
203205
* @description Stores additional details about the current transacation's async context
204206
* @param finalizerContext - The instance of `System.FinalizerContext` to track
205207
*/
206208
global static void setAsyncContext(System.FinalizerContext finalizerContext) {
207-
setAsyncContext(new AsyncContext(finalizerContext));
209+
if (finalizerContext != null) {
210+
setAsyncContext(new AsyncContext(finalizerContext));
211+
}
208212
}
209213

210214
/**
211215
* @description Stores additional details about the current transacation's async context
212216
* @param queueableContext - The instance of `System.QueueableContext` to track
213217
*/
214218
global static void setAsyncContext(System.QueueableContext queueableContext) {
215-
setAsyncContext(new AsyncContext(queueableContext));
219+
if (queueableContext != null) {
220+
setAsyncContext(new AsyncContext(queueableContext));
221+
}
216222
}
217223

218224
/**
219225
* @description Stores additional details about the current transacation's async context
220226
* @param schedulableContext - The instance of `System.SchedulableContext` to track
221227
*/
222228
global static void setAsyncContext(System.SchedulableContext schedulableContext) {
223-
setAsyncContext(new AsyncContext(schedulableContext));
229+
if (schedulableContext != null) {
230+
setAsyncContext(new AsyncContext(schedulableContext));
231+
}
224232
}
225233

226234
/**
@@ -3415,8 +3423,13 @@ global with sharing class Logger {
34153423
}
34163424

34173425
private static void setAsyncContext(AsyncContext asyncContext) {
3418-
currentAsyncContext = asyncContext;
3419-
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Async Context: ' + System.JSON.serializePretty(asyncContext));
3426+
// Only set the async context the first time that a non-null value is provided
3427+
// Previous versions of Nebula Logger would always set it, but that wasn't the
3428+
// intended behavior
3429+
if (currentAsyncContext == null) {
3430+
currentAsyncContext = asyncContext;
3431+
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Async Context: ' + System.JSON.serializePretty(asyncContext));
3432+
}
34203433
}
34213434

34223435
private static SaveMethod getSaveMethod(String saveMethodName) {

nebula-logger/core/main/logger-engine/lwc/logger/logEntryBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import FORM_FACTOR from '@salesforce/client/formFactor';
66
import { log as lightningLog } from 'lightning/logger';
77
import { LoggerStackTrace } from './loggerStackTrace';
88

9-
const CURRENT_VERSION_NUMBER = 'v4.14.10';
9+
const CURRENT_VERSION_NUMBER = 'v4.14.11';
1010

1111
const LOGGING_LEVEL_EMOJIS = {
1212
ERROR: '⛔',

nebula-logger/core/tests/logger-engine/classes/Logger_Tests.cls

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,39 @@ private class Logger_Tests {
792792
System.Assert.areEqual(Database.BatchableContext.class.getName(), logEntryEvent.AsyncContextType__c);
793793
}
794794

795+
@IsTest
796+
static void it_should_use_first_non_null_context_details_for_batchable_context_when_event_published() {
797+
Database.BatchableContext nullMockContext = null;
798+
Database.BatchableContext firstNonNullMockContext = new LoggerMockDataCreator.MockBatchableContext();
799+
Database.BatchableContext secondNonNullMockContext = new LoggerMockDataCreator.MockBatchableContext();
800+
System.Assert.areNotEqual(
801+
firstNonNullMockContext,
802+
secondNonNullMockContext,
803+
'Test has started under the wrong conditions, expected 2 different mock contexts'
804+
);
805+
LoggerDataStore.setMock(LoggerMockDataStore.getEventBus());
806+
LogEntryEvent__e logEntryEvent = Logger.info('hello, world').getLogEntryEvent();
807+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
808+
System.Assert.isNull(logEntryEvent.AsyncContextParentJobId__c);
809+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
810+
System.Assert.isNull(logEntryEvent.AsyncContextType__c);
811+
System.Assert.areEqual(1, Logger.getBufferSize());
812+
System.Assert.areEqual(0, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
813+
814+
Logger.setAsyncContext(nullMockContext);
815+
Logger.setAsyncContext(firstNonNullMockContext);
816+
Logger.setAsyncContext(secondNonNullMockContext);
817+
Logger.saveLog(Logger.SaveMethod.EVENT_BUS);
818+
819+
System.Assert.areEqual(0, Logger.getBufferSize());
820+
System.Assert.areEqual(1, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
821+
System.Assert.areEqual(logEntryEvent, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().get(0));
822+
System.Assert.areEqual(firstNonNullMockContext.getChildJobId(), logEntryEvent.AsyncContextChildJobId__c);
823+
System.Assert.areEqual(firstNonNullMockContext.getJobId(), logEntryEvent.AsyncContextParentJobId__c);
824+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
825+
System.Assert.areEqual(Database.BatchableContext.class.getName(), logEntryEvent.AsyncContextType__c);
826+
}
827+
795828
@IsTest
796829
static void it_should_set_async_context_details_for_finalizer_context_when_event_published() {
797830
Id mockParentAsyncApexJobId = LoggerMockDataCreator.createId(Schema.AsyncApexJob.SObjectType);
@@ -818,6 +851,39 @@ private class Logger_Tests {
818851
System.Assert.areEqual(System.FinalizerContext.class.getName(), logEntryEvent.AsyncContextType__c);
819852
}
820853

854+
@IsTest
855+
static void it_should_use_first_non_null_context_details_for_finalizer_context_when_event_published() {
856+
System.FinalizerContext nullMockContext = null;
857+
System.FinalizerContext firstNonNullMockContext = new LoggerMockDataCreator.MockFinalizerContext();
858+
System.FinalizerContext secondNonNullMockContext = new LoggerMockDataCreator.MockFinalizerContext();
859+
System.Assert.areNotEqual(
860+
firstNonNullMockContext,
861+
secondNonNullMockContext,
862+
'Test has started under the wrong conditions, expected 2 different mock contexts'
863+
);
864+
LoggerDataStore.setMock(LoggerMockDataStore.getEventBus());
865+
LogEntryEvent__e logEntryEvent = Logger.info('hello, world').getLogEntryEvent();
866+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
867+
System.Assert.isNull(logEntryEvent.AsyncContextParentJobId__c);
868+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
869+
System.Assert.isNull(logEntryEvent.AsyncContextType__c);
870+
System.Assert.areEqual(1, Logger.getBufferSize());
871+
System.Assert.areEqual(0, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
872+
873+
Logger.setAsyncContext(nullMockContext);
874+
Logger.setAsyncContext(firstNonNullMockContext);
875+
Logger.setAsyncContext(secondNonNullMockContext);
876+
Logger.saveLog(Logger.SaveMethod.EVENT_BUS);
877+
878+
System.Assert.areEqual(0, Logger.getBufferSize());
879+
System.Assert.areEqual(1, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
880+
System.Assert.areEqual(logEntryEvent, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().get(0));
881+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
882+
System.Assert.areEqual(firstNonNullMockContext.getAsyncApexJobId(), logEntryEvent.AsyncContextParentJobId__c);
883+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
884+
System.Assert.areEqual(System.FinalizerContext.class.getName(), logEntryEvent.AsyncContextType__c);
885+
}
886+
821887
@IsTest
822888
static void it_should_set_async_context_details_for_queueable_context_when_event_published() {
823889
Id mockParentAsyncApexJobId = LoggerMockDataCreator.createId(Schema.AsyncApexJob.SObjectType);
@@ -844,6 +910,39 @@ private class Logger_Tests {
844910
System.Assert.areEqual(System.QueueableContext.class.getName(), logEntryEvent.AsyncContextType__c);
845911
}
846912

913+
@IsTest
914+
static void it_should_use_first_non_null_context_details_for_queueable_context_when_event_published() {
915+
System.QueueableContext nullMockContext = null;
916+
System.QueueableContext firstNonNullMockContext = new LoggerMockDataCreator.MockQueueableContext();
917+
System.QueueableContext secondNonNullMockContext = new LoggerMockDataCreator.MockQueueableContext();
918+
System.Assert.areNotEqual(
919+
firstNonNullMockContext,
920+
secondNonNullMockContext,
921+
'Test has started under the wrong conditions, expected 2 different mock contexts'
922+
);
923+
LoggerDataStore.setMock(LoggerMockDataStore.getEventBus());
924+
LogEntryEvent__e logEntryEvent = Logger.info('hello, world').getLogEntryEvent();
925+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
926+
System.Assert.isNull(logEntryEvent.AsyncContextParentJobId__c);
927+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
928+
System.Assert.isNull(logEntryEvent.AsyncContextType__c);
929+
System.Assert.areEqual(1, Logger.getBufferSize());
930+
System.Assert.areEqual(0, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
931+
932+
Logger.setAsyncContext(nullMockContext);
933+
Logger.setAsyncContext(firstNonNullMockContext);
934+
Logger.setAsyncContext(secondNonNullMockContext);
935+
Logger.saveLog(Logger.SaveMethod.EVENT_BUS);
936+
937+
System.Assert.areEqual(0, Logger.getBufferSize());
938+
System.Assert.areEqual(1, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
939+
System.Assert.areEqual(logEntryEvent, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().get(0));
940+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
941+
System.Assert.areEqual(firstNonNullMockContext.getJobId(), logEntryEvent.AsyncContextParentJobId__c);
942+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
943+
System.Assert.areEqual(System.QueueableContext.class.getName(), logEntryEvent.AsyncContextType__c);
944+
}
945+
847946
@IsTest
848947
static void it_should_set_async_context_details_for_schedulable_context_when_event_published() {
849948
Id mockCronTriggerId = LoggerMockDataCreator.createId(Schema.CronTrigger.SObjectType);
@@ -869,6 +968,39 @@ private class Logger_Tests {
869968
System.Assert.areEqual(System.SchedulableContext.class.getName(), logEntryEvent.AsyncContextType__c);
870969
}
871970

971+
@IsTest
972+
static void it_should_use_first_non_null_context_details_for_schedulable_context_when_event_published() {
973+
System.SchedulableContext nullMockContext = null;
974+
System.SchedulableContext firstNonNullMockContext = new LoggerMockDataCreator.MockSchedulableContext();
975+
System.SchedulableContext secondNonNullMockContext = new LoggerMockDataCreator.MockSchedulableContext();
976+
System.Assert.areNotEqual(
977+
firstNonNullMockContext,
978+
secondNonNullMockContext,
979+
'Test has started under the wrong conditions, expected 2 different mock contexts'
980+
);
981+
LoggerDataStore.setMock(LoggerMockDataStore.getEventBus());
982+
LogEntryEvent__e logEntryEvent = Logger.info('hello, world').getLogEntryEvent();
983+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
984+
System.Assert.isNull(logEntryEvent.AsyncContextParentJobId__c);
985+
System.Assert.isNull(logEntryEvent.AsyncContextTriggerId__c);
986+
System.Assert.isNull(logEntryEvent.AsyncContextType__c);
987+
System.Assert.areEqual(1, Logger.getBufferSize());
988+
System.Assert.areEqual(0, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
989+
990+
Logger.setAsyncContext(nullMockContext);
991+
Logger.setAsyncContext(firstNonNullMockContext);
992+
Logger.setAsyncContext(secondNonNullMockContext);
993+
Logger.saveLog(Logger.SaveMethod.EVENT_BUS);
994+
995+
System.Assert.areEqual(0, Logger.getBufferSize());
996+
System.Assert.areEqual(1, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().size());
997+
System.Assert.areEqual(logEntryEvent, LoggerMockDataStore.getEventBus().getPublishedPlatformEvents().get(0));
998+
System.Assert.isNull(logEntryEvent.AsyncContextChildJobId__c);
999+
System.Assert.isNull(logEntryEvent.AsyncContextParentJobId__c);
1000+
System.Assert.areEqual(firstNonNullMockContext.getTriggerId(), logEntryEvent.AsyncContextTriggerId__c);
1001+
System.Assert.areEqual(System.SchedulableContext.class.getName(), logEntryEvent.AsyncContextType__c);
1002+
}
1003+
8721004
@IsTest
8731005
static void it_should_set_parent_transaction_id() {
8741006
String expectedParentTransactionId = 'imagineThisWereAGuid';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nebula-logger",
3-
"version": "4.14.10",
3+
"version": "4.14.11",
44
"description": "The most robust logger for Salesforce. Works with Apex, Lightning Components, Flow, Process Builder & Integrations. Designed for Salesforce admins, developers & architects.",
55
"author": "Jonathan Gillespie",
66
"license": "MIT",

sfdx-project.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"path": "./nebula-logger/core",
1010
"definitionFile": "./config/scratch-orgs/base-scratch-def.json",
1111
"scopeProfiles": true,
12-
"versionNumber": "4.14.10.NEXT",
13-
"versionName": "New CallableLogger Apex class",
14-
"versionDescription": "Added a new CallableLogger class that provides support for both OmniStudio logging, as well as the ability to dynamically call Nebula Logger in Apex when it's available",
12+
"versionNumber": "4.14.11.NEXT",
13+
"versionName": "Updated Behavior of Logger.setAsyncContext()",
14+
"versionDescription": "Updated the behavior of Logger.setAsyncContext() to only set the context the first time a non-null context value is provided. Previously, subsequent calls would overwrite the context value, which wasn't really the intended behaviour.",
1515
"releaseNotesUrl": "https://github.com/jongpie/NebulaLogger/releases",
1616
"unpackagedMetadata": {
1717
"path": "./nebula-logger/extra-tests"
@@ -195,6 +195,7 @@
195195
"Nebula Logger - Core@4.14.8-store-httprequest-header-keys-&-values": "04t5Y0000015oS1QAI",
196196
"Nebula Logger - Core@4.14.9-bugfix:-apex-code-snippets-auto-truncated": "04t5Y0000015oSQQAY",
197197
"Nebula Logger - Core@4.14.10-new-callablelogger-apex-class": "04t5Y0000015oTdQAI",
198+
"Nebula Logger - Core@4.14.11-updated-behavior-of-logger.setasynccontext()": "04t5Y0000015oUgQAI",
198199
"Nebula Logger - Core Plugin - Async Failure Additions": "0Ho5Y000000blO4SAI",
199200
"Nebula Logger - Core Plugin - Async Failure Additions@1.0.0": "04t5Y0000015lhiQAA",
200201
"Nebula Logger - Core Plugin - Async Failure Additions@1.0.1": "04t5Y0000015lhsQAA",

0 commit comments

Comments
 (0)