Skip to content

Commit

Permalink
Improved handling of log entries to save in saveLog() JS function
Browse files Browse the repository at this point in the history
  • Loading branch information
jongpie committed Sep 30, 2024
1 parent fc66148 commit 8967ed2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getLogger, createLogger } from 'c/logger';
// Legacy markup-based approach
import Logger from 'c/logger';
import getSettings from '@salesforce/apex/ComponentLogger.getSettings';
import saveComponentLogEntries from '@salesforce/apex/ComponentLogger.saveComponentLogEntries';

const MOCK_GET_SETTINGS = require('./data/getLoggerSettings.json');

Expand All @@ -26,6 +27,16 @@ jest.mock(
{ virtual: true }
);

jest.mock(
'@salesforce/apex/ComponentLogger.saveComponentLogEntries',
() => {
return {
default: jest.fn()
};
},
{ virtual: true }
);

describe('logger lwc recommended sync getLogger() import approach tests', () => {
beforeEach(() => {
// One of logger's features (when enabled) is to auto-call the browser's console
Expand Down Expand Up @@ -591,15 +602,22 @@ describe('logger lwc recommended sync getLogger() import approach tests', () =>
// before some sync tasks are executed
await flushPromises('Resolve async task queue');
await logger.getUserSettings({ forceReload: true });
logger.info('example INFO log entry');
logger.debug('example DEBUG log entry');
const firstEntryBuilder = logger.info('example INFO log entry added BEFORE saveLog()');
const secondEntryBuilder = logger.debug('example DEBUG log entry added BEFORE saveLog()');
await flushPromises();
expect(logger.getBufferSize()).toBe(2);

logger.saveLog();
logger.warn('example WARN log entry added AFTER saveLog()');

await flushPromises('Resolve async task queue');
expect(logger.getBufferSize()).toBe(0);
expect(logger.getBufferSize()).toBe(1);
expect(saveComponentLogEntries).toHaveBeenCalledTimes(1);
const expectedApexParameter = {
componentLogEntries: [firstEntryBuilder.getComponentLogEntry(), secondEntryBuilder.getComponentLogEntry()],
saveMethodName: undefined
};
expect(saveComponentLogEntries.mock.calls[0][0]).toEqual(expectedApexParameter);
});
});

Expand Down Expand Up @@ -1107,16 +1125,22 @@ describe('logger lwc deprecated async createLogger() import tests', () => {
it('saves log entries and flushes buffer when using deprecated async createLogger() import approach', async () => {
getSettings.mockResolvedValue({ ...MOCK_GET_SETTINGS });
const logger = await createLogger();
await logger.getUserSettings({ forceReload: true });
logger.info('example INFO log entry');
logger.debug('example DEBUG log entry');
const firstEntryBuilder = logger.info('example INFO log entry added BEFORE saveLog()');
const secondEntryBuilder = logger.debug('example DEBUG log entry added BEFORE saveLog()');
await flushPromises();
expect(logger.getBufferSize()).toBe(2);

logger.saveLog();
logger.warn('example WARN log entry added AFTER saveLog()');

await flushPromises();
expect(logger.getBufferSize()).toBe(0);
await flushPromises('Resolve async task queue');
expect(logger.getBufferSize()).toBe(1);
expect(saveComponentLogEntries).toHaveBeenCalledTimes(1);
const expectedApexParameter = {
componentLogEntries: [firstEntryBuilder.getComponentLogEntry(), secondEntryBuilder.getComponentLogEntry()],
saveMethodName: undefined
};
expect(saveComponentLogEntries.mock.calls[0][0]).toEqual(expectedApexParameter);
});
});

Expand Down Expand Up @@ -1647,17 +1671,23 @@ describe('logger lwc legacy markup tests', () => {
getSettings.mockResolvedValue({ ...MOCK_GET_SETTINGS });
const logger = createElement('c-logger', { is: Logger });
document.body.appendChild(logger);
await flushPromises();
const settings = await logger.getUserSettings({ forceReload: true });
expect(settings.isEnabled).toEqual(true);
logger.info('example INFO log entry');
logger.debug('example DEBUG log entry');
await flushPromises('Resolve async task queue');
await logger.getUserSettings({ forceReload: true });
const firstEntryBuilder = logger.info('example INFO log entry added BEFORE saveLog()');
const secondEntryBuilder = logger.debug('example DEBUG log entry added BEFORE saveLog()');
await flushPromises();
expect(logger.getBufferSize()).toBe(2);

logger.saveLog();
logger.warn('example WARN log entry added AFTER saveLog()');

await flushPromises();
expect(logger.getBufferSize()).toBe(0);
await flushPromises('Resolve async task queue');
expect(logger.getBufferSize()).toBe(1);
expect(saveComponentLogEntries).toHaveBeenCalledTimes(1);
const expectedApexParameter = {
componentLogEntries: [firstEntryBuilder.getComponentLogEntry(), secondEntryBuilder.getComponentLogEntry()],
saveMethodName: undefined
};
expect(saveComponentLogEntries.mock.calls[0][0]).toEqual(expectedApexParameter);
});
});
26 changes: 14 additions & 12 deletions nebula-logger/core/main/logger-engine/lwc/logger/loggerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,22 @@ const LoggerService = class {
* on subsequent saveLog() calls
*/
async saveLog(saveMethodName) {
const saveLogTask = async providedSaveMethodName => {
if (this.#componentLogEntries.length === 0) {
return;
}
if (this.#componentLogEntries.length === 0) {
return;
}

const logEntriesToSave = [...this.#componentLogEntries];
this.flushBuffer();

const saveLogTask = async (providedComponentLogEntries, providedSaveMethodName) => {
if (!providedSaveMethodName && this.#settings?.defaultSaveMethodName) {
providedSaveMethodName = this.#settings.defaultSaveMethodName;
}

try {
const logEntriesToSave = [...this.#componentLogEntries];
// this is an attempt to only flush the buffer for log entries that we are sending to Apex
// rather than any that could be added if the saveLog call isn't awaited properly
this.flushBuffer();
await saveComponentLogEntries({
componentLogEntries: logEntriesToSave,
providedSaveMethodName
componentLogEntries: providedComponentLogEntries,
saveMethodName: providedSaveMethodName
});
} catch (error) {
if (this.#settings.isConsoleLoggingEnabled === true) {
Expand All @@ -103,8 +102,9 @@ const LoggerService = class {
throw error;
}
};
this.#taskQueue.enqueueTask(saveLogTask, saveMethodName);
this.#taskQueue.executeTasks();

this.#taskQueue.enqueueTask(saveLogTask, logEntriesToSave, saveMethodName);
await this.#taskQueue.executeTasks();
}

async _loadSettingsFromServer() {
Expand All @@ -122,6 +122,7 @@ const LoggerService = class {
throw error;
}
};

this.#taskQueue.enqueueTask(loadSettingsTask);
await this.#taskQueue.executeTasks();
}
Expand All @@ -144,6 +145,7 @@ const LoggerService = class {
}
}
};

this.#taskQueue.enqueueTask(loggingLevelCheckTask, loggingLevel);
this.#taskQueue.executeTasks();

Expand Down

0 comments on commit 8967ed2

Please sign in to comment.