# Triton

## Core Instance

### instance

```apex
public static Triton instance { get; private set; }
```

Singleton instance of the Triton logger. All logging operations should be performed through this instance.

**Example:**

```apex
Triton.instance.addError(TritonTypes.Area.Accounts, e);
```

## 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()

```apex
public String startTransaction()
```

Starts a new transaction with an auto-generated transaction ID.

**Returns:**

* Current transaction ID

### resumeTransaction(String transactionId)

```apex
public void resumeTransaction(String transactionId)
```

Resumes an existing transaction. Use this to tie together different Salesforce transactions.

**Parameters:**

* `transactionId` - Existing transaction ID to resume

### stopTransaction()

```apex
public void stopTransaction()
```

Stops the current transaction and resets the 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()-*&#x69;ng. 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)

```apex
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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

```apex
public void addError(Area area, Exception e)
```

The corresponding immediate method:

```apex
public void error(Area area, Exception e)
```

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj-1)).

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj-1))

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

Add a Log with Event category and an INFO Log Level (see [Level Enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj-1)).

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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.

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

The corresponding immediate method:

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

`type` -- log record Type (see [Type enum](/pharos-triton/apex-logging-basics.md#h_01ht0g8b2jq5s214ya8nw8x9za))

`area` -- log record Functional Area (see [Area enum](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj))

`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

Here's the markdown documentation for the requested methods from Triton.cls:

###

### addDMLResult(Area area, List\<Object> dmlResults)

Add Log with Error Category for DML operation results. This method automatically captures failed operations and related record IDs from Database operation results (SaveResult, DeleteResult, etc.).

```apex
public void addDMLResult(Area area, List<Object> dmlResults)
```

The corresponding immediate method:

```apex
public void dmlResult(Area area, List<Object> dmlResults)
```

* area - log record Functional Area (see Area enum)
* dmlResults - List of Database operation results (SaveResult, DeleteResult, UndeleteResult, UpsertResult, or MergeResult)

Supported DML Result Types:

* Database.SaveResult
* Database.DeleteResult
* Database.UndeleteResult
* Database.UpsertResult
* Database.MergeResult

This method will create a log with the following additional attributes:

* Category: Apex
* Type: DMLResult
* Level: ERROR (only logs failures)
* Summary: Contains operation type and failure statistics
* Details: Contains all error messages
* Related Objects: Contains IDs of failed records

Example usage:

```apex
List<Account> accounts = new List<Account>();
// ... populate accounts ...
List<Database.SaveResult> results = Database.insert(accounts, false);
Triton.instance.addDMLResult(TritonTypes.Area.AccountManagement, results);
```

The above example will create an error log if any accounts fail to insert, including:

* Which records failed (IDs in Related Objects)
* How many records failed vs total
* All error messages from the failed operations

## All Purpose Logging

### setTemplate(TritonBuilder builder)

```apex
public void setTemplate(TritonBuilder builder)
```

Sets a builder template that can be reused for creating multiple log entries with similar base attributes.

`builder` -- TritonBuilder instance to be used as a template

**Example:**

```apex
TritonBuilder template = Triton.makeBuilder()
    .category(TritonTypes.Category.Apex)
    .area(TritonTypes.Area.OpportunityManagement)
    .level(TritonTypes.Level.INFO);
Triton.instance.setTemplate(template);
```

### fromTemplate()

```apex
public TritonBuilder fromTemplate()
```

Retrieves a copy of the previously saved template builder. The returned builder includes the current transaction ID.

**Returns:**

`TritonBuilder` -- A cloned instance of the template builder with the current transaction ID set

**Example:**

```apex
TritonBuilder builder = Triton.instance.fromTemplate()
    .summary('New log entry')
    .details('Log details');
```

### addLog

```apex
public void addLog(TritonBuilder builder)
```

Adds a log entry to the buffer using a TritonBuilder instance. Automatically captures stack trace if not provided.

**Parameters:**

* `builder` - Configured TritonBuilder instance

**Example:**

```apex
Triton.instance.addLog(
    Triton.makeBuilder()
        .category(TritonTypes.Category.Apex)
        .type(TritonTypes.Type.Backend)
        .area(TritonTypes.Area.OpportunityManagement)
        .summary('Process completed')
        .details('Operation successful')
);
```

### log

```apex
public void log(TritonBuilder builder)
```

Immediately persists a log entry. Combines addLog() and flush() operations.

**Parameters:**

* `builder` - Configured TritonBuilder instance

**Example:**

```apex
Triton.instance.log(
    Triton.makeBuilder()
        .category(TritonTypes.Category.Apex)
        .type(TritonTypes.Type.Backend)
        .area(TritonTypes.Area.OpportunityManagement)
        .summary('Critical operation completed')
        .details('Operation successful')
        .level(TritonTypes.Level.INFO)
);
```

## 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.

```apex
@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](/pharos-triton/apex-logging-basics.md#h_01hsyh0nmy90sa21hdbktchavj-1)).

```apex
@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](https://triton.pharos.ai/pharos-triton/methods-reference/apex-methods-reference/pages/B0bhnnbvGLvDiSScPA0a#tritonflow.flowlog))


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://triton.pharos.ai/pharos-triton/methods-reference/apex-methods-reference/triton.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
