Skip to the main content
{}
Back to all articles

Technical article

Privacy-safe logging in Laravel without leaking sensitive data

Log correlation IDs, modules and safe technical context, but never raw email addresses, tokens, cookies or complete payloads.

By Codegenie2 minute read
  • Security & privacy
  • Privacy-safe
  • logging
  • Laravel
  • leaking
  • sensitive
Online form beside a padlock and security checks.
In this article

Log what helps explain a failure: event type, technical status and a useful correlation ID. Do not automatically retain a whole request or provider response. An allowlist is easier to inspect than trying to remove unknown sensitive fields afterwards.

Design the event before failure occurs

Specify allowed fields, readers and necessary retention for each event. Distinguish diagnostics, security events and business records. A log file is not an unlimited copy of the database.

Illustration: an invoicing provider fails

Log diagnostic context without payloads or personal dataapp/Support/Logging.phpphp
// Created once at the operation boundary and passed to this handler.
Log::warning('billing.provider_failed', [
    'correlation_id' => $correlationId,
    'provider' => 'billing',
    'http_status' => $response->status(),
    'operation' => 'create_invoice',
]);

The fragment records provider, operation and HTTP status. Generate the correlation ID at the start of the operation and reuse it in subsequent events; a fresh random ID on every line connects nothing. Even a technical identifier can lead to a person through another system, so assess its context.

What stays out of the event?

  • Authorization headers, cookies, session IDs and tokens.
  • Email, phone, free-form messages and complete form payloads.
  • Database credentials, encryption keys and full provider error responses.
  • Uncontrolled text that could inject new log lines or misleading fields.

Check less visible logging paths

A careful Log::warning call does not stop exception reporting, request tracing, error pages or hosting logs from retaining payloads. Inspect those layers separately. In a test environment, use distinctive synthetic values, trigger validation and provider failures and check whether those values appear outside permitted context.

Retention and access belong in the design

Limit reading and export permissions, rotate files and verify retention is actually applied to copies and error monitoring. Define what may be temporarily needed during an incident and who decides. Data minimisation in these examples is not a legal assessment of the complete processing activity.

Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.

Last updated:

Need technical context for your own project?

Codegenie helps with Laravel applications, existing codebases, shared-hosting deployments and technical improvements.