tracing-sanitize
A tracing layer that automatically strips PII from log output using sanitize-pii.
The problem
You add structured logging to your app. Somewhere, an email, IP address, or API key ends up in your logs. Now you have a GDPR issue, a security incident, or both. tracing-sanitize catches it automatically.
Usage
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.3"
Basic setup (all PII detectors)
use *;
use SanitizeLayer;
registry
.with
.with
.init;
// PII in span and event fields is automatically masked
info!;
// Output: email=j***@***.com "user logged in"
Custom configuration (pick detectors)
use *;
use SanitizeLayer;
use Sanitizer;
let sanitizer = builder
.email
.credit_card
.build;
registry
.with
.with
.init;
What gets sanitized
All built-in detectors from sanitize-pii are supported:
| Type | Example | Masked |
|---|---|---|
joe@gmail.com |
j***@***.com |
|
| Credit card | 4111 1111 1111 1111 |
4111-****-****-1111 |
| Phone | +33 612 345 678 |
+** *** *** 78 |
| IPv4 | 192.168.1.42 |
192.***.***.42 |
| IPv6 | 2001:0db8:... |
***:***:***:*** |
| API keys | sk_live_abc123... |
sk_l****... |
Custom patterns are also supported via Sanitizer::builder().custom(name, regex).
How it works
tracing-sanitize implements a tracing_subscriber::Layer that intercepts span and event fields. String and debug fields are passed through sanitize-pii before reaching downstream layers (like fmt). Numeric and boolean fields pass through untouched.
License
MIT