· tutorials · 3 min read

Replace Salesforce Debug Logs in 4 Easy Steps with Nebula Logger

Learn how to replace Salesforce debug logs with Nebula Logger for enhanced logging, persistent data, and an intuitive dashboard.

Learn how to replace Salesforce debug logs with Nebula Logger for enhanced logging, persistent data, and an intuitive dashboard.

Understanding how code runs in a production environment can be difficult without a tool like Nebula Logger. The standard debug logs inside of Salesforce are very limited, making developers feel like they are flying blind. With Nebula Logger, we can get a comprehensive, manageable, and insightful logging framework.

This is a complete tutorial on how to set up Nebula Logger. This logging tool is the most popular logging framework in the Salesforce ecosystem. But why do you need a logging framework at all?

Why Use Nebula Logger?

Debug logs in Salesforce are difficult to read without a tool like Nebula Logger. The standard debug logs are limited in functionality, making it hard to track issues effectively. With the Nebula Logging framework, you can gain full control over your logging needs, providing a complete picture of your code’s behavior.

Benefits of Nebula Logger:

  • Persistent Logging: Save logs for as long as needed.
  • Detailed Dashboard: An easy-to-digest logging dashboard.
  • Versatile Logging: Log in both Apex and Flow.

Getting Started with Nebula Logger

To install the latest version, go to the GitHub repo, and select your preferred install method. It is recommended to use the unlocked package.

Once the package is installed, go to the Nebula Logger app in Salesforce, and you are greeted with the dashboard.

Dashboard Overview

The dashboard serves as a hub for viewing logs. It is a standard Salesforce dashboard that breaks down logs by:

  • Creation Date
  • Tag
  • Origin Source

Each dashboard component is a report containing log entries. Log entries are grouped by a parent log entry that can show multiple logger statements in a transaction.

Logging in Apex

Nebula Logger is great because it can be used in both Apex and Flow. For basic usage, replace the standard debug statement:

System.debug('Test');

with the Logger function:

Logger.debug('Test');
Logger.saveLog();

The major difference between the standard debug log and the logger framework is the need to save the logs at the end of a transaction.

Logging Levels

Like debug logs, you can use logging levels to categorize the severity of the logs. Here are the available levels:

Logger.error('ERROR');
Logger.warn('WARN');
Logger.info('INFO');
Logger.debug('DEBUG');
Logger.fine('FINE');
Logger.finer('FINER');
Logger.finest('FINEST');

Commonly used levels:

Logger.error('ERROR');
Logger.warn('WARN');
Logger.debug('DEBUG');

Logging in Other Places

Nebula Logger can be used across various types of Salesforce automation, including Lightning Web Components (LWC).

Example in LWC

To use in LWC, import the Nebula logger LWC and create the logger in your JavaScript:

import { createLogger } from 'Nebula/logger';

export default class LoggerLWCDemo extends LightningElement {
  logger;

  async connectedCallback() {
    this.logger = await createLogger();
    this.logger.info('Hello, world');
    this.logger.saveLog();
  }
}

Tags and Transaction IDs

To get the most out of Nebula Logger, use additional features to better categorize your logs.

Using Tags

Tags group logs by functionality, which is useful for categorizing logs that are part of a larger business process.

Example using method chaining:

Logger.error('DANGER!').addTag('Integration').addTag('Account');

Example using a list of strings:

List<String> myTags = new List<String>{'Integration', 'Account'};
Logger.error('DANGER!').addTags(myTags);

Using Parent Transaction IDs

Set the parent transaction ID to link asynchronous classes to their originating process.

Example in a batch class:

public with sharing class BatchableLoggerExample implements Database.Batchable<SObject>, Database.Stateful {
    private String originalTransactionId;

    public Database.QueryLocator start(Database.BatchableContext batchableContext) {
        this.originalTransactionId = Logger.getTransactionId();
        Logger.info('Starting BatchableLoggerExample');
        Logger.saveLog();
        return Database.getQueryLocator([SELECT Id, Name, RecordTypeId FROM Account]);
    }

    public void execute(Database.BatchableContext batchableContext, List<Account> scope) {
        Logger.setParentLogTransactionId(this.originalTransactionId);

        for (Account account : scope) {
            Logger.info('Processed an account record', account);
        }

        Logger.saveLog();
    }

    public void finish(Database.BatchableContext batchableContext) {
        Logger.setParentLogTransactionId(this.originalTransactionId);
        Logger.info('Finishing running BatchableLoggerExample');
        Logger.saveLog();
    }
}

Notifications

Nebula Logger can send notifications, for example, to Slack.

Setting Up Slack Notifications

  1. Install the Slack Plugin.
  2. Configure a webhook in Slack.
  3. Copy the URL and paste it into the Nebula Logger settings. Keep this URL secure.
  4. Define the logging level for notifications.
  5. Monitor your Slack channel for notifications.

Conclusion

Nebula Logger is a powerful tool for debugging in Salesforce, offering persistent, detailed, and versatile logging capabilities. By setting it up and using its features, you can significantly improve your debugging process.

Need Our Help To Get Your Data Into Salesforce?

Join dozens of other companies by learning how you can get all your company's data in one place.

Back to Blog