misprint
An integration with the Python standard library's logging module that masks secrets in log messages before they are emitted. This project was heavily inspired by an excellent blog post which has been updated/adapted for modern Python versions.
Misprint is fully type-annotated for easier integration with mypy.
Table of Contents
Installation
pip install misprint
License
misprint is distributed under the terms of the MIT
license.
Usage
misprint.Misprinter
You can use the Misprinter class to redact exact string matches or regular expressions
within a string:
misprinter = Misprinter(token=["my_secret_token", re.compile(r"^ghp_\w+")])
assert misprinter.mask("this is my_secret_token") == "this is ****"
assert (
misprinter.mask("github tokens: ghp_abc123 ghp_def456")
== "github tokens: **** ****"
)
If you need to add a mask for new data to an existing instance then you can use the
add_mask_for method:
misprinter = Misprinter()
assert misprinter.mask("a secret1234") == "a secret1234"
misprinter.add_mask_for("secret1234")
assert misprinter.mask("a secret1234") == "a ****"
You can also initialise your Misprinter instance with use_named_masks=True
if you would like to be able to identify what data has been masked more easily:
misprinter = Misprinter(use_named_masks=True)
misprinter.add_mask_for("another_secret", name="database password")
assert (
misprinter.mask("printing another_secret")
== "printing <'database password' (value removed)>"
)
misprint.MisprintFilter
misprint also provides a logging.Filter subclass, which integrates with the
Python standard library's logging module to enable redaction of log messages.
MisprintFilter is a subclass of Misprinter, so inherits all of the methods
detailed above. This is useful, as the filter can be connected to the loggers
that are configured for a program at startup, and new secrets can be
conveniently added to the filter to be redacted in the logs:
import logging
import sys
import misprint
logging.basicConfig(
datefmt="[%X]",
handlers=[logging.StreamHandler(sys.stderr)], # plus others
)
misprinter = misprint.MisprintFilter()
for handler in logging.getLogger().handlers:
handler.addFilter(misprinter)
A_TOKEN = "asdf1234"
misprinter.add_mask_for(A_TOKEN)
log.error("Bad token: %s", A_TOKEN)
Metadata
Release files for misprint 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| misprint-0.2.0.tar.gz | 8.8 kB | Without provenance | Details for misprint-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| misprint-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for misprint-0.2.0-py3-none-any.whl |
Total release size: 14.0 kB
Release files / misprint-0.2.0.tar.gz
| Download URL | misprint-0.2.0.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c76df3a7f4f2e01e63169f8dfc78e73a4ab7ec56c8d86852fea2280bdb91ada8
|
|
BLAKE2b-256 checksum How to use checksums |
34079a2bfb6855f27b08886114370a7bb2d9d20c22f657406f90338949af9b25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / misprint-0.2.0-py3-none-any.whl
| Download URL | misprint-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
68e465f7033cec6d21f4a12df3a1e22dbe7ee7fb29205594c94565121f12aa6f
|
|
BLAKE2b-256 checksum How to use checksums |
80fb9bfeffd1961b467912712f5b86f5af9ab363549c7b7a638aa4ac94e5ae4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for misprint 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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.