# What architectural components make up a robust integration logging and auditing framework in Salesforce?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Integration Logging, Audit Trails

Read it on the site: https://www.forcechallenges.com/interview-questions/what-architectural-components-make-up-a-robust-integration-logging-and-auditing-framework-in-salesforce

## Answer

**Basics:** When an integration fails at 2 a.m., you need to know what was sent, what came back and which record it concerned. Debug logs are short-lived (24 hours, 20 MB per log) and not designed for this, so teams build their own logger and store entries as records.

**How it works (components):**
1. Log object: `Integration_Log__c` with Correlation_Id, Direction (Inbound/Outbound), Endpoint or Named Credential, HTTP method, Status code, Status picklist (Success, Failed, Retry), Duration, Related Record Id, Attempt count, Request and Response (long text, up to 131,072 characters, truncated and masked).
2. Logger service: an Apex class that buffers entries in memory and flushes once.
3. Wrapper around callouts and inbound Apex REST handlers so logging is automatic.
4. Alerting: Flow or platform event on failures; dashboards for error rate and latency.
5. Retention: scheduled batch to delete or archive old logs.

**Advanced / deep dive:** Transaction behavior is the key design issue. Inserting a log before a callout causes "uncommitted work pending", so buffer and flush after callouts. If the transaction fails and rolls back, so does the log; use a platform event published immediately (with a trigger that inserts the log) or log from a finalizer or separate async job. Volume: millions of rows cost storage, so log failures at full detail and successes sparsely (a custom metadata log-level switch), or use Big Objects or external tools (Splunk, Datadog) via events. Security: never log Authorization headers, tokens or sensitive personal data; mask fields and consider Shield Platform Encryption for log fields. Pass a correlation ID header to the remote system so end-to-end tracing works. Add Event Monitoring (Shield) for API-level audit of who called what.

**Code / config example:**
```apex
public class IntegrationLogger {
    private static List<Integration_Log__c> buffer = new List<Integration_Log__c>();
    public static void add(String corrId, String endpoint, Integer status, String body, Id relId) {
        buffer.add(new Integration_Log__c(
            Correlation_Id__c = corrId, Endpoint__c = endpoint, Status_Code__c = status,
            Response__c = body == null ? null : body.abbreviate(131072), Related_Id__c = relId));
    }
    public static void flush() {          // call once, after all callouts are done
        if (!buffer.isEmpty()) { insert buffer; buffer.clear(); }
    }
}
```

**Hidden gotchas & must-remember points:**
- Logging DML before a callout breaks the callout.
- Logs vanish with a rolled-back transaction unless sent through a separate path.
- Long Text Area holds at most 131,072 characters; truncate deliberately.
- Log storage counts against data storage limits; schedule cleanup.
- Redact secrets and personal data.

**Best practice:** Centralize logging in one service, buffer-and-flush, store sanitized payloads and set retention from day one.

---
Source: ForceChallenges · last updated 2026-11-02
