Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for python-cloud-logger, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size python_cloud_logger-0.0.8-py3-none-any.whl (3.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size python-cloud-logger-0.0.8.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page