Logging in Apex
The everyday Apex logging patterns — a first log, grouping logs under a transaction, and the try/catch/finally shape you'll reach for again and again.
You've got the mental model down: describe a log with a builder, hand it to Triton, flush when you're ready. This section is where that model meets real Apex — triggers, services, batches, callouts, REST endpoints. We'll start with the shape you'll use most and build up from there.
A first Apex log
Here's error logging in Triton 2.0, start to finish:
public void updateAccounts() {
Triton.startTransaction();
try {
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 1];
if (!accounts.isEmpty()) {
Triton.debug(Triton.t
.area(TritonTypes.Area.Accounts)
.summary('Account found, applying update')
.details('Processing ' + accounts[0].Id));
accounts[0].Name = 'Logging rules!';
update accounts;
}
Triton.flush();
} catch (Exception e) {
Triton.logNow(Triton.t.area(TritonTypes.Area.Accounts).exception(e));
} finally {
Triton.stopTransaction();
}
}Everything you need is in that snippet. Let's unpack the three ideas that make it tick.
Group related logs with a transaction
Triton.startTransaction() opens a correlation scope: every log you create until stopTransaction() shares one transaction id, and Pharos groups them under a single parent log. Instead of a scattered pile of records, you get a tidy tree you can navigate top-down.
The first log created becomes the parent; everything after nests beneath it.
If you skip startTransaction() entirely, Triton still logs everything — the records just won't be grouped under a shared parent. Grouping is a convenience, not a requirement.
.exception(e) — the error one-liner
When you're catching an exception, don't hand-assemble the log. .exception(e) sets the type from the exception (falling back to Backend), flips the level to ERROR, copies the message into the summary, captures the stack trace and details, and flags the log for issue creation — all at once:
Set the area, pass the exception, done. Issues are created automatically for error logs; routine debug/info logs don't generate issues. Full builder details live in The Builder & Templates.
Buffer the routine, publish the critical
Notice the mix above: Triton.debug(...) and Triton.info(...) buffer (cheap, published together on flush()), while the exception uses Triton.logNow(...) to publish immediately. That's the discipline for every Apex context:
Buffer progress and info logs, then
Triton.flush()at a safe checkpoint.Publish critical failures now with
Triton.logNow(...)so you never lose them if the transaction dies.
Where to go next
Each page here takes one Apex context and goes deep:
Batch Logging — buffering across
execute()chunks and grouping every iteration under one transaction.Performance Marks — timing a stretch of Apex with
startMark/endMark, and the span tree they build.Integration Logs — capturing request/response payloads on outbound callouts.
Apex REST Logging — logging inbound REST requests and threading the transaction id downstream.
Full Control with TritonBuilder — the complete builder surface, custom attributes, and templates.
Log Levels — choosing severity and tuning verbosity per environment.
Last updated