Skip to main content

Tool for easy logging with current context information

Project description

context_logging

pypi Python: 3.7+ Downloads Build Status Code coverage License: MIT Code style: black

Description

Tool for easy logging with current context information

Installation

pip install context_logging

Usage

As contextmanager

from context_logging import Context, current_context

with Context(val=1):
    assert current_context['val'] == 1

assert 'val' not in current_context

Any nesting of contexts is allowed

with Context(val=1):
    assert current_context == {'val': 1}

    with Context(val=2, var=2):
        assert current_context == {'val': 2, 'var': 2}

    assert current_context == {'val': 1}

assert 'val' not in current_context

As decorator

@Context(val=1)
def f():
    assert current_context['val'] == 1

f()
assert 'val' not in current_context

With start/finish

ctx = Context(val=1)
assert 'val' not in current_context

ctx.start()
assert current_context['val'] == 1

ctx.finish()
assert 'val' not in current_context

Write/delete to current_context

with Context():
    assert 'val' not in current_context
    current_context['val'] = 1
    assert current_context['val'] == 1

Explicit context name (else will be used path to the python module)

with Context(name='my_context'):
    pass

Setup logging with context

import logging
from context_logging import current_context, setup_log_record

logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s %(context)s', level=logging.INFO)
setup_log_record()

current_context['val'] = 1
logging.info('message')

# 2019-07-25 19:49:43,892 INFO root message {'val': 1}

Execution time logged on exit from context (disable with log_execution_time=False)

with Context(name='my_context'):
    time.sleep(1)

# INFO 'my_context: executed in 00:00:01',

Exceptions from context are populated with current_contextdisable with fill_exception_context=False)

try:
    with Context(val=1):
        raise Exception(1)
except Exception as exc:
    assert exc.args = (1, {'val': 1})

We can set data to root context that never will be closed

from context_logging import root_context

root_context['env'] = 'test'

For autofilling thread context in async code

from contextvars_executor import ContextVarExecutor

loop.set_default_executor(ContextVarExecutor())

For developers

Create venv and install deps

make init

Install git precommit hook

make precommit_install

Run linters, autoformat, tests etc.

make pretty lint test

Bump new version

make bump_major
make bump_minor
make bump_patch

License

MIT

Change Log

Unreleased

  • ...

0.2.0 - 2019-07-25

  • context as attr of log record

0.1.0 - 2019-07-23

  • initial

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

context_logging-0.2.0.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

context_logging-0.2.0-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

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