Debug logs
sfd logs, sfd logs get and sfd test --logs: listing and reading Apex
debug logs, the TraceFlag setup they trigger, and the filtered output format.
Source: src/ops/logs.mjs, src/ops/log-get.mjs, src/ops/test.mjs,
src/org-session.mjs (traceFor), src/sf-util.mjs (ensureTrace,
afterTrace, logRows, logBody), src/format.mjs (filterLog,
formatLog, formatLogList, formatTests), the logs, logs get and test
adapters in src/cli/logs.mjs, src/cli/logs-get.mjs and src/cli/test.mjs
(options), the stderr trace notice in runRequest in bin/sfd.mjs,
src/server.mjs (returns trace with error responses) and
src/rewrite.mjs (apex list log, apex get log). Tests:
test/ops-test-logs.test.mjs, test/org-session.test.mjs (trace cache),
test/format.test.mjs, test/cli.test.mjs, test/rewrite.test.mjs. test/org.test.mjs has an opt-in check of logs and
logs get latest against a real org (SFD_TEST_ORG=<alias> npm test).
Commands
sfd logs [--count 10] [--user <username>] # one line per log, newest firstsfd logs get <id|latest> # filtered body with a headersfd logs get latest --full # raw body textsfd test --logs # run tests, then attach logs--limitis an alias of--count. The count must be a positive integer.logs gettakes a 15- or 18-character07LApexLog ID, orlatest, which is the authenticated user’s most recent log.--userfilters the list only. It never enables tracing for another user.--no-traceskips TraceFlag setup for one call.
TraceFlag setup
logs, logs get and test --logs need a developer TraceFlag for the
authenticated user. Before each of these calls, sfd ensures one:
- It looks for a
DEVELOPER_LOGTraceFlag on the user that has started and expires more than five minutes from now. If one exists, nothing is written. - Otherwise it updates an existing
DEVELOPER_LOGTraceFlag for the user, or creates one if none exists. The window is 30 minutes from now. - Creating a flag needs a
DebugLevelnamedSFD. If none exists, sfd creates it once and reuses it: ApexCodeFINE, SystemDEBUG, Apex profiling, callout, database, validation and workflowINFO, and Visualforce, Wave and NbaNONE. Flags that sfd extends keep their existing level.
Setup needs Tooling API permission to manage TraceFlag and DebugLevel records.
When sfd writes a TraceFlag, it prints one notice on stderr:
sfd: enabled debug logging for <username> until <expiry> (TraceFlag <id>)The notice appears even with --json, so stdout stays parseable. If a later
step fails (for example, logs get finds no logs), the error response still
carries the notice (trace in the daemon response).
Serialization and caching
- TraceFlag setup runs in its own queue (
traceQueueinsrc/org-session.mjs), separate from the deploy/retrieve/tracking queue. Concurrent calls therefore create at most one flag. - A successful check is cached in daemon memory per org alias, with the user id and expiry. Later calls skip the query while the cached expiry is more than five minutes away.
- The cache is cleared by an auth refresh, by a session error for that alias
(whether or not the call is retried), and by
sfd reload.
Filtered output
sfd logs get and sfd test --logs use filterLog. It keeps these lines and
drops the rest:
| Log event | Rendered as |
|---|---|
USER_DEBUG |
[<line>] <message> (includes EXTERNAL debug lines) |
FATAL_ERROR |
FATAL: <message> |
EXCEPTION_THROWN |
EXCEPTION <message> |
VALIDATION_FAIL, VALIDATION_ERROR |
VALIDATION <message> |
FLOW_ELEMENT_ERROR |
FLOW <fields> |
Limit usage at or above 50% of the maximum (LIMIT_USAGE_FOR_NS block) |
LIMIT <name> <used>/<max> |
Flow limit-summary events (FLOW_INTERVIEW_FINISHED_LIMIT_USAGE) are omitted;
they do not indicate Flow errors. Limit diagnostics come from the thresholded
LIMIT_USAGE_FOR_NS block.
Consecutive identical lines collapse into one line with a ×N suffix. The
logs get output starts with a header, <operation> <status> <duration>ms <size>. logs lists one line per log: <Id> <start time> <operation> <status> <size> <duration>ms.
logs get --full prints the original body with no header or filtering. The JSON
output always contains the original body.
Anonymous Apex (sfd apex) uses a narrower rule in formatApex: only
USER_DEBUG lines (not EXTERNAL) and FATAL_ERROR lines, with no
deduplication and no limit or exception lines. --log prints the full log. Keep
that behavior when changing filterLog; test/format.test.mjs covers it.
Example, from test/fixtures/sample-debug-log.txt:
Test Failed 12ms 2.0 KB[3] helloEXCEPTION [9] System.NullPointerException: boom ×2FATAL: System.NullPointerException: boomVALIDATIONVALIDATION Bad valueFLOW bad flow UpdateRecordsLIMIT SOQL 87/100LIMIT CPU time 5000/10000test --logs
With --logs, the test handler:
- Ensures TraceFlag setup (unless
--no-trace) before the run starts. - Records the client-side start and finish times around the run, subtracting five minutes from the start and adding five minutes to the finish to allow for clock skew with Salesforce. Rounds the start down and the finish up to whole seconds for SOQL UTC datetime literals.
- Before running tests, snapshots up to 200 existing log IDs for the current user at or after the padded start, newest first.
- After the run, queries up to ten
ApexLogrows for the current user whoseStartTimefalls in that window, excluding the snapshot IDs, newest first.
Output depends on the result:
- If the run has no failures, the summary lists only the log IDs.
- If any test fails or any setup failure occurs, every listed log includes its filtered body.
Logs from other activity by the same user during the run may still appear in the list. If more than 200 logs already exist in the snapshot window, older logs beyond that cap may also appear. The allowance covers clock differences of up to five minutes in either direction; larger differences can still omit logs.
Changing this area
- Keep the TraceFlag rules above in sync with
ensureTraceandtraceFor.test/ops-test-logs.test.mjscovers creation, extension, active flags and notices after errors;test/org-session.test.mjscovers concurrent setup and caching. - Filter changes belong in
filterLoginsrc/format.mjs. Add cases totest/format.test.mjs, and update the example above if the output changes. - The agent hook maps
apex list logandapex get log. Keep their rows in the table in agent-hooks.md and inmappingsinsrc/rewrite.mjsin sync, with cases intest/rewrite.test.mjs. - The README’s debug-log summary and command table must match the
optionsof the adapters insrc/cli/logs.mjs,src/cli/logs-get.mjsandsrc/cli/test.mjs.