📔Apex Methods Reference

This article covers the base Apex methods you'll need to create log records.

There are 3 files that contain all of the logging methods:

  • Triton.cls -- contains all things Apex, which is the majority of the logging methods.

  • TritonLwc.cls -- here you'll find some helper wrappers as well as the AuraEnabled method for saving logs.

  • TritonFlow.cls -- here you'll find the invocable log method as well the FlowLog wrapper class.

Transaction Methods

These methods are responsible for establishing and maintaining a transaction context. A transaction is identified by either an auto-generated transaction Id or the standard request Id provided by Salesforce. These methods can be found in the main class Triton.cls.

startTransaction()

public String startTransaction()

Start new transaction. This method will obtain a new auto-genrated transaction id.

Returns the current transaction Id.

resumeTransaction(String transactionId)

resumeTransaction(String transactionId)

Resumes a transaction and assumes an existing transaction id. Use this method to tie together different Salesforce transactions into a larger transaction.

transactionId -- An existing transaction Id

stopTransaction()

public void stopTransaction()

Stops a transaction and resets the current transaction Id. Use this method to marking tracking logs with the current transaction Id.

Apex Logging Methods

There are two types of log methods per each category: buffered and immediate. Buffered methods will add to the log collection without flush()-ing. Immediate methods will add to the log collection and call flush(). Buffered log methods are prefixed with add. The reference below covers only the buffered methods as their immediate counterparts have the same signatures. These methods can be found in the main class Triton.cls.

add(pharos__Log__c log)

public void add(pharos__Log__c log)

Adds a log to the buffer. Performs a check on current log level prior.

log -- Pharos log record

addError(Type type, Area area, String summary, String details)

Add Log with Error Category. This method will automatically get the stacktrace and save it on the log record.

public void addError(Type type, Area area, String summary, String details)

The corresponding immediate method:

public void error(Type type, Area area, String summary, String details)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

addError(Area area, Exception e)

Adds a Log with Error Category. This method will automatically get the stacktrace from the passed in Exception instance. Type will be obtained from Exception as well. If blank, a default Backend Type will be saved. Log Summary is the Exception message and Log Details will be a combination of the Exception string and stack trace.

public void addError(Area area, Exception e)

The corresponding immediate method:

public void error(Area area, Exception e)

area -- log record Functional Area (see Area enum)

e --instance of an Exception

addWarning(Type type, Area area, String summary, String details)

Adds a Log with Warning category. This method will not save a stack trace.

public void addWarning(Type type, Area area, String summary, String details)

The corresponding immediate method:

public void warning(Type type, Area area, String summary, String details)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

addDebug(Type type, Area area, String summary, String details)

Adds a Log with Debug category. This method will automatically get the stack trace.

public void addDebug(Type type, Area area, String summary, String details)

The corresponding immediate method:

public void debug(Type type, Area area, String summary, String details)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

addDebug(Type type, Area area, String summary, String details)

Adds a Log with Debug category and saves the execution duration. This method will automatically get the stack trace.

public void addDebug(Type type, Area area, String summary, String details, Decimal duration)

The corresponding immediate method:

public void debug(Type type, Area area, String summary, String details, Decimal duration)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

duration -- a custom decimal value representing the duration of the execution (in milliseconds)

addEvent(Level level, Type type, Area area, String summary, String details)

Add a Log with Event category and a custom Log Level (see Level Enum).

public void addEvent(Level level, Type type, Area area, String summary, String details)

The corresponding immediate method:

public void event(Level level, Type type, Area area, String summary, String details)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

level -- log level (see Level Enum)

addEvent(Type type, Area area, String summary, String details)

Add a Log with Event category and an INFO Log Level (see Level Enum).

public void addEvent(Type type, Area area, String summary, String details)

The corresponding immediate method:

public void event(Type type, Area area, String summary, String details)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- summary of the issue. Saves to log record Summary field

details -- details of the issue. Saves to log record Details field

addIntegrationError(Area area, Exception e, HttpRequest request, HttpResponse response)

Add Log with Integration category that also saves http request and response payloads. This method will automatically get the stack trace from Exception.

public void addIntegrationError(Area area, Exception e, HttpRequest request, HttpResponse response)

The corresponding immediate method:

public void integrationError(Area area, Exception e, HttpRequest request, HttpResponse response)

area -- log record Functional Area (see Area enum)

e -- instance of an Exception

request -- an instance of HttpRequest. Saves to log record Details field

response -- an instance of HttpResponse. Saves to log record Details field

addIntegrationError(Type type, Area area, String summary, String details, HttpRequest request, HttpResponse response)

Add Log with Integration category that also saves http request and response payloads. This method will automatically get the stack trace from Exception.

public void addIntegrationError(Type type, 
                                Area area, 
                                String summary, 
                                String details, 
                                HttpRequest request, 
                                HttpResponse response)

The corresponding immediate method:

public void integrationError(Type type, 
                                Area area, 
                                String summary, 
                                String details, 
                                HttpRequest request, 
                                HttpResponse response)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- a brief summary string

details -- a detailed description string

request -- an instance of HttpRequest. Saves to log record Details field

response -- an instance of HttpResponse. Saves to log record Details field

addIntegrationError(Type type, Area area, String summary, String details, RestRequest request, RestResponse response)

Add Log with Integration category that also saves rest request and response payloads. This method will automatically get the stack trace from Exception.

public void addIntegrationError(Type type, 
                                Area area, 
                                String summary, 
                                String details, 
                                RestRequest request, 
                                HttpResponse response)

The corresponding immediate method:

public void integrationError(Type type, 
                                Area area, 
                                String summary, 
                                String details, 
                                RestRequest request, 
                                RestResponse response)

type -- log record Type (see Type enum)

area -- log record Functional Area (see Area enum)

summary -- a brief summary string

details -- a detailed description string

request -- an instance of RestRequest. Saves to log record Details field

response -- an instance of RestResponse. Saves to log record Details field

LWC Logging Methods

This methods is used by the LWC logger from custom LWC components. This method is located in TritonLwc.cls.

saveComponentLogs(List<ComponentLog> componentLogs)

Saves component logs from LWC. This method will publish immediately. Use this method to persist logs generated from LWC components.

  • Category will be fetched from the componentLog.

  • Type will be fetched from the componentLog directly, or from the error. If neither are set a default Frontend category will be used.

  • Area will be fetched from the componentLog directly if set. Otherwise component name will be used.

  • Summary will be fetched from the componentLog directly if set. Otherwise, error message will be used if provided.

  • Transaction Id will be used from the componentLog, or a new transaction id will be generated.

  • Apex Name (Operation) will be set to component.function or component.action.

  • Created Timestamp will be either set from the componentLog if provided, otherwise current timestamp will be used.

  • Log Level will be taken from the componentLog if provided, otherwise INFO will be used.

@AuraEnabled
public static void saveComponentLogs(List<ComponentLog> componentLogs)

componentLogs -- a collection of ComponentLog records (see ComponentLog class)

Flow Logging Methods

This method is used by Flows or Process Builder to generate logs. It is located in TritonFlow.cls.

log(List flowLogs)

Invocable method, that can be called via flow. Defaults to INFO log level (see Level Enum).

@InvocableMethod(Category='Logging' 
                Label='Add Log' 
                Description='Creates logs from flow or process builder')
public static void log(List<FlowLog> flowLogs)

flowLogs -- a collection of FlowLog records (see FlowLog class)

Last updated