Safeguard
Security Guides•

What should code review check in logging changes?

Check that new log lines leave out tokens, passwords and personal data, guard against log injection, and still record the security events you need.

Safeguard Security Team
4 min read

When a pull request adds or changes logging, check for two opposite mistakes. Log lines that include secrets or personal data, and missing logs for events you will need during an investigation. Also check that user supplied text cannot forge or break log entries.

Data to keep out of logs

The OWASP Logging Cheat Sheet lists items to remove, mask, hash or encrypt before logging: session IDs (hash them if you need to track session events), access tokens, authentication passwords, encryption keys and other primary secrets, database connection strings, sensitive personal data such as health data and government identifiers, payment data, application source code and commercially sensitive information.

A second group needs care rather than a ban: non-sensitive personal data like names, phone numbers and email addresses, file paths, and internal network names and addresses. Consider pseudonymizing where identity is not needed.

A reviewer's table

In the diffComment to leave
Logging a whole request or response objectList the fields instead. Headers may carry tokens
Logging an exception with its contextCheck what the context object contains
Logging a connection string on startupRemove or mask it
Logging user input in a messageEncode it for the log format
A debug log left in a hot pathConfirm it is off in production

Log injection

The cheat sheet advises sanitizing event data by removing or encoding carriage returns, line feeds and delimiter characters, encoding for the log format, and treating data from other trust zones as untrusted. If a field is malformed, it suggests omitting or safely replacing it and recording the validation failure, not dropping the whole event. It cites CWE-117 for the underlying weakness.

# risky: a newline in username can forge a second entry
log.info("login failed for " + username)

# safer: structured fields are encoded by the logger
log.info("login failed", extra={"user": username})

Events worth logging

A change that removes a log line deserves as much attention as one that adds one. The cheat sheet lists events to record, among them authentication successes and failures, authorization failures, session management failures, input validation failures, application errors, configuration changes, user and privilege changes, access to sensitive data, cryptographic key use or rotation, data import and export, file uploads and deserialization failures.

Each entry should say when, where, who and what: timestamp, application and host, user or machine identity and source address, and event type with severity and description.

Practical steps

  1. Search the diff for logging calls and read each argument.
  2. For each argument, ask what the worst value it could hold is.
  3. Check that security relevant paths still log after the change.
  4. Check retention and access to the log store. A secret in a log is as exposed as the log system is.
  5. If a secret did reach the logs, rotate it. Deleting the line does not undo the exposure.

Where Safeguard fits

Safeguard scans dependencies and builds an SBOM, including the logging libraries you use. Try the free scan at gold.safeguard.sh/scan. What your own code logs is for your reviewers.

Quick check

Should session IDs ever be logged?

OWASP says to remove, mask, hash or encrypt them. Hash them if you need to follow a session's events.

Is user email address sensitive in logs?

It is listed as data to handle with care. Pseudonymize it where identity is not needed.

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.