Redact logs based on regex filters
Project description
Log Redactor
Redacts data in python logs based on regex filters passed in by the user.
This will work with json logging formats as well and also with nested data in the extra argument.
Examples
import re
import logging
import logredactor
# Create a logger
logger = logging.getLogger()
# Add the redact filter to the logger with your custom filters
redact_patterns = [re.compile(r'\d+')]
# if no `default_mask` is passed in, 4 asterisks will be used
logger.addFilter(logredactor.RedactingFilter(redact_patterns, default_mask='xx'))
logger.warning("This is a test 123...")
# Output: This is a test xx...
Python only applies the filter on that logger, so any other files using logging will not get the filter applied. To have this filter applied to all loggers do the following
import re
import logging
import logredactor
from pythonjsonlogger import jsonlogger
# Create a pattern to hide api key in url. This uses a _Positive Lookbehind_
redact_patterns = [re.compile(r'(?<=api_key=)[\w-]+')]
# Override the logging handler that you want redacted
class RedactStreamHandler(logging.StreamHandler):
def __init__(self, *args, **kwargs):
logging.StreamHandler.__init__(self, *args, **kwargs)
self.addFilter(logredactor.RedactingFilter(redact_patterns))
root_logger = logging.getLogger()
sys_stream = RedactStreamHandler()
# Also set the formatter to use json, this is optional and all nested keys will get redacted too
sys_stream.setFormatter(jsonlogger.JsonFormatter('%(name)s %(message)s'))
root_logger.addHandler(sys_stream)
logger = logging.getLogger(__name__)
logger.error("Request Failed", extra={'url': 'https://example.com?api_key=my-secret-key'})
# Output: {"name": "__main__", "message": "Request Failed", "url": "https://example.com?api_key=****"}
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 logredactor-0.0.2.tar.gz.
File metadata
- Download URL: logredactor-0.0.2.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e10117aab8af3edffacb5aba21b37787822c95a6b44546f7930443b9906fcb5
|
|
| MD5 |
85a671601e7ef7d66331fd882f2ea032
|
|
| BLAKE2b-256 |
0b7497586e8166566489babf8dad1cf7aec00a26ccb644b362405aaa73b36ef5
|
File details
Details for the file logredactor-0.0.2-py3-none-any.whl.
File metadata
- Download URL: logredactor-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f76cac75db8c01a32c1f8f139674a9393ebde94f1e95c34c143573a8483ee1d
|
|
| MD5 |
dbaed6d4150b5db6069147c7ed13df31
|
|
| BLAKE2b-256 |
d0ea5d9cac91a985532760eb2c794cf3a23e8643b6a3ac3b7051b4b369e9a5fc
|