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.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6437cb0f7410d6359d2cc6bd066ff69294a5b414f8c10a8f13a52fdc7298095 |
|
MD5 | dc66f0d3dd57970d552f998e687127f6 |
|
BLAKE2b-256 | 8070950ac938feabe15d08e5dccd563fdc025914686e3b30cdbe886c12c60d05 |
Hashes for python_cloud_logger-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce0ecfe92d93e43b3aac003eb5b6fd149760e2646d540217798217ad881cb014 |
|
MD5 | 3101d2c3b9d089403d179aeb23b8bfe8 |
|
BLAKE2b-256 | c96b5a4c6ac750a54f2af53e131ef6f5c58354987909549c6230f300e0759094 |