To keep phone numbers and message text out of your logs, log identifiers instead of content, redact known fields at the logger, and run a masking function over anything free-form before it's written. Treat your logs and traces as a data store with its own exposure, because they usually have wider access and longer retention than the database the data came from.

An SMS integration touches personal data on nearly every request. The recipient's phone number is in the URL or body, and the text itself may contain a name, an address, an order detail or a one-time code. If your request logger dumps the full payload, you have copied all of that into a system that engineers, vendors and sometimes support staff can search. This post is about engineering practice. It isn't legal advice, and what you must do depends on your own obligations, so check with your counsel for that part.

What counts as sensitive in an SMS log line

Start with an inventory. For a texting platform, these show up in logs more often than people expect:

DataWhere it leaksSafer replacement
Recipient phone numberRequest body, URL path such as a validate call, error messagesInternal contact ID, or masked form
Message bodyRequest body, debug dumps, failed-job payloads, webhook payloadsLength in characters, segment count, template ID
One-time codesMessage bodyNever log, not even masked
Names and addresses inside templatesRendered bodyTemplate ID plus a variable count
API keysHeaders dumped on errorDrop the header, or keep the last 4 characters
Webhook payloadsRaw body logged by middlewareEvent ID, message ID, status

Two rules of thumb sit under the table. If a field is only useful to a human reading the log, ask whether an ID would do the same job. And if a field is a secret, such as a code or a key, don't log a masked version, log nothing.

Log the attempt, not the content

Most debugging needs facts about the send, not its text. A line like this answers nearly every question:

{
  "level": "info",
  "event": "sms.send",
  "request_id": "9b1e0c52",
  "tenant_id": "acct_42",
  "contact_id": "con_8841",
  "template_id": "appt_reminder_v3",
  "body_chars": 94,
  "result": "accepted",
  "http_status": 200,
  "duration_ms": 312
}

It tells you who, which template, how long the text was, and what happened. It doesn't tell you the number or the words. When someone needs the text, they can look it up in your database through the contact and message IDs, where access is controlled and audited, instead of in a log that anyone with a dashboard login can search. Our observability guide covers which fields make a send traceable.

Redact at the logger, by key

Structured logging makes redaction practical, because you can address fields by name. Most JS and Python loggers support it. With pino, for example:

import pino from "pino";

export const logger = pino({
  redact: {
    paths: [
      "req.headers['x-api-key']",
      "req.body.to",
      "req.body.body",
      "*.phone",
      "*.to",
      "*.message",
      "*.body",
    ],
    censor: "[redacted]",
  },
});

A key-based list is your first layer, and it has a known weakness: it only catches the names you thought of. A new field called recipient_number slips straight through. So add a second layer that looks at values.

A masking function for free-form text

Error messages, exception strings and third-party library output contain phone numbers in no predictable field. Run those through a masker that finds number-shaped strings and keeps just enough to correlate.

// Matches E.164 and common US formats: +12125550199, (212) 555-0199, 212-555-0199.
const PHONE = /(?<![\w])(?:\+?1[\s.-]?)?(?:\(\d{3}\)|\d{3})[\s.-]?\d{3}[\s.-]?\d{4}(?!\w)/g;
const E164 = /\+\d{8,15}/g;

export function maskPhone(raw: string): string {
  const digits = raw.replace(/\D/g, "");
  if (digits.length < 7) return "[phone]";
  return `***${digits.slice(-2)}`; // last 2 digits only
}

export function maskText(input: string): string {
  return input
    .replace(E164, (m) => maskPhone(m))
    .replace(PHONE, (m) => maskPhone(m));
}

Choose how much to keep on purpose. Keeping the last two digits helps a person tell two numbers apart in one incident, but it also shrinks the space for someone guessing the rest, so many teams keep nothing. If you need to correlate the same number across log lines without exposing it, use a keyed hash instead of a mask:

import { createHmac } from "node:crypto";

export function phoneToken(e164: string, secret: string): string {
  return createHmac("sha256", secret).update(e164).digest("hex").slice(0, 16);
}

Use a keyed HMAC, not a plain hash. Phone numbers are a small space, so an unkeyed SHA-256 can be reversed by trying every number. Keep the key outside the logging system and rotate it on a schedule. Formatting variants such as (212) 555-0199 and +12125550199 need to be normalized to one form first. Our post on E.164 formatting shows why that step matters.

Be honest about the limit here. A regex catches formats it knows. It won't catch a number written as words, split across fields, or embedded in a long digit string. Masking is a net under the first layer, not a replacement for it.

Traces and error trackers need the same care

Distributed tracing and error trackers are easy to forget, and they often hold richer data than logs.

  • Span attributes. Don't set the recipient or body as an attribute. Set contact_id, template_id and body_chars.
  • HTTP instrumentation. Auto-instrumentation can record full request URLs and bodies. Turn body capture off, and strip or template path parameters, so a route such as GET /v1/validate/:phone is recorded as the pattern and not with the real number in it.
  • Error trackers. Configure a before-send hook that runs your masker over messages, breadcrumbs and request data, and disable capturing request bodies.
  • Queue payloads. Failed jobs sit in a dead-letter queue with their full payload. Our dead-letter guide covers replay. Encrypt those payloads or store a reference to the message row, and not a copy of the text.

Also look at your reverse proxy and load balancer. Access logs record the URL, and a phone number in a path or query string is now in a file you may not control.

Retention is part of redaction

Data you never wrote can't leak, and data you deleted can't either. Set retention per log type, label the numbers as your own policy choices, and write them down:

Log typeExample retention (assumption)Reason
Debug logs3 to 7 daysVerbose, highest risk, short debugging value
Application logs14 to 30 daysEnough for incident review
Audit trail of sendsPer your own policyLives in the database, not in logs
Traces7 daysHigh volume, rarely needed beyond an incident

Add access controls to match. Limit who can search production logs, keep a record of who did, and keep customer-facing support from needing raw logs at all by giving them a tool that reads the message record instead.

Test that redaction works

A redaction rule you've never tested is a hope. Add a test that logs a fixture containing a fake number such as +12125550199, a made-up message body and a fake API key, captures the output, and asserts that none of those strings appear. Run it in CI. Then add a scheduled search of your real log store for number-shaped strings, and alert if it finds any, since that's how you learn a new field started leaking.

Keep message text that includes sensitive health or financial details out of texts to begin with. Our note on PHI in the message body explains why. Ready is not HIPAA compliant and doesn't sign BAAs, so don't treat any logging approach here as making health data safe to send.

The short version

Log IDs, lengths and results, not numbers or words. Redact by field name at the logger, mask number-shaped strings in free text as a second net, and use a keyed HMAC when you need to correlate one number across lines. Strip bodies from traces, error trackers and proxy logs, set short retention you can defend, and test the whole thing with fake data. The goal is simple: when someone searches your logs, they find what happened to a message without finding who it was for or what it said.