Skip to main content

Log Redactor

CI PyPI Python License

This is a small redaction helper for Python logs and payloads that helps prevent accidental exposure, via logging, of secrets and other things you wouldn't want to persist on a server somewhere (email addresses, passwords, etc.).

Of course, you may be thinking we should never log this stuff anyway, but there are some instances where things are okay when working locally but not okay when working with real infrastructure. Unfortunately, this means that some things that shouldn't get logged, do get logged, by mistake. This package tries to stop that.

It works with both the official Python logger and with print statements.

Install the PyPI package python-log-redactor, then import log_redactor. The GitHub repo is log-redactor.

Why use it?

  • Redacts by key name and regex for common secrets/sensitive information
  • Supports nested dict / list / tuple structures
  • Works with standard library logging and %s-style args
  • Keeps runtime dependencies at zero (stdlib only)

Installation

pip install python-log-redactor

Quick start

The usual setup is once at process start: put RedactingFilter on your logging handlers, then forget about it. Leave patterns and keys unset so every built-in detector runs. Attaching to the logging handlers themselves (not only a named logger) means library logs that pass through those handlers are redacted too.

import logging
from log_redactor import RedactingFilter

logging.basicConfig(level=logging.INFO)
for handler in logging.getLogger().handlers:
    handler.addFilter(RedactingFilter())

logging.info("User %s used key %s", "alice@example.com", "sk-live-abc123")
# User [REDACTED] used key [REDACTED]

That is the core use case. The rest of the API is for when you already have a string or dict in hand, or you want the same treatment for print.

from log_redactor import redact, redact_dict

redact("Contact: dev@example.com", patterns=["email"])
# 'Contact: [REDACTED]'

redact("https://api.example.com/x?token=super-secret&id=9", patterns=["url_token"])
# 'https://api.example.com/x?token=[REDACTED]&id=9'

print(redact_dict(
    {"username": "alice", "password": "super-secret", "profile": {"email": "alice@example.com"}},
    patterns=["email"],
))
# {'username': 'alice', 'password': '[REDACTED]', 'profile': {'email': '[REDACTED]'}}

Print wrapping is opt-in and process-wide. Restore print when you are done:

from log_redactor import disable_print_redaction, enable_print_redaction

enable_print_redaction(patterns=["email", "api_key"])
print("User", "alice@example.com", "used", "sk-live-abc123")
# User [REDACTED] used [REDACTED]
disable_print_redaction()

How it works

Redaction uses two independent mechanisms:

  • Keys: if a mapping key matches a sensitive name (case-insensitive), the entire value is replaced. Nested dicts, lists, and tuples are included.
  • Patterns: string values are scanned with regex. Matches are replaced with replacement (default [REDACTED]).

patterns=None enables all built-in patterns. keys=None uses the built-in sensitive key set. Passing keys=[...] or patterns=[...] replaces those defaults with your preferred keys or pattersn. It does not merge with them. Use patterns=[] plus custom_patterns= if you want to only apply your own regexes.

Importantly, it identifies auth tokens with the identifier of url_token and this is the exception to whole-match replacement. It keeps the query-parameter prefix (token=, api_key=, and similar) and only redacts the value so that developers can see the type of auth token that was printed accidentally.

API

from log_redactor import (
    RedactingFilter,
    disable_print_redaction,
    enable_print_redaction,
    redact,
    redact_dict,
)
  • redact(text: str, patterns=None, custom_patterns=None, replacement="[REDACTED]") -> str
  • redact_dict(data: dict, keys=None, patterns=None, custom_patterns=None, replacement="[REDACTED]") -> dict
  • RedactingFilter(logging.Filter)
  • enable_print_redaction(keys=None, patterns=None, custom_patterns=None, replacement="[REDACTED]") -> None
  • disable_print_redaction() -> None

Unknown built-in pattern names raise ValueError. redact and redact_dict raise TypeError if the top-level value is not a str or dict respectively.

Custom patterns and keys

from log_redactor import redact, redact_dict

redact(
    "order id: internal-12345",
    patterns=[],
    custom_patterns=[r"internal-\d+"],
)
# 'order id: [REDACTED]'

redact_dict(
    {"password": "letmein", "session_id": "abc", "note": "alice@example.com"},
    keys=["session_id"],
    patterns=["email"],
)
# {'password': 'letmein', 'session_id': '[REDACTED]', 'note': '[REDACTED]'}

In the second example, password is not redacted by key detection because the optional keys=["session_id"] replaced the built-in key set.

Logging

Add the filter to handlers at startup (see Quick start) so every record those handlers emit is redacted. logger.addFilter(...) only covers records created on that logger.

RedactingFilter mutates record.msg and record.args in place before the record is formatted. It works with %s-style arguments and mapping arguments. If you log an f-string, the message is already interpolated and pattern matching still runs on that text, however key-based redaction cannot see the original objects.

extra= fields and exception text are not redacted.

Print redaction

enable_print_redaction() is opt-in. It monkeypatches builtins.print for the entire process, so every later print in that process uses the wrapper, including library code and tests. Call disable_print_redaction() to restore the original print (this is safe to call even if redaction is not active). It is preferred to use try / finally so a failure cannot leave the patch in place:

enable_print_redaction(patterns=["email", "api_key"])
try:
    print("User", "alice@example.com", "used", "sk-live-abc123")
finally:
    disable_print_redaction()

This is a convenience for accidental prints, not a substitute for RedactingFilter on your handlers.

Built-in patterns

These are best-attempt regexes and I hold my hands up that these are not perfect detectors. patterns=None enables all of the built in regexes that log-redactor ships with.

Name Example match Notes
email alice@example.com Typical user@host.tld addresses
ipv4 127.0.0.1 Dotted-quad IPv4
jwt eyJhbGciOi... (three .-separated segments) Header must start with eyJ; payload and signature must be at least 8 characters. Versions like 1.2.3 are left alone
bearer_token Bearer abcd... Bearer plus a token-like string
api_key sk-live-abc123, AKIAxxxxxxxxxxxxxxxx, AIzaxx... Only OpenAI-style sk-live / sk-test, AWS access key IDs, and Google AIza keys
url_token ?token=secret → ?token=[REDACTED] Keeps the parameter name; redacts the value
credit_card_basic 4111111111111111, 378282246310005 13–19 digit runs (Visa, Mastercard, Amex, …), optional spaces/hyphens. Replaced only if the digits pass a Luhn check

Built-in sensitive keys

When keys=None, these names (case-insensitive) cause the whole value to be replaced:

  • password
  • passwd
  • secret
  • token
  • access_token
  • refresh_token
  • api_key
  • authorization

Development

python3 -m venv .venv
. .venv/bin/activate
pip install -e . pytest ruff
pytest
ruff check .

See CONTRIBUTING.md if you want to send a change.

Security note

This package is intended to reduce accidental leakage, not guarantee perfect anonymisation or satisfy a compliance regime. You should not be printing secrets to logs on real infrastructure regardless. If you do, this is just a failsafe. It may miss values, and some patterns will over-redact. The primary use case is combatting forgetfulness when moving from local to real infra.

Don't be stupid. Do not log secrets on real infra.

To report a vulnerability, see SECURITY.md.

License

This package is distributed under an MIT license.

Changes are listed in CHANGELOG.md.

Metadata

Release files for python-log-redactor 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-log-redactor 0.2.0
File Size Uploaded Provenance Actions
python_log_redactor-0.2.0.tar.gz 12.4 kB With provenance Details for python_log_redactor-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-log-redactor 0.2.0
File Interpreter ABI Platform Actions
python_log_redactor-0.2.0-py3-none-any.whl Python 3 none any Details for python_log_redactor-0.2.0-py3-none-any.whl

Total release size: 23.0 kB

Release files / python_log_redactor-0.2.0.tar.gz

Download URL python_log_redactor-0.2.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1c126f415ab5ae499c7b870eb64cddeccce7c98f0080a1f581e68834affa8c15
BLAKE2b-256 checksum
How to use checksums
241e62a5feca27c071dc15128aa95c73868c00d8fcca17feebcc39a2fa788a1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

Release files / python_log_redactor-0.2.0-py3-none-any.whl

Download URL python_log_redactor-0.2.0-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1da1052f4f4b831389a0702ddffead1727dbea95e3c8d690839ddfacd664b0c
BLAKE2b-256 checksum
How to use checksums
261b2acd779d27fd9a04ce9e0eb716013c0efd1950092323e55c4bc95700830a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

Security for python-log-redactor 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for python_log_redactor-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page