Salesforce is a great company with excellent products and an even better platform. One area that needs more attention is knowing what’s happening in your Salesforce org without enabling Salesforce debug logs. Has a client or a user contacted you saying X is not working and they don’t know what’s going on so you turn on the debug logs hoping for more information and it’s not helpful?

It happens more often than not and troubleshooting it is hard.

Meet Salesforce Logger

This new Logging framework allows one to add logging to their customizations which can then be saved to one or more logging repositories, based on how you configure it, as the customizations are run. Out of the box, it supports saving logs to the debug log, a log custom object, and/or to Loggly, a separate SAAS logging service. If more is needed, one can implement their own persistence logic to save logs wherever they’re needed.

This allows one to monitor the logs to see what errors are happening and how the customizations are being used or not.

Pilot

I’m currently offering this as a free Beta package to pilot testers so they can test it out and provide feedback. If you’re interested in being a pilot tester, follow the instructions below to install, configure, and use the Salesforce Logger Beta package.

When you’re done, Contact Me about what’s good, what needs improvement, how much you’re willing to pay to use this package in production, and any other feedback you’d like to give.

Installing the Beta

Developer Edition Installation URL – Use this for an org created through developer.salesforce.com or the partner portal.

Sandbox Installation URL – Use this for sandboxes.

Note: This can’t be installed into production because it’s a Beta Package and Beta packages can’t be upgraded.

After clicking the installation link, you’ll see this:

Ensure “Install for Admins Only” is selected and click Install. The package is designed to work for all users even though “Admins Only” is chosen.

Check “yes, grant access to these third-party web sites” so that the package can save logs to Loggly, if configured to do so, and then click Continue. The package is now installing.

It takes a little while for it to install but then you should see the installation completed screen:

Configuration

Configuring Salesforce Logger is done through Log Configuration records. Each record specifies an apex class that is used to save logs to somewhere and whether it’s Enabled or not. This allows one to specify one or more places where the logs are saved.

In the current version, one can save logs to the Salesforce debug log, a Log custom object that’s included in the package, and to Loggly which is an external Logging service that has a free and paid tier. If other options are needed, additional code can be created to save the logs elsewhere. See “Creating your own Log Saver” below for more info.

Saving To the Debug Log

First, copy the CustomDebugLogSaver code below and create the class in your org.

Now, the Log Configuration has to be created so that logs can use this class to save logs to the debug log.

  1. Open the Log Configurations tab.
  2. Click New.
  3. Enter “CustomDebugLogSaver” for the Log Saver Class field.
  4. Click Save.

Congratulations, now any logs will show up in the debug logs when the debug logs are enabled.

Saving to the Log Custom Object

The package has a “Log” custom object that lets logs to be saved as records in Salesforce. To enable this:

  1. Open the Log Configurations tab.
  2. Click New.
  3. Enter “mtl_sflog.LogObjectLogSaver” for the Log Saver Class field.
  4. Click Save.

Log Custom Object Example

Saving to Loggly

Loggly is an external, centralized logging service that lets one aggregate their logs from multiple sources to one location and then search, report, and send alerts when various logs are seen. They have a free version that lets one log up to 200 MB per day and have that retained for up to 7 days. They have other paid plans that offer varying amounts of log usage per day and retention periods.

To enable this:

  1. Create an account at Loggly.
  2. After signing in, click “Source Setup” at the top.
  3. Click “Customer Tokens”.
  4. Copy the customer token as it will be used later.
  5. Open the Log Configurations tab.
  6. Click New.
  7. Enter “mtl_sflog.LogglyLogSaver” for the Log Saver Class field.
  8. Replace <token> in the https://logs-01.loggly.com/bulk/<token>/tag/bulk/ url using the customer token and copy it into the Endpoint field. For example, if the customer token is af918, enter “https://logs-01.loggly.com/bulk/af918/tag/bulk/” as the endpoint.
  9. Click Save.

Loggly Example

Disabling Log Savers

To disable logging,

  1. Open the Log Configurations tab.
  2. Edit the log saver record to disable.
  3. Change the status to “Disabled”.
  4. Save.

Note: Disabling all the logs savers will prevent any logs from being saved even though the Logging code is in use.

Using Salesforce Logger

After the package is installed and configured, one uses the Logger apex class to add logs and then save them to the configured places.

The general flow is to add one or more logs using the Logger apex class and then save them as needed when desired.

Logging an Info Log

Let’s see an example of logging an Info Log:

This creates a LogEntry object with the current datetime, a LogLevel of “Info” and a Message of “Some Information”. This LogEntry is then saved to the configured places.

Logging an Error Log

This creates a LogEntry object with the current datetime, a LogLevel of “Error” and a Message of “Some error”. This error LogEntry is then saved to the configured places.

Logging an Exception Log

This creates a LogEntry object with the current datetime, a LogLevel of “Error”, and a formatted error message that is then saved to the configured places.

The formatted message looks like this:

Error Message: <exception message>

Stack Trace: <exception stack trace>

Type Name: <exception type name>

Line Number: <exception line number>

Logging a LogEntry

You can also create your own LogEntry instances and save them as needed. A LogEntry has 3 properties:

1) Message – This is the message to log.

2) LogLevel – A string that represents the log level. This can be any value you’d like.

3) LogDate – The date and time of this log.

The other logging methods are convenience methods that create LogEntry instances for you so only two lines of code are needed.

Creating your own Log Saver

Saving logs to a custom location involves writing your own “LogSaver” class and then creating the Log Configuration record for it.

Creating the LogSaver Class

A LogSaver apex class is one that extends the mtl_sflog.LogSaverBase abstract class and overrides the saveLogs method.

Let’s revisit the CustomDebugLogSaver class as seen below.

It overrides the saveLogs method which takes in a SaveLogsRequest parameter. The SaveLogsRequest parameter has one property, LogsToSave, which is a List of LogEntry to save.

The LogSaverBase class also has a LogConfiguration property that contains the Log_Configuration__c record that was used to create this LogSaver apex class. The product queries all the fields that are on the Log_Configuration object and then makes them available to the LogSaver as needed so it can be used for various settings as needed. This means that custom fields can be added to the object and then used as needed without having to query the Log Configuration record again in the LogSaver.