Safeguard
Security Guides•

What should an API log for security, and what must it never log?

A field list for API security logs based on OWASP logging guidance: who, what, where and when, plus the data that must never be written to a log.

Safeguard Security Team
3 min read

An API security log should record who called, what they tried, where it landed, when it happened and what the result was, with a request ID that ties events together. It must never record tokens, passwords, keys, card data or session identifiers in clear form.

Why it matters

You cannot investigate abuse you did not record. OWASP's REST guidance says to write audit logs before and after security events, log token validation errors and sanitise log data to prevent log injection. Its validation advice adds that high volumes of validation failures suggest abuse, which you can only see if failures are logged.

Fields to record

OWASP's Logging Cheat Sheet organises event attributes as when, where, who and what. Applied to an API:

GroupFields
WhenLog time, event time, an interaction or request ID
WhereService name and version, host, method and path (normalised), entry point
WhoSource IP, user or client identity, user type if known
WhatEvent type, severity, action attempted, object affected, result, reason, HTTP status code, user agent

Events worth logging

From the same cheat sheet, the ones that matter most for APIs:

  • authentication successes and failures
  • authorization failures
  • input and output validation failures
  • suspicious JWT validation failures
  • requests with unexpected HTTP verbs
  • administrative actions and use of privileged or shared accounts
  • access to sensitive data, and data imports and exports
  • attempts to exceed limits and other suspicious business logic activity

Data to keep out

The cheat sheet lists data to remove, mask, hash or encrypt: access tokens and authentication passwords, session identifiers (or a hashed value), database connection strings, encryption keys, payment card data and sensitive personal data. Handle file paths, internal addresses and names with care. Query strings deserve a look, since a token sent there would otherwise be written into every access log.

Practical steps

  1. Define one log schema for all services, so queries work across them.
  2. Add a request ID at the edge and pass it downstream.
  3. Redact in the logging library, not only in review.
  4. Log authorization failures with the object type, not the object contents.
  5. Alert on spikes of 401, 403, 429 and validation errors.
  6. Set retention to match your policy and keep logs access controlled.

Check it works

Run a scan or a test with a fake token in a header and in a query string, then search your logs for that value. If it appears, fix the redaction. Then trigger a few failures and confirm each one is searchable by request ID.

Quick check

Should I log request bodies?

Rarely in full. Log selected non sensitive fields, sizes and identifiers. Full bodies often contain personal data and secrets.

How do I log without breaking privacy rules?

Minimise personal data, pseudonymise where identity is not needed, and set a retention period. Confirm the details with your privacy team.

Safeguard's free scan at gold.safeguard.sh/scan covers dependency risk, including logging libraries.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.