Skip to main content

aiologfields: inject Task-context-fields into loggers

Project description

https://travis-ci.org/cjrh/aiologfields.svg?branch=master https://coveralls.io/repos/github/cjrh/aiologfields/badge.svg?branch=master https://img.shields.io/pypi/pyversions/aiologfields.svg https://img.shields.io/github/tag/cjrh/aiologfields.svg https://img.shields.io/badge/install-pip%20install%20aiologfields-ff69b4.svg https://img.shields.io/pypi/v/aiologfields.svg https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg

aiologfields

aiologfields makes it easy to include correlation IDs, as well as other contextual information into log messages, across await calls and loop.create_task() calls. Correlation IDs are critically important for accurate telemetry in monitoring and debugging distributed microservices.

Instructions

It couldn’t be easier:

aiologfields.install()

After this, every single task created will have a logging_fields. To add a field to a LogRecord, simply apply it to any task:

t = loop.create_task(coro)
t.logging_fields.correlation_id = '12345'

If you’re using a logging handler that produces JSON output (like logjson!), or some other formatter that produces output with all fields in the LogRecord, you will find that each record within the context of the task will include an additional field called correlation_id with a value of 12345.

Demo

This is adapted from one of the tests:

aiologfields.install()
correlation_id = str(uuid4())
logger = logging.getLogger('blah')

async def cf2():
    logger.info('blah blah')

async def cf1():
    ct = asyncio.Task.current_task()
    ct.logging_fields.correlation_id = correlation_id
    await cf2()

loop.run_until_complete(cf1())

In the LogRecord produced inside cf2(), an additional field correlation_id is included, even though the field was set in coroutine function cf1().

It would also have worked if cf2() had been executed in a separate task itself, since the logging_fields namespace is copied between nested tasks.

Project details


Download files

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

Source Distribution

aiologfields-2017.12.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

aiologfields-2017.12.1-py2.py3-none-any.whl (16.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file aiologfields-2017.12.1.tar.gz.

File metadata

File hashes

Hashes for aiologfields-2017.12.1.tar.gz
Algorithm Hash digest
SHA256 8033db6a660acbfd5619b73a11fbb7cb3c66bf7b6634b639d4ed1e2bc31bb4a4
MD5 68f607a6aca018c4306085ba42275cdc
BLAKE2b-256 f96efbf411bd84af650315883a9ad61b1ff2e57b6582a1822bddfe434dec96ae

See more details on using hashes here.

File details

Details for the file aiologfields-2017.12.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for aiologfields-2017.12.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a3452f15e283bffb786c71eb4229a03cc3d2064270fb17a712f736dd00b8a9fe
MD5 fbc433b1b3b9cd561af27a3e21cfef44
BLAKE2b-256 e325c4f10b176bb119431ddb8b37afb173814dae843423af948a2142d33028de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page