Advanced Logging
Salesforce provides some valuable logging capabilities out of the box. To learn more about
the built in Apex Debug Logs, see the documentation here.
Built in logging
The Apex Debug logging is very useful, but has certain limitations
- A developer must manually enable the logging. Note: opening the developer tools will also
turn on logging for the current user temporarily.
- Logging is only enabled for a maximum of 24 hours.
- Logging must be enabled per user.
- The desired information may be logged into many separate log files; each log must be opened
individually to find the desired data.
- Salesforce limits the size of each log file, and further limits the total log size.
- It a bad practice to swallow exceptions, however an error may never be seen if
it is only written to an Apex Debug log. :-(
Developers from other platforms may be accustomed to having the ability to write a
persistent log at will, and may be surprised to learn that Salesforce doesn't have this ability
natively.
Advanced Logging
To have a more advanced logging system, we require certain features:
- Logs should persist and be available at anytime.
- Logs should be searchable.
- Logs should be written even if an error roles back the transaction.
- Errors should not increase per-transaction limits (e.g. maximum DML counts).
- Logs should have a way of filtering sensitive info; I've seen logs that inadvertently
stored credit card info, social security numbers etc.
- We should allow different levels of logging with the ability to configure which levels are
actually logged at any time. One might log different levels in a sandbox vs a production
org.
To see useful code for advanced persistent logs, please see this github repository.
For this project, we've implemented the following features:
- Writes log data to Salesforce database for convenient access with reports and SOQL
- Logs are written in a manner so DML is not added to the current transaction, and there
is no
rollback when errors occur
- Provides both static methods, and a concrete class that implements an interface
developers
that might want to mock logging in tests, or provide other loggers.
- Configure data that should be filtered out of logs, e.g. Social Security numbers.
- Configure which entries are logged by LoggingLevel/User
- Log from ProcessBuilders