Skip to content

🪵 Lightweight logging framework for Salesforce using Platform Events

Notifications You must be signed in to change notification settings

tomcarman/sf-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A logging framework for Salesforce using Platform Events

Most Salesforce orgs require some form of logging to overcome the limitations of the standard debug logs which do not persist. Logging frameworks often become some of your lowest level code, so its imperative that they are non-blocking to normal code - and equally that failures in the main execution can't block the DML of the logs. This makes Platform Events a great candidate to use, as they fire immediately, asynchronously, and in under their own execution.

Approach

  • This is still a work in progress and is not production ready
  • Utility class to create logs (Logger.cls)
  • Batch Job to delete historic logs
  • Log structure encapsulated in class, with subclasses to handle different types of logs, or indeed create different types of Platform Events (Log.cls)
  • Platform Events are published for any logs created
  • A Trigger on the Platform Event listens for events and persists them to a Custom Object

Todo

  • Error handling for character limits (eg. stack trace too big for field)
  • Improve the Log.location matching. The current implementation is largely based on this stackexchange post, but its not ideal.
  • Unit tests
  • Expand the types that can be logged
  • Stacked exceptions - could these be logged recursively?
  • Implement a basic 'JSON Viewer' in LWC to make viewing log data nicer (possibly using something already available like json-viewer-js

Installation

Scratch Org

  1. Clone the repo
git clone https://github.com/tomcarman/logger.git
  1. Create a new scratch org
sfdx force:org:create -f config/project-scratch-def.json -a logger-demo
  1. Push source to the org
sfdx force:source:push -u logger-demo
  1. Open the org
sfdx force:org:open -u logger-demo
  1. Change to the App 'Logger Demo' and switch the List View to 'All'

app

view

  1. View some logs (you'll need to create some first - see Usage)

loglist

logdetail

Existing Org

  • Deploy metadata using your normal methods

Usage

// Create & immediately publish a simple log

Logger.get().publish('Hello world');

// Also accepts Exceptions or DMLExceptions eg.

try {  
    insert new Account();
} catch(DMLException dmlEx) {
    Logger.get().publish(dmlEx);  
}


// If you know you are going to create many logs in quick succession, they
// can be stored on the stack before publishing

Logger.get().add('Starting awesome code');

// Awesome code
// ..
// ..

Logger.get().add('Something went wrong');

// Dont forget to publish the logs!

Logger.get().publish();

Sources

This project is heavily inspired by the following -

About

🪵 Lightweight logging framework for Salesforce using Platform Events

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages