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
from pythonjsonlogger.formatters import RedactJsonFormatter
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.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e48a6c4c575d0b71af3ba3b551c96a2e516b2f47864c9a282fc33fcfaed91cc8 |
|
MD5 | 825c391947d7aff90a97b80b5838f426 |
|
BLAKE2b-256 | fd57cbe6195bf8d61f98702046ab47aaae103c26a441eea048fe8d4ff401989c |
Hashes for python_cloud_logger-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85b817f023735fa4188b3edef7ea044993cb34a0377912507547421064e90573 |
|
MD5 | 2091eef31b21c1bae8533deac3e5f23f |
|
BLAKE2b-256 | 4b044d580c3c3fe69dea679c895a8716346d877db6a57bc48331821630eed7f1 |