A single, configurable logging framework for Apex, Triggers, Flows, LWC, and Aura — designed to standardise error handling, performance tracking, and operational observability in Salesforce orgs.
Overview
This Universal Logging Framework provides a centralised way to capture logs across the Salesforce platform. It standardises log levels, groups logs by transaction, supports intelligent buffering for performance, and captures rich context (user, record, component, custom metadata). It is suitable for production systems where consistent observability and traceability are required.
Key Features
- Universal Coverage: Apex, Triggers, Flows, Lightning Web Components (LWC), and Aura.
- Configurable Log Levels: DEBUG, INFO, WARN, ERROR, FATAL.
- Transaction Tracking: Unique transaction IDs to correlate related logs.
- Buffer Management: Batching and flush controls to reduce DML overhead.
- Performance & Error Monitoring: Built-in performance logging and stack trace capture.
Setup Summary
The framework requires a custom object Application_Log__c with fields for level, source, component, message, stack trace, user, record id, transaction id and additional JSON data. Deploy supporting Apex classes (Logger, TriggerLogger, FlowLogger, LoggerController), create an LWC wrapper, and set object/class/flow permissions for users who will generate logs.
Example: Apex Usage
Use the Logger methods to record events and errors. Remember to flush at logical boundaries (batch end, end of trigger, or periodic).
public class ExampleUsage {
public void demonstrateLogging() {
// Simple logging
Logger.info('Process started');
Logger.debug('ExampleUsage.demonstrateLogging', 'Debug information');
try {
// Some business logic
performBusinessLogic();
Logger.info('ExampleUsage.demonstrateLogging', 'Business logic completed successfully');
} catch (Exception ex) {
Logger.error('ExampleUsage.demonstrateLogging', ex);
Logger.fatal('ExampleUsage.demonstrateLogging', 'Critical error occurred');
} finally {
Logger.flush(); // Ensure logs are written
}
}
private void performBusinessLogic() {
// Simulate some processing
Logger.debug('ExampleUsage.performBusinessLogic', 'Starting business logic');
// Simulate an error
throw new CalloutException('Simulated error for demonstration');
}
}Example: LWC Integration
Use the client-side logger to emit logs from components and track performance.
import { LightningElement, api, track } from 'lwc';
import logger from 'c/logger';
export default class ExampleLogComponent extends LightningElement {
@api recordId;
@track data = [];
@track error;
async connectedCallback() {
logger.setComponentName('ExampleLogComponent');
await logger.info('Component initialized', this.recordId, {
timestamp: new Date().toISOString(),
userAgent: navigator.userAgent
});
}
disconnectedCallback() {
logger.logSync('INFO', 'Component disconnected', this.recordId);
}
}
Flows
Create an Action element in Flow and call the provided invocable method “Log Message” with inputs like Flow Name, Level, Message and optional Record Id.
Configuration & Best Practices
- Set MIN_LOG_LEVEL in Logger to control minimum persisted logs (e.g., INFO for production).
- Tune buffer size (MAX_BUFFER_SIZE) to balance DML overhead and memory.
- Always avoid logging sensitive data (PII, passwords) — mask or omit such fields.
- Flush logs at logical boundaries: end of trigger, after each batch, or periodically in long-running processes.
- Use meaningful messages and include context (class.method, record id, user) for faster troubleshooting.
Monitoring, Reporting & Integration
Create reports and dashboards on the Application_Log__c object: error trend reports, performance dashboards, and component health overviews. For advanced analytics, export logs to external systems (Splunk, ELK) via callouts.
Sample SOQL
SELECT Id, Level__c, Source__c, Class_Method__c, Message__c, CreatedDate, User__r.Name FROM Application_Log__c WHERE Level__c = 'ERROR' AND CreatedDate = LAST_N_DAYS:1 ORDER BY CreatedDate DESC
Troubleshooting & Maintenance
Common issues include missing permissions, high log volume, and performance impact. Implement automated cleanup policies (e.g., delete logs older than 90 days) and consider archiving to external storage for compliance.
Why this matters for Salesforce teams
Consistent, platform-wide logging improves incident response, reduces mean time to resolution (MTTR), and provides visibility for performance tuning. For admins and developers, a shared logging standard simplifies debugging across Apex, Flows, and UI components; for business users, it means fewer disruptions and clearer root-cause explanations.








Leave a Reply