A logger with thread local storage for logging context in all logs without repeating the code
Project description
python-cloud-logger
A logger with thread local storage for logging context in all logs without repeating the code. This is a json logger where some contextual fields need to be included in every log per request/thread in a safe and efficient way.
Note that, all the loggers retrieved on that particular thread ( by using logging.getLogger ) will inherit the context.
Usage
from pythoncloudlogger import *
logger=logging.getLogger("myapp-logger")
logger.updateContext({
"requestId" : "1245"
})
logger.info("Request successfully processed")
logger.clearContext()
RedactJsonFormatter
This formatter is an extension of pythonjsonlogger ( https://pypi.org/project/pythonjsonlogger ). This extension will allow you to mask fields in the json before it logs. The redaction keys are case insensitive.
Usage
# Set the environment variables
#
import logging,os
os.environ["redactionKeys"]="x-api-key,Authorization"
os.environ["redactionString"]="<secret>"
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = pythoncloudlogger.RedactJsonFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info({
"message" : "My request details",
"headers" : {
# will be masked
"x-api-key" : "sdfsf",
"another-level" :{
# will be masked
"Authorization" : "Bearer dfsfd"
}
}
})
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
Hashes for python-cloud-logger-0.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 830ecae3320dd5ed0ccdb44bd7a34bcee55a1af5cda44ef805e3bebc5c2f0c60 |
|
MD5 | 2273d35f39addd96eb9b3c9e10831c61 |
|
BLAKE2b-256 | e7fc9a8309d125587566df480b851319fd644ddac2a43b03d4397e2c8a5143fe |
Hashes for python_cloud_logger-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dbc7014993c75fce701b6a94c56c5c5c9928dfad0f8882bb1c4e60b8b86491f |
|
MD5 | 05f6ca760fe276fe45fcf6a2983859a0 |
|
BLAKE2b-256 | 3c0e0850903ff9fadd28e691b115f16cd5637c5fad9bada880e89f95b8bd1879 |