New synchronous JavaScript function getLogger() + deprecated async function createLogger()
This release is focused on several enhancements & bugfixes for logging in JavaScript (aura & lightning web components). Thanks to @jamessimone for reviewing & discussing the changes for this release, and thanks to @srikanth3107 for reporting issue #776!
Core Unlocked Package Changes
New getLogger()
function
Partially fixed #728 by adding a new function getLogger()
in logger
LWC that can be called synchronously, and deprecated the async function createLogger()
- This simplifies how developers use the
logger
LWC, and avoids some lingering JavaScript (JS) stack trace issues that occur inasync
functions - The function
createLogger()
is still supported & functional, but it's now considered deprecated since it requires usingawait
(which is no longer necessary withgetLogger()
)
Note
This does not truly "fix" or improve the stack trace parsing used in async
functions, and async
functions will continue to have inaccurate function names reported in some browsers (typically, eval
is listed as the function name). This is a ongoing challenge in JavaScript due to what info is/isn't available in stack traces in some browsers (most notably, webkit browsers like Chrome & Edge).
But with the new getLogger()
function, Nebula Logger no longer requires the use of async
/ await
- and often the only reason developers were using async
/ await
was to be able to use Nebula Logger. In these situations, developers can eliminate async
/ await
, and the resulting log entries will have accurate JS function names.
Example: Old createLogger()
Usage
Previously, JS developers had to use async
, await
, and truthy checks to ensure that Nebula Logger's LWC had finished being loaded. This resulted in more complexity for developers, who just want to be able to log some stuff 😢
import { createLogger } from 'c/logger';
export default class LoggerDemo extends LightningElement {
logger;
// Some lifecycle event (typically connectedCallback()) has to run async & await createLogger()
async connectedCallback() {
this.logger = await createLogger();
this.logger.info('Hello, world');
this.logger.saveLog();
}
// @wire functions run around the same time as connectedCallback(), but there's no guaranteed order
// This result in some logging requiring truthy checks using the safe navigator "?.", and some loss of logging data could occur
@wire(returnSomeString)
wiredReturnSomeString({ error, data }) {
this.logger?.info('>>> logging inside a wire function, if the logger is loaded');
if (data) {
this.logger?.info('>>> wire function return value: ' + data);
}
if (error) {
this.logger?.error('>>> wire function error: ' + JSON.stringify(error));
}
}
}
Example: New getLogger()
Usage
Now, await
is no longer needed, and a logger
instance can be immediately initialized & used. This results in less code, and happier developers 🥳
import { getLogger } from 'c/logger';
export default class LoggerDemo extends LightningElement {
logger = getLogger();
connectedCallback() {
// Immediately use this.logger - no await, no truthy checks
this.logger.info('Hello, world');
this.logger.saveLog();
}
}
@wire(returnSomeString)
wiredReturnSomeString({ error, data }) {
// Immediately use this.logger - no await, no truthy checks
this.logger.info('>>> logging inside a wire function');
if (data) {
this.logger.info('>>> wire function return value: ' + data);
}
if (error) {
this.logger.error('>>> wire function error: ' + JSON.stringify(error));
}
}
}
New exception()
function
Resolved #763 by adding a JS function equivalent to the Apex method Logger.exception()
. Both of these do 3 things in 1 line of code:
- Log an exception
- Save the log
- Rethrow the exception
Previously in JS, this would have been 3 lines of code:
this.logger.error('An error occurred').setExceptionDetails(someJSError);
this.logger.saveLog();
throw someJSError;
Now, 1 line of code provides the same functionality:
this.logger.exception('An error occurred', someJSError);
More Improvements for JS Stack Trace Parsing
Fixed #776 by updating logic in loggerStackTrace.js
to better handle parsing when lightning debug mode is disabled
Previously, stack traces worked when debug mode was enabled, but was still inaccurate in some browsers when debug mode was off due to some subtle differences in the generated stack traces.
Installation Info
Core Unlocked Package - no namespace
Full Changelog: v4.14.12...v4.14.13
- SF CLI:
sf package install --wait 20 --security-type AdminsOnly --package 04t5Y0000015oW3QAI
- SFDX CLI:
sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y0000015oW3QAI
- Sandbox: https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oW3QAI
- Production: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015oW3QAI