log() method from the lightning/logger module in the Salesforce Lightning Web Component can be used to log custom messages to Event Monitoring.
Enable Lightning Logger Events should be enabled in the Event Monitoring Settings in Salesforce Setup.
Sample Lightning Web Component:
HTML:
<template>
<lightning-card>
<div class="slds-p-around_medium">
<lightning-button
label="Example"
onclick={handleClick}>
</lightning-button>
</div>
</lightning-card>
</template>
JavaScript:
import { LightningElement } from 'lwc';
import { log } from 'lightning/logger';
import USERID from "@salesforce/user/Id";
export default class SampleLightningWebComponent extends LightningElement {
userId = USERID;
handleClick() {
let msg = {
type: "click",
action: "Example",
user: this.userId
}
console.log(
'msg is',
JSON.stringify( msg )
);
log( msg );
}
}
js-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__Tab</target>
</targets>
</LightningComponentBundle>
Use the Event Log File Browser in the Salesforce Setup to download the Event Log Files.
Output:
SOQL:
SELECT Id, EventType, CreatedDate,
LogFileLength, LogDate, ApiVersion,
LogFileContentType, Sequence
FROM EventLogFile
WHERE Interval = 'Hourly'
AND EventType = 'LightningLogger'