Skip to content
Dhaval Heruwala edited this page Apr 29, 2020 · 20 revisions

Welcome to the SFLogging wiki!

This component is part of a Comprehensive Logging strategy, it plays the role of capturing custom logs and tracing into Apex code. For a broader logging strategy, refer to the GitBook.

Unlocked Package Installation

Install the unlocked package in your Org using below URL:

https://login.salesforce.com/packaging/installPackage.apexp?p0=04t1U0000058YTBQA2

Test the functionality

below are two demonstrations:

Example of Streaming logs/tracing

Once the package is installed, to test the functionality,

  1. From the Salesforce App Launcher, open the Event Logger App and Developer console in the Browser side by side and
  2. paste and execute the following code in Debug > Execute Anonymous Window
Log.push('MyClass.methodA'); 
  insert new Task (Subject = 'Test'); 
  Log.push('MyClass.methodB'); 
    Log.message('Doing something'); 
    insert new Task (Subject = 'Test'); 
  Log.pop(); 
  
  Task[] tasks = [select Id from Task]; 
  
  Log.push('MyClass.methodC'); 
    Task[] tasks2 = [select Id from Task]; 
  Log.pop(); 
  Log.message('Doing something else'); 
Log.pop(); 

Log.emit();

You would notice the logs streamed to Streaming Component Tab as well as in the Log Monitor utility bar

Example of embedding logger in your Apex Code

Again, in your developer console, add a Demo Class CustomLogEventDemo.apxc

public class CustomLogEventDemo{
  public static void TestSOQLTolerance(){
    Log.push('CustomLogEventDemo.TestSOQLTolerance');
      Log.message('Testing SOQL Tolerance Logging');
      
      Opportunity[] opps = [SELECT Name,NextStep FROM Opportunity];
      opps = [SELECT Name,Probability,StageName FROM Opportunity];
    Log.pop();

    Log.emit();
  }
}

Test the Logging by pasting CustomLogEventDemo.TestSOQLTolerance(); in Debug > Execute Anonymous Window in the developer console.

You would notice the logs streamed one more time, however since there was SOQL tolerance threshold hit, the event was logged to a custom object which can be viewed by navigating to the third tab Log Recordings

Some Commands

Create a Package Version:

  • Creating Package Version

sfdx force:package:version:create -p SFLogging -d force-app -x -w 10

  • Deploy code to org

sfdx force:source:push -f for forcing

  • Pull code from scratch org

sfdx force:source:pull

  • Install package

sfdx force:package:install --package 04t1U0000058YTBQA2 -u scratch-org-user@yourOwnOrg.org

  • Generate Password for your scratch org

sfdx force:user:password:generate -u MyScratchOrg1