Skip to content

Commit

Permalink
Refactored logEntryBuilder.js and loggerService.js to use setScenario…
Browse files Browse the repository at this point in the history
…() function for setting the scenario field in log entry events (#667)

* Also improved some tests in logger.test.js to validate that the scenario is set for existing entries in the buffer & newly added entries
  • Loading branch information
rcastanosgonzalez committed Apr 17, 2024
1 parent 8233697 commit f4addfd
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

The most robust logger for Salesforce. Works with Apex, Lightning Components, Flow, Process Builder & Integrations. Designed for Salesforce admins, developers & architects.

## Unlocked Package - v4.13.6
## Unlocked Package - v4.13.7

[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001MkGxQAK)
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001MkGxQAK)
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001MkHRQA0)
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001MkHRQA0)
[![View Documentation](./images/btn-view-documentation.png)](https://jongpie.github.io/NebulaLogger/)

`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y000001MkGxQAK`
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y000001MkHRQA0`

`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y000001MkGxQAK`
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y000001MkHRQA0`

---

Expand Down
14 changes: 14 additions & 0 deletions docs/lightning-components/LogEntryBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [.setMessage(message)](#LogEntryBuilder+setMessage) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.setRecordId(recordId)](#LogEntryBuilder+setRecordId) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.setRecord(record)](#LogEntryBuilder+setRecord) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.setScenario(scenario)](#LogEntryBuilder+setScenario) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.setError(error)](#LogEntryBuilder+setError) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.addTag(tag)](#LogEntryBuilder+addTag) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
- [.addTags(tags)](#LogEntryBuilder+addTags) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
Expand Down Expand Up @@ -65,6 +66,19 @@ Sets the log entry event's record fields
| ------ | ------------------- | ----------------------------------------------------------------------------------------------------- |
| record | <code>Object</code> | The `SObject` record related to the entry. The JSON of the record is automatically added to the entry |

<a name="LogEntryBuilder+setScenario"></a>

### logEntryBuilder.setScenario(scenario) [<code>LogEntryBuilder</code>](#LogEntryBuilder)

Sets the log entry event's scenario field

**Kind**: instance method of [<code>LogEntryBuilder</code>](#LogEntryBuilder)
**Returns**: [<code>LogEntryBuilder</code>](#LogEntryBuilder) - The same instance of `LogEntryBuilder`, useful for chaining methods

| Param | Type | Description |
| -------- | ------------------- | --------------------------------------------------- |
| scenario | <code>String</code> | The string to use to set the entry's scenario field |

<a name="LogEntryBuilder+setError"></a>

### logEntryBuilder.setError(error) [<code>LogEntryBuilder</code>](#LogEntryBuilder)
Expand Down
2 changes: 1 addition & 1 deletion nebula-logger/core/main/logger-engine/classes/Logger.cls
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
global with sharing class Logger {
// There's no reliable way to get the version number dynamically in Apex
@TestVisible
private static final String CURRENT_VERSION_NUMBER = 'v4.13.6';
private static final String CURRENT_VERSION_NUMBER = 'v4.13.7';
private static final System.LoggingLevel FALLBACK_LOGGING_LEVEL = System.LoggingLevel.DEBUG;
private static final List<LogEntryEventBuilder> LOG_ENTRIES_BUFFER = new List<LogEntryEventBuilder>();
private static final String MISSING_SCENARIO_ERROR_MESSAGE = 'No logger scenario specified. A scenario is required for logging in this org.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,10 @@ describe('logger lwc import tests', () => {
const logger = await createLogger();
const scenario = 'some scenario';
const message = 'some message';

const firstLogEntry = logger.finest(message).getComponentLogEntry();
await flushPromises();
expect(firstLogEntry.scenario).toBeNull();
expect(logger.getBufferSize()).toEqual(1);
logger.setScenario(scenario);
const secondLogEntry = logger.info(message).getComponentLogEntry();
await flushPromises();
expect(secondLogEntry.scenario).toBeNull();
expect(logger.getBufferSize()).toEqual(2);

await logger.setScenario(scenario);

expect(firstLogEntry.scenario).toEqual(scenario);
expect(secondLogEntry.scenario).toEqual(scenario);
Expand Down Expand Up @@ -519,16 +513,12 @@ describe('logger lwc legacy markup tests', () => {
const logger = createElement('c-logger', { is: Logger });
document.body.appendChild(logger);
await flushPromises();
const scenario = 'some scenario';
const message = 'some message';
const firstLogEntry = await logger.finest(message).getComponentLogEntry();
expect(firstLogEntry.scenario).toBeNull();
expect(logger.getBufferSize()).toEqual(1);
const secondLogEntry = await logger.info(message).getComponentLogEntry();
expect(secondLogEntry.scenario).toBeNull();
expect(logger.getBufferSize()).toEqual(2);

const scenario = 'another scenario';
const firstLogEntry = logger.finest(message).getComponentLogEntry();
logger.setScenario(scenario);
const secondLogEntry = logger.info(message).getComponentLogEntry();

expect(firstLogEntry.scenario).toEqual(scenario);
expect(secondLogEntry.scenario).toEqual(scenario);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//------------------------------------------------------------------------------------------------//
import FORM_FACTOR from '@salesforce/client/formFactor';

const CURRENT_VERSION_NUMBER = 'v4.13.6';
const CURRENT_VERSION_NUMBER = 'v4.13.7';

// JavaScript equivalent to the Apex class ComponentLogger.ComponentLogEntry
const ComponentLogEntry = class {
Expand Down Expand Up @@ -78,6 +78,16 @@ const LogEntryBuilder = class {
return this;
}

/**
* @description Sets the log entry event's scenario field
* @param {String} scenario The string to use to set the entry's scenario field
* @return {LogEntryBuilder} The same instance of `LogEntryBuilder`, useful for chaining methods
*/
setScenario(scenario) {
this.#componentLogEntry.scenario = scenario;
return this;
}

/**
* @description Sets the log entry event's exception fields
* @param {Error} error The instance of a JavaScript `Error` object to use, or an Apex HTTP error to use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,9 @@ const LoggerService = class {
}

_newEntry(loggingLevel, message) {
// Builder is returned immediately but console log will be determined after loading settings from server
const logEntryBuilder = newLogEntry(loggingLevel, this.#settings?.isConsoleLoggingEnabled);
logEntryBuilder.setMessage(message);
if (this.#scenario) {
logEntryBuilder.scenario = this.#scenario;
}
logEntryBuilder.setScenario(this.#scenario);
if (this._meetsUserLoggingLevel(loggingLevel)) {
this.#componentLogEntries.push(logEntryBuilder.getComponentLogEntry());
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nebula-logger",
"version": "4.13.6",
"version": "4.13.7",
"description": "The most robust logger for Salesforce. Works with Apex, Lightning Components, Flow, Process Builder & Integrations. Designed for Salesforce admins, developers & architects.",
"author": "Jonathan Gillespie",
"license": "MIT",
Expand Down
7 changes: 4 additions & 3 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"path": "./nebula-logger/core",
"definitionFile": "./config/scratch-orgs/base-scratch-def.json",
"scopeProfiles": true,
"versionNumber": "4.13.6.NEXT",
"versionName": "View Log Entry Metadata Custom Permission",
"versionDescription": "Added new custom permission CanViewLogEntryMetadata so that users without query access to ApexClass and ApexTrigger can still see the source code in the LWC logEntryMetadataViewer",
"versionNumber": "4.13.7.NEXT",
"versionName": "Fixed function setScenario() in logger LWC",
"versionDescription": "Fixed an issue in the logger LWC's function setScenario() where the value was sometimes (often?) incorrectly set to null",
"releaseNotesUrl": "https://github.com/jongpie/NebulaLogger/releases",
"unpackagedMetadata": {
"path": "./nebula-logger/extra-tests"
Expand Down Expand Up @@ -174,6 +174,7 @@
"Nebula Logger - Core@4.13.4-logger-parameter-comments": "04t5Y000001MkFBQA0",
"Nebula Logger - Core@4.13.5-performance-improvements": "04t5Y000001MkGnQAK",
"Nebula Logger - Core@4.13.6-view-log-entry-metadata-custom-permission": "04t5Y000001MkGxQAK",
"Nebula Logger - Core@4.13.7-fixed-function-setscenario()-in-logger-lwc": "04t5Y000001MkHRQA0",
"Nebula Logger - Core Plugin - Async Failure Additions": "0Ho5Y000000blO4SAI",
"Nebula Logger - Core Plugin - Async Failure Additions@1.0.0": "04t5Y0000015lhiQAA",
"Nebula Logger - Core Plugin - Async Failure Additions@1.0.1": "04t5Y0000015lhsQAA",
Expand Down

0 comments on commit f4addfd

Please sign in to comment.