Frequently in Salesforce – we need to log something. Salesforce logs a lot of things for us automatically, out of the box, but when we build custom functionality using Apex or Flow, we should log these processes and store them for analysis later.
This can be very helpful for troubleshooting, analytics and compliance requirements.
In this blog, we’ll explore what to log, how to log, and look at what is required to ensure logging is scalable and follows industry best practice.
What should we log?
At a high level, we should always log events that may be helpful in understanding what exactly our automation is doing – especially if it fails!
- General Application Errors
Capture details of any exceptions or errors that occur within your application. This includes:
Apex Errors: Log exceptions thrown in Apex classes and triggers.
LWC Errors: Log exceptions that happen in JavaScript.
Flow Faults: Log faults and errors that occur in Salesforce Flows.
- Integration Results
Log the success or failure of integrations with external systems. You should be able to see the results of each request and response in both inbound contexts and outbound contexts.
How should I log?
Salesforce provides logging functionality out of the box, but these are limited and shouldn’t be relied on for anything other than incidental, first-line debugging.
Some organisations decide to roll their own logging solution – while generally unadvisable, this approach can be useful in restricted environments where a third-party package cannot be used. In these instances, a custom-built solution might make sense.
However you decide to log, you should be logging in a structured format. This makes it easier to parse logs and feed them into analytics, which is really helpful for spotting trends or patterns within your logs.
You should also ensure you log the minimum required information to stay compliant with your local laws and regulations.
I always recommend the following package for logging in Salesforce, due to the excellent features and reduced effort versus building your own: