Tool for easy logging with current context information
Project description
context_logging
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)
Built Distribution
Close
Hashes for context_logging-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d70ad193e9baea41b4c702f389f491348c0db82ac18a7e0992dcc36b4e14665 |
|
MD5 | 1f9a2d154a44d9a122f7d0b1aaa3fea0 |
|
BLAKE2b-256 | 5a50249f206cf7834f7b7a94b59a72a9dac200747b6c32e6bf84f66160466e8a |