Skip to content
Backend & APIChapter 31 / 57Intermediate6 min read

Logging

Logging best practices with PII in mind. Use one logger, structured lines, a request id and a scope prefix, and never log passwords, tokens or personal data.

On this page

1. What is this practice?

Logs are the diary that acts as your eyes in production. When a report of something broken comes in, you cannot attach a debugger, you can only read the logs. Good logs answer your question. Bad logs are either silent or become a data leak themselves.

2. Why does it matter?

A developer wrote console.log("login", body) for debugging and never removed it. Now every login password sits in the logs in plain text, and the logs go to a third party service where the whole team and the vendor can read them.

Another case: a magic link token was in the URL query string, and the request logger wrote the full URL. Anyone with log access could open anyone's account.

A third case: a user says "my payment failed". The logs have thousands of lines but no request id, so there is no way to tell which line was theirs.

Scattered console.logOne structured logger
Passwords and tokens in plain text in logsSensitive keys are redacted automatically
Full URL including the query stringOnly the path, no query string
No way to tell which line belongs to which requestA request id on every line
Both console and logger, every error twiceOne logger, one line
Free text, hard to searchJSON fields, easy to filter and alert on

3. When to follow it

LevelHow importantWhy
Learning projectconsole.error is fine, but never log secretsThe habit is formed here
Production codeRequiredLogs are both the only way to debug and a big privacy risk
Senior levelDecide the logger, scrubber and retention policyWhat gets logged, how long it stays, who has access

When to learn it: the first time you deploy an app and have to read production logs to understand an error.

4. Bad vs good code

Bad: log whatever you get

ts
export async function POST(request: Request) {
  const body = await request.json();
  console.log("request received", request.url, request.headers, body);

  try {
    return Response.json(await resetPassword(body));
  } catch (error) {
    console.log(error);
    logger.error(error);
    throw error;
  }
}

The headers hold the cookie and authorization token, the body holds the new password, and the URL holds the reset token. The error is logged in two places and no line has a request id.

Good: one logger, with redaction

File lib/logger/logger.ts, with pino:

ts
import pino from "pino";

import { SENSITIVE_PATHS } from "./logger.types";

export const logger = pino({
  level: process.env.LOG_LEVEL ?? "info",
  redact: { paths: SENSITIVE_PATHS, censor: "[REDACTED]" },
});

File lib/logger/logger.types.ts:

ts
export const SENSITIVE_PATHS = [
  "password",
  "*.password",
  "token",
  "*.token",
  "headers.authorization",
  "headers.cookie",
  "email",
  "*.email",
];

One line per request, written when it finishes:

ts
export function logRequest(entry: RequestLogEntry) {
  logger.info(
    {
      requestId: entry.requestId,
      route: new URL(entry.url).pathname,
      status: entry.status,
      durationMs: entry.durationMs,
    },
    "request completed",
  );
}

Errors in one place, with a scope:

ts
logger.error({ err: error, requestId, scope: "password-reset" }, "[password-reset] reset failed");

Also write a small unit test for the scrubber that checks password and authorization become [REDACTED] in the output. Otherwise someone will remove a key from the list and nobody will notice.

What is the difference?

ThingBad codeGood code
LoggerBoth console.log and a loggerOnly one logger
SecretsBody, headers and URL all in plain textA redact list, with a test
URLWith the query stringOnly the pathname
ContextNothingrequestId, route, status, durationMs
ErrorLogged twiceOnce, with a [scope]

5. How to apply it

Logging setup
Create one logger module
One list of sensitive keys and redaction
Give every request an id and write one line when it finishes
Log errors in one place, with a scope prefix
Unit test the scrubber and ban console.log in lint

What the levels simply mean:

LevelWhen
errorSomething broke and someone should look
warnOdd, but the app is running, like a retry happened
infoAn important event, like a request completed or a job finished
debugOnly for local use or short term troubleshooting

6. Exceptions and common mistakes

Debugging with console.log in local development is perfectly fine, just remove it before you commit. Logging a user id is usually fine because it is not personal data by itself, but never log email, phone, address, identity numbers or payment details.

Mistake: Logging the whole request body or headers

Why? Passwords, cookies and tokens end up in the logs, and anyone with log access can use them.

Better: Log only chosen fields and keep a redaction list of sensitive keys.

Mistake: Logging the full URL in production

Why? Query strings hold reset tokens, search terms and sometimes emails.

Better: Log only the pathname, and if a query param is needed, log it by name, separately.

Mistake: Logging the error at every layer

Why? One problem becomes five lines, alerts get noisy and it is hard to find the real cause.

Better: Log the error where it turns into a response or a job failure, and just propagate it everywhere else.

7. What to learn before and after

Final quick revision

CheckQuestion
One loggerAre console and a logger both running by accident?
RedactionIs there a list of sensitive keys, and a test for it?
URLIs the query string being logged?
ContextDoes every request line have id, route, status and duration?
ErrorsIs every error logged exactly once, with a scope?
Personal dataAre email, phone and identity numbers kept out of the logs?