What is Lightning Logger in LWC with Example

Lightning Logger in Salesforce

Salesforce development has evolved significantly, and debugging Lightning Web Components has evolved with it. For many years, developers relied on console.log() to understand client-side behavior. Salesforce has now introduced Lightning Logger, a more capable logging option designed for production environments. It captures client-side events, performance data, and debugging information and stores them as Event Log Files in Salesforce.

Lightning Logger provides a reliable and secure way to capture logs from Lightning Web Components. Instead of depending only on browser-based tools, developers can review these logs directly in Salesforce when checking issues, diagnosing problems, or supporting audit requirements.

This blog explains what Lightning Logger is, how to enable it, how to retrieve and view logs, and its usage within an LWC.

What Is a Lightning Logger?

Lightning Logger is a logging framework made particularly for Lightning Web Components. It will resolve the gaps of old console logging by connecting client-side activity with server-side log storage.

Unlike console.log() output, which disappears when the browser is refreshed or closed, Lightning Logger records events that remain available for later review, including days or weeks after they occur.

This persistence makes it possible to investigate issues that are difficult to reproduce and to analyze behavior across users and sessions.

How to Enable Lightning Logger?

  • Open Setup and search for Event Monitoring Settings.
  • Enable Lightning Logger Events and enable Generate Event Log Files .
  • Save the changes.

Users generating logs need the following permissions:

  • View Event Log Files permission

  • API Enabled

Lightning Logger in LWC

Once enabled, Salesforce starts creating LightningLogger event records in the EventLogFile object whenever your LWC code calls the log() function from lightning/logger. You can then access these records via SOQL, Event Log File Browser, API tools, or analytics platforms.

Lightning Logger Syntax

Using Lightning Logger in LWC is straightforward. Import the function and call log() with either a string or a structured object.

Example:

HTML

<template>
<lightning-card title="Lightning Logger Demo">
<div class="slds-p-around_medium">
<lightning-button
label="Log Event"
variant="brand"
onclick={handleClick}>
</lightning-button>
</div>
</lightning-card>
</template>

JS

import { LightningElement } from 'lwc';
import { log } from 'lightning/logger';
export default class EventLogger extends LightningElement {
handleClick() {
log('Button clicked in EventLogger component');
const payload = {
type: 'click',
action: 'primary_button',
page: 'Home',
timestamp: new Date().toISOString()
};
log(payload);
console.log('Event logged using lightning/logger');
}
}

Meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>

Lightning Logger Demo

Where to See Lightning Logger’s Output?

Lightning Logger output can be checked in different places depending on whether the focus is on local debugging or organisation-wide analysis.

Different Ways

  • Browser console: When Lightning Debug Mode is turned on, Lightning Logger messages also viewed from the browser’s developer console.
  • Event Log File Browser: Admins can view Lightning Logger event types and download CSV files using the Event Log File Browser application, where available.
  • External tools and APIs: Records with EventType = ‘LightningLogger’ can be queried from SOQL or REST APIs and they can be studied using tools such as Splunk, Tableau, or CRM Analytics.

Csv file downloaded from Event log File Browser.

SOQL to Retrieve Lightning Logger Logs

Lightning Logger data is saved in the EventLogFile object under the LightningLogger event type. The usual way is to query recent records and then download and parse the associated log files.

Example

SELECT Id, EventType, CreatedDate, LogDate, LogFileLength, ApiVersion, Interval, Sequence,
LogFileContentType, LogFile FROM EventLogFile WHERE EventType = 'LightningLogger' AND Interval =
'Hourly' ORDER BY LogDate DESC, Sequence DESC

 

Console Logs vs Lightning Logger

Lightning Logger does not replace regular console logging statements like console.log, console.error. It upgrades it. Console logs are useful for immediate, local debugging and can be seen only by the developer during a browser session. They are not stored and cannot be reviewed for future use.

Lightning Logger records events in Event Log Files that can be queried using SOQL, downloaded, and reviewed across the organization like other objects. This makes it a fitting solution for investigating production issues, assessing activity, and analyzing usage patterns.

Uses

  • User interaction tracking: Keep a record of clicks, selections, or navigation operations (e.g., which filters the users applied) to analyze usage patterns and improve the UX.
  • Error diagnostics: When exceptions arise in LWCs, gather the error messages, stack traces, and surrounding data, and then associate them with Apex errors or integration failures.
  • Compliance and auditing: Keep an eye on the access routes to sensitive records or features by capturing the main front‑end events for logging purposes and reviewing them later in the Event Monitoring dashboards.
  • Performance monitoring: UI changes or crossing of thresholds can be recorded along with timings and then this information can be pooled externally to locate slow user flows.

Limitations

  • Lightning Logger does not support standard log levels like debug, info, warn, error, or critical, its single log() function treats all messages uniformly without severity parameters.
  • The lightning logger module is explicitly available only in Lightning Experience on desktop/tablet browsers and is not supported in the Salesforce mobile app (iOS or Android).
  • Lightning logger is available only in Lightning Experience and not in the Salesforce mobile app. This restriction applies even if Event Monitoring is enabled, as mobile uses a separate native rendering engine without full LWC utility module support.
  • Log file generation in the Event log file browser and SOQL data won’t generate instantly it takes a few hours to generate.

Also Read – SOQL Scenario-Based Interview Questions and Answers

FAQs

1. What is the purpose of the lightning data service in LWC?

Lightning Data Service enables you to load, create, update, or delete records directly within your component without writing Apex, while automatically respecting the user’s sharing rules and field-level security.

2. What is Lightning Logger in LWC?

Lightning Logger is a powerful logging framework built specifically for Lightning Web Components. Instead of relying on console.log(), which disappears when the page is refreshed or the browser is closed, Lightning Logger stores logs persistently on the server.

This creates a bridge between client-side errors and server-side tracking, making it much easier to debug issues in production. Teams can review logs even days or weeks later, helping them understand what went wrong and fix problems faster.

Conclusion

Lightning Logger brings a much-needed upgrade to client-side debugging in Salesforce. It helps developers capture meaningful LWC logs in a reliable and persistent way, making it easier to troubleshoot production issues and understand user behavior. While console logs still have their place during development, Lightning Logger is the better choice when visibility, auditability, and long-term analysis matter.

Get a complete Roadmap to Learn Salesforce Admin and Development👇

Share Now

Learn Salesforce Flows 👇 Beginner-friendly course | More than 90+ tutorials

Prepare for PD1 Exam 3 Mock Practice Sets to prepare for the exam

Book a 1:1 Call 👇 Doubt related to Salesforce Flow?

Categories

What’s Trending