lightning/logger - Add Observability to your custom Lightning Web Components | Salesforce ☁️⚡️


 lightning/logger it's generally available to use now and can be used to add observability to your custom Lightning Web Components.

lightning/logger is available only in Lightning Experience and not in the Salesforce mobile app. To use lightning/logger, make sure Event Monitoring is enabled in your org and the "Enable Lightning Logger Events" toggle is set to On.

lightning/logger has one method, log().

log()

Logs the event data from the component.

PARAMETERTYPEDESCRIPTION
messagestring or objectThe message to be logged to the Lightning Logger Event Type in Event Monitoring. The maximum string length is 4096 characters. The log() function automatically stringifies an object passed in.

Turn on Event Monitoring Settings
From Setup, in the Quick Find box, enter event, and then select Event Monitoring Settings. Turn on Lightning Logger Events.



To add lightning logger in your LWC, you can simply import it from lightning/logger library as shown in the example below.

instrumentationAPI.html
<template>
    <lightning-card title="lightning/logger" icon-name="utility:user">
        <lightning-button label="Click Me" onclick={handleClick}></lightning-button>
    </lightning-card>
</template>

instrumentationAPI.js
import { LightningElement } from 'lwc';
import { log } from 'lightning/logger'

export default class InstrumentationAPI extends LightningElement {
    handleClick() {
        let msg = {
            type: "Click",
            action:"Approve"
        }
        log(msg);
        console.log(JSON.stringify(msg));
    }
}

Output



Checkout Complete Video Tutorial Below



 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments