Package: context-log
Python context-log libraryAboutcontext-log is a simple library to emit contextual information in structured logs (JSON).It works particularly well in a Docker or Serverless (e.g. AWS Lambda) environment where a single thread executes a request and produces a response.The library uses python threading to store contextual information that is automatically added to all subsequent logs in acontextMapfield.Because the library uses the Python thread local context it works across packages and modules in a given project.The approach is loosely based on theLog4j 2 API Thread Context.UsageStructured logging can be achieved with thepython-json-logger library.Simply add project dependencies to requirements.txt:python_json_loggerPyYAMLcontext-logAdd the code below to the main code module.Add the following YAML configuration in theresources/logging.yamlfile, which outputs JSON structured logs tostdout.version:1formatters:json:class:.jsonlogger.JsonFormatterformat:'%(asctime)s%(name)s%(levelname)s%(message)s%(filename)s'handlers:console:class:logging.StreamHandlerlevel:DEBUGformatter:jsonroot:level:DEBUGhandlers:-consoleUse the context_log library to emit logs. Example below.importlogging.configimportyamlwithopen('resources/logging.yaml','r')aslog_config_file:logging.config.dictConfig(yaml.safe_load(log_config_file))fromcontext_logimportContextLogdefhandler(event,context):# Clear context (e.g. re-use) and get loggerlog=ContextLog.get_logger('handler',True)log.info('start')ContextLog.put('ip','1.2.3.4')# Helper to add start time in ISO and epoch timeContextLog.put_request_start_time()# Process requestsleep(0.1)# Helper to add end time in ISO and epoch time# as well as duration in millisecondsContextLog.put_request_end_time()log.info('end')First log info event:{"asctime":"2019-09-19 11:53:20,479","name":"handler","levelname":"INFO","message":"start","filename":"test_example.py","contextMap":{}}Second log info event:{"asctime":"2019-09-19 11:53:20,580","name":"handler","levelname":"INFO","message":"end","filename":"test_example.py","contextMap":{"ip":"1.2.3.4","start-time":"2019-09-19T11:53:20.480085","epoch-start-time":1568890400.480085,"end-time":"2019-09-19T11:53:20.580513","epoch-end-time":1568890400.580513,"duration":100.428}}The DetailThe standard logger is wrapped by aLoggerAdapter. It is therefore imperative that theContextLog.get_logger(name='<name>', clear=True|False)call is made to get the logger to emit contextual logs.Useclear=Truewhen starting a new request in order to clear the previous context if the thread is re-used. This is typically the case in thread pools and in AWS Lambda's.To manipulate or retrieve thecontextMapuse the following methods:clear()put(key, value)get(key)get_map()There are also a number of helpers in an attempt to standardise log outputcontextMapfields:put_request_id(request_id)put_request_method(request_method)put_request_path(request_path)put_response_status(response_status)put_start_time()put_end_time()put_request_user_id(request_user_id)put_request_client_id(request_client_id)put_request_primary_ip(primary_ip)put_request_client_ip(client_ip)put_request_viewer_country(viewer_country)put_trigger_source(trigger_source)ContributingPull requests are more than welcome.Running pytestCreate virtualenv, download dependencies and run tests:python3-mvenv.venvsource.venv/bin/activate
pip3install-rtests/requirements.txt
pip3install-e.
pytestRunning toxpip3install--user--upgradetox
toxReleasing library to PyPIShort version from thePackaging Python Projectssite.Install the release tools:python3-mpipinstall--user--upgradesetuptoolswheeltwineRemove old distribution(s):rm -rf dist/Build the context-log package:python3setup.pysdistbdist_wheelUpload context-log first to Test PyPI:python3-mtwineupload--repository-urlhttps://test.pypi.org/legacy/dist/*