Log messages using LWC with lightning/logger
The lightning/logger module offers a convenient way to enhance the observability of your Lightning web components by using the Custom Component Instrumentation API. You can use the log() function to create custom messages and log them from your components to Event Monitoring. It is important to note that lightning/logger can only be used in the Lightning Experience, and is not available in the Salesforce mobile app. To use lightning/logger, you must ensure that Event Monitoring is enabled in your org and that the "Enable Lightning Logger Events" toggle is set to On. The lightning/logger module has only one method, log().
This isn't supported for Aura components
According to Salesforce documentation, this change is only available for customers who have purchased Salesforce Shield or Salesforce Event Monitoring add-on subscriptions.
Enable Event Monitoring Settings
Here's an example from Salesforce
In JavaScript, import the log from lightning/logger and use it in the handleClick function. In this example, clicking on the button creates an object that is passed to Event Monitoring.The following example logs a click from within a button component. The log() function can be used for any event.
LWC HTML
<template><lightning-button label="Approve" onclick={handleClick}>
</lightning-button>
</template>
LWC JS
import { LightningElement } from 'lwc';import { log } from 'lightning/logger';
export default class HelloWorld extends LightningElement {
handleClick() {
let msg = {
type: "click",
action: "Approve"
}
log(msg);
}
}
Once an Event Log is created from LWC, using the following query,
SELECT Id, EventType, CreatedDate, LogFileLength, LogDate, ApiVersion, LogFileContentType, Sequence, Interval, LogFile FROM EventLogFile WHERE Interval = 'Hourly' AND EventType = 'LightningLogger'
We can use the ELF Browser application by clicking this link: https://salesforce-elf.herokuapp.com. Note: Salesforce Event Log File Browser is not an official Salesforce tool.
Resources
lightning/logger Documentation
Get Started with Event Monitoring
Monitor Component Events with Custom Component Instrumentation API


Comments
Post a Comment