Skip to main content

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=****"}

Metadata

Release files for logredactor 0.0.2

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

Source distribution (sdist)

Source distribution for logredactor 0.0.2
File Size Uploaded
logredactor-0.0.2.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logredactor 0.0.2
File Interpreter ABI Platform
logredactor-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.6 kB

Release files / logredactor-0.0.2.tar.gz

Download URL logredactor-0.0.2.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0e10117aab8af3edffacb5aba21b37787822c95a6b44546f7930443b9906fcb5
BLAKE2b-256 checksum
How to use checksums
0b7497586e8166566489babf8dad1cf7aec00a26ccb644b362405aaa73b36ef5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.16

Release files / logredactor-0.0.2-py3-none-any.whl

Download URL logredactor-0.0.2-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f76cac75db8c01a32c1f8f139674a9393ebde94f1e95c34c143573a8483ee1d
BLAKE2b-256 checksum
How to use checksums
d0ea5d9cac91a985532760eb2c794cf3a23e8643b6a3ac3b7051b4b369e9a5fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.16

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

1 release file

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