No project description provided
Project description
safe-logs
A lightweight, zero-dependency Python library that automatically scrubs sensitive information (PII, secrets, API keys) from your logs.
Why use safe-logs?
Keeping sensitive information out of your logs is critical for security and compliance. Log files are often aggregated and stored in centralized systems where multiple developers or third-party services might have access to them. Leaking emails, phone numbers, credit cards, or API tokens can lead to severe security breaches. safe-logs acts as a safety net, ensuring these secrets are masked before they ever hit the output stream.
Installation
You can install safe-logs via pip:
pip install safe-logs
Or using poetry:
poetry add safe-logs
Usage
1. The Quickest Way (Convenience Function)
The easiest way to start logging safely is to use the get_safe_logger function. This returns a standard Python logging.Logger with our scrubbing formatter already attached.
from safe_logs import get_safe_logger
logger = get_safe_logger("my_app")
# Outputs: 2026-06-12 12:00:00,000 - my_app - INFO - User [MASKED_EMAIL] has signed in.
logger.info("User test@example.com has signed in.")
2. Using with an Existing Logger (ScrubbingFormatter)
If you already have a complex logging setup, you can simply swap out your standard formatter for the ScrubbingFormatter.
import logging
from safe_logs import ScrubbingFormatter
logger = logging.getLogger("custom_app")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = ScrubbingFormatter(fmt='%(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("API request with token='super-secret-token'")
# Outputs: DEBUG: API request with token='[MASKED_SECRET]'
3. Direct String & Structured Data Scrubbing (LogScrubber)
If you just need to scrub data independent of the logging module, you can use the LogScrubber class directly. It recursively scrubs strings, lists, and dictionaries.
from safe_logs import LogScrubber
scrubber = LogScrubber()
data = {
"user_id": 123,
"email": "user@domain.com",
"password": "my_super_secret_password"
}
clean_data = scrubber.scrub(data)
print(clean_data)
# Outputs: {'user_id': 123, 'email': '[MASKED_EMAIL]', 'password': '[MASKED]'}
Note: Any dictionary key matching "password", "secret", "token", etc., is automatically scrubbed entirely, regardless of its value's format.
Advanced Features
Partial Masking (Obfuscation)
Sometimes for debugging, you want to see the last 4 digits of a credit card or API key instead of replacing the entire string.
from safe_logs import LogScrubber, keep_last_n_chars
scrubber = LogScrubber(mask_templates={
"credit_card": keep_last_n_chars(4, '*'),
"api_key": keep_last_n_chars(4, '*')
})
# Outputs: "Card: ****************3456"
print(scrubber.scrub("Card: 1234-5678-9012-3456"))
Adding Custom Patterns
You can easily add your own regex patterns to the scrubber:
scrubber = LogScrubber()
# Add a Social Security Number pattern
scrubber.add_pattern("ssn", r"\b\d{3}-\d{2}-\d{4}\b", "[MASKED_SSN]")
# Outputs: "SSN: [MASKED_SSN]"
print(scrubber.scrub("SSN: 123-45-6789"))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safe_logs-0.1.0.tar.gz.
File metadata
- Download URL: safe_logs-0.1.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/7.0.11-76070011-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e2517dbcd10ca0679a3b768fd932dd6f6acf22d4ca8439c2adb5e0818bfc434
|
|
| MD5 |
eb39465267773bfa1751ea6700a80295
|
|
| BLAKE2b-256 |
a2762c1eb49e872291262584a224398e79b3a0271f084c2efe7a8756b1aba823
|
File details
Details for the file safe_logs-0.1.0-py3-none-any.whl.
File metadata
- Download URL: safe_logs-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/7.0.11-76070011-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
561957b9eb3e5004b0ac163ba340062fc6d11365af31fdad14fa8a4ac4cc68ea
|
|
| MD5 |
66c02a08dfdcd57523cfee65e49f6e2a
|
|
| BLAKE2b-256 |
94fb26fd1155b8f85de050fb0801013927938edb223f159bc29ce034968e3fef
|