Skip to Content
Docs

Log drains

Send the logs of your workspace to your own log service, and verify each request.

A log drain sends the logs of your workspace to a log service. It is a feature of the Business plan. Test mode needs no plan, so you can try it with a test key. The trial of a new workspace has the Business limits, so it has log drains too. See Plans.

A log drain sends records from one or more sources:

SourceOne record is
api_requestone API request
deliveryone attempt to deliver an event (webhook, postback), or one call to a destination or channel (batch, digest, test)
sync_runone sync run that has ended
clickone click on a link

A record has no secret and no raw IP address. It has id, object (log_record), source, level (info, warning or error), timestamp, workspace, livemode, test and data. data has the fields of the source. Use filters to send only some levels or some HTTP status classes.

A new log drain sends the records that come after you make it. It does not send old records.

Make a log drain in the dashboard

The dashboard shows log drains on the page Integrations. It uses the same public API as the examples below. The list shows the status, the sources, the filters, the last delivery and the last error of each log drain.

  1. Open Integrations. In the group Developer of the catalog, select Connect on the card of a log drain type.
  2. Choose the type. The form shows the fields of the type. A secret field is hidden. The dashboard never shows a saved secret again.
  3. Choose the sources. For each source you can choose levels. For api_request and delivery you can also choose HTTP status classes. Choose nothing to send all records.
  4. Select Add log drain. Then select Send test. The dashboard shows if the log service accepted the test record.

To change a log drain, select Edit. The credentials stay as they are until you select Replace the credentials. New credentials replace all old credentials, so enter all fields. Pause stops a log drain. Delete removes it and its credentials. It does not remove what the log service got already.

Only owners and admins can add, change, test, pause, resume and delete. A member can read the list and the details.

Plan

A live log drain needs the Business plan. Test mode works on all plans. On another plan, the page says so and shows a link to Billing. The API answers 403 plan_feature_unavailable to a live create, update or resume. You can always read, test and delete a log drain.

Statuses

StatusMeaningWhat to do
activeThe log drain sends records.Nothing.
pausedThe log drain does not send. The reason is on the page.Select Resume. For plan_downgraded, upgrade first.
failingThe delivery failed too often. The log drain stopped.Fix the problem (see the last error). Select Resume.

When you resume a log drain, it goes on from its place. It sends the records that came while it was stopped.

The HTTPS endpoint

The type https posts the records to an HTTPS endpoint on your server. You need an API key with the scope log_drains:write.

Make a signing secret. It starts with whsec_ and has the base64 text of 24 to 64 random bytes:

echo "whsec_$(openssl rand -base64 32)"

Make the log drain:

curl https://api.afffast.com/v1/log_drains \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "type": "https",
    "label": "Log service",
    "config": { "url": "https://logs.example.com/ingest" },
    "credentials": {
      "signing_secret": "whsec_...",
      "headers": "Authorization: Bearer my-token"
    },
    "sources": ["api_request", "sync_run"],
    "filters": { "api_request": { "levels": ["warning", "error"] } }
  }'
  • url must use https. It must not point to a private or internal address. The API checks the address at each request, after the DNS lookup. The API does not follow a redirect.
  • signing_secret signs each request. The API never returns it.
  • headers is optional. Write one Name: value in each line (10 lines at most). Use it for a token. The API never returns the value. You cannot set Host, Content-Type, Content-Encoding, Content-Length, User-Agent or a header that starts with webhook-.

POST /v1/log_drains/{id}/test sends one sample record ("test": true).

The request

The API sends one batch of records in each request (1000 records or 5 MB at most):

  • Method POST, Content-Type: application/x-ndjson, Content-Encoding: gzip.
  • The body is NDJSON: one JSON record in each line.
  • The headers webhook-id, webhook-timestamp and webhook-signature follow the Standard Webhooks specification. webhook-id names the batch. A retry of a batch has the same webhook-id.

Answer with a 2xx status. The API reads the status only.

Your answerWhat the API does
2xxThe batch is done.
429The API waits (the Retry-After header sets the time) and tries again.
401, 403, 404, 410The API stops the log drain at once (failing).
408, 5xx, other, no answerThe API tries again with a growing wait.

Verify the signature

The signature is the HMAC-SHA256 of <webhook-id>.<webhook-timestamp>.<body>. The <body> is the exact body of the request, as you receive it (the gzip data). Verify the raw body first. Decompress it after. The secret is the text after whsec_, decoded from base64.

import { createHmac, timingSafeEqual } from "node:crypto";
import { gunzipSync } from "node:zlib";

export function verify(secret, headers, rawBody) {
  // rawBody is a Buffer with the bytes of the request. Do not let a framework decompress it before this function.
  const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
  const id = headers["webhook-id"];
  const timestamp = headers["webhook-timestamp"];
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) throw new Error("old request");
  const expected = createHmac("sha256", key)
    .update(`${id}.${timestamp}.`)
    .update(rawBody)
    .digest("base64");
  const ok = headers["webhook-signature"].split(" ").some((part) => {
    const given = Buffer.from(part.replace(/^v1,/, ""));
    return (
      given.length === Buffer.from(expected).length && timingSafeEqual(given, Buffer.from(expected))
    );
  });
  if (!ok) throw new Error("bad signature");
  return gunzipSync(rawBody)
    .toString("utf8")
    .split("\n")
    .filter(Boolean)
    .map((line) => JSON.parse(line));
}

Other log services

Delivery

  • Delivery is at least once. A record can arrive two times. Use id of the record to remove duplicates. It is stable: api_request:<request ID>, delivery:<delivery ID>:<attempt>, sync_run:<run ID> and click:<click ID>.
  • The API sends the records of a source in the order of their time. A new record arrives after about a minute. Records of api_request and click wait 30 seconds, so that a late write does not skip a record. A click that reaches the API late (after an outage) is still sent, in the order of the time that the API got it. Its timestamp is the time of the click.
  • When the log service fails, the API tries again with the same growing wait as a webhook: 5 seconds, 5 minutes, 30 minutes, 2 hours, 5 hours, then 10 and 14 hours. The last of 11 attempts is about 73.6 hours (3 days) after the first. After it the status is failing and the API writes the event log_drain.failed. A notification channel that selects it tells you. The default email channel of the workspace selects it, so the owners and admins get an email.
  • A log drain that fails keeps its place. To start again, fix the problem and send POST /v1/log_drains/{id}/resume. The log drain goes on from its place, so you get the records of the time of the failure.
  • The API keeps the source data for a limited time: API requests 7, 30 or 90 days by plan (Starter, Pro, Business), deliveries 30 days (postbacks 90 days), clicks 13 months, 3 years or 5 years by plan. A log drain needs the Business plan, so its limits are 90 days for API requests and 5 years for clicks (an owner can choose a shorter time for clicks). A log drain that is stopped for longer than this cannot send the records that are gone.

On this page