A function decorator for beautiful and complete logging
Project description
Secure Logger
A Python decorator to generate redacted and nicely formatted log entries. Works on all callables: class, class methods, Python module functions. Recursively redacts Python dictionary key values based on a customizable list of keys.
Usage
As a decorator
from secure_logger.decorators import secure_logger
class TestClass(object):
@secure_logger()
def test_2(self, test_dict, test_list):
pass
# call your method, passing some sensitive data
test_dict = {
'not_a_sensitive_key': 'you-can-see-me',
'aws-access-key_id': conf.AWS_ACCESS_KEY_ID,
'aws-secret-access-key': conf.AWS_SECRET_ACCESS_KEY
}
test_list = ['foo', 'bar']
o = TestClass()
o.test_2(test_dict=test_dict, test_list=test_list)
Log output:
INFO:secure_logger: __main__.TestClass().test_2() keyword args: {
"test_dict": {
"not_a_sensitive_key": "you-can-see-me",
"aws-access-key-id": "*** -- REDACTED -- ***",
"aws-secret-access-key": "*** -- REDACTED -- ***"
},
"test_list": [
"foo",
"bar"
]
}
As library functions
from secure_logger.masked_dict import masked_dict, masked_dict2str
test_dict = {
'not_a_sensitive_key': 'you-can-see-me',
'aws-access-key_id': conf.AWS_ACCESS_KEY_ID,
'aws-secret-access-key': conf.AWS_SECRET_ACCESS_KEY
}
print(masked_dict2str(test_dict))
Output:
{
"not_a_sensitive_key": "you-can-see-me",
"aws-access-key-id": "*** -- REDACTED -- ***",
"aws-secret-access-key": "*** -- REDACTED -- ***"
}
Installation
pip install secure-logger
Configuration
secure_logger accepts optional parameters.
- sensitive_keys: a Python list of dictionary keys. Not case sensitive.
- message: a string value that will replace the sensitive key values
- indent: number of characters to indent JSON string output when logging output
class MyClass():
@secure_logger(sensitive_keys=["password", "token", "crown_jewels"], message="***", indent=4)
def another_def(self):
Configuration Defaults
DEFAULT_REDACTION_MESSAGE = "*** -- REDACTED -- ***"
DEFAULT_INDENT = 4
DEFAULT_SENSITIVE_KEYS = [
"password",
"token",
"client_id",
"client_secret",
"Authorization",
"secret",
"access_key_id",
"secret_access_key",
"access-key-id",
"secret-access-key",
"aws_access_key_id",
"aws_secret_access_key",
"aws-access-key-id",
"aws-secret-access-key",
]
Contributing
Pull requests are welcome, and you can also contact Lawrence McDaniel directly.
Getting Started With Local development
- Use the same virtual environment that you use for edx-platform
- Ensure that your Python interpreter to 3.8x
- install black: https://pypi.org/project/black/
- install flake8: https://flake8.pycqa.org/en/latest/
- install flake8-coding: https://pypi.org/project/flake8-coding/
# Run these from within your edx-platform virtual environment
python3 -m venv venv
source venv/bin/activate
pip install -r requirements/local.txt
pip install pre-commit black flake8
pre-commit install
Local development good practices
- run
black
on modified code before committing. - run
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- run
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- run
pre-commit run --all-files
before pushing. see: https://pre-commit.com/
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
secure-logger-0.1.3.tar.gz
(19.1 kB
view details)
Built Distribution
File details
Details for the file secure-logger-0.1.3.tar.gz
.
File metadata
- Download URL: secure-logger-0.1.3.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b65ce839cbf7e1444f734c65691724e1145a16ef69b9ddcab157d7fcf751e03 |
|
MD5 | c84c87b305c99763b5ff0d3d13c2e51e |
|
BLAKE2b-256 | be09d3946235b0980143d62c9984a5620da99cc7552d1e2c6f5a5ba11572056a |
Provenance
File details
Details for the file secure_logger-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: secure_logger-0.1.3-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4fcba5e55ec1420340a8cc4f80771398e308746dfc0c9a578c52b7fc5c17917 |
|
MD5 | e14e96e5c592c43cd08c58998c579d8c |
|
BLAKE2b-256 | 6f1b591dbbaa05d8c1deea1272a735014bc702f10100bdbbe5b1f8dbf44adafd |