context_logging
Description
Tool for easy logging with current context information.
from context_logging import current_context
logging.info('before context')
# 2019-07-25 19:49:43 INFO before context
with Context('my_context'):
current_context['var'] = 1
logging.info('in context')
# 2019-07-25 19:49:43 INFO in context {'var': 1}
# 2019-07-25 19:49:43 INFO 'my_context: executed in 00:00:01 {'var': 1}'
logging.info('after context')
# 2019-07-25 19:49:43 INFO after context
Installation
pip install context_logging
Usage
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['var'] = 1
logging.info('message')
# 2019-07-25 19:49:43,892 INFO root message {'var': 1}
As contextmanager
from context_logging import Context, current_context
with Context(var=1):
assert current_context['var'] == 1
assert 'var' not in current_context
Any nesting of contexts is allowed
with Context(var=1):
assert current_context == {'var': 1}
with Context(val=2, var=2):
assert current_context == {'val': 2, 'var': 2}
assert current_context == {'var': 1}
assert 'var' not in current_context
As decorator
@Context(var=1)
def f():
assert current_context['var'] == 1
f()
assert 'var' not in current_context
With start/finish [DEPRECATED]
ctx = Context(var=1)
assert 'var' not in current_context
ctx.start()
assert current_context['var'] == 1
ctx.finish()
assert 'var' not in current_context
Add/remove values from current_context
with Context():
assert 'var' not in current_context
current_context['var'] = 1
assert current_context['var'] == 1
Explicit context name (else will be used path to the python module)
with Context('my_context'):
pass
Execution time logged on exit from context (it can be disabled with log_execution_time=False argument)
with Context('my_context'):
time.sleep(1)
# INFO 'my_context: executed in 00:00:01',
Default value for log_execution_time param can be changed with env
export CONTEXT_LOGGING_LOG_EXECUTION_TIME_DEFAULT=0
Exceptions from context are populated with current_context (it can be disabled with fill_exception_context=False argument)
try:
with Context(var=1):
raise Exception(1)
except Exception as exc:
assert exc.args = (1, {'var': 1})
Default value for fill_exception_context param can be changed with env
export CONTEXT_LOGGING_FILL_EXEPTIONS_DEFAULT=0
We can set data to root context that never will be closed
from context_logging import root_context
root_context['env'] = 'test'
If you want to pass context to other threads use ContextVarExecutor
from context_logging import ContextVarExecutor
with ContextVarExecutor() as executor:
executor.submit(...)
# OR
loop.set_default_executor(ContextVarExecutor()) # for asyncio loop
For developers
Create venv and install deps
make init
Install git precommit hook
make precommit_hook
Run linters, autoformat, tests etc.
make pretty lint test
Bump new version
make bump_major
make bump_minor
make bump_patch
Metadata
Release files for context_logging 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| context_logging-1.2.0.tar.gz | 5.1 kB | Without provenance | Details for context_logging-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| context_logging-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for context_logging-1.2.0-py3-none-any.whl |
Total release size: 11.7 kB
Release files / context_logging-1.2.0.tar.gz
| Download URL | context_logging-1.2.0.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
910cfdc843b52537608f01ba45ef0456da78a7b1a955f126cbe9153f99fe1530
|
|
BLAKE2b-256 checksum How to use checksums |
154df931791b78a24108c07008aa4c90f1c455e8d86ffb28518454fe89acc596
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0
|
Release files / context_logging-1.2.0-py3-none-any.whl
| Download URL | context_logging-1.2.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20b7df9038b7291af5a1e8e765bc418ea6d61b180b15d195fa546cdf51c6fefb
|
|
BLAKE2b-256 checksum How to use checksums |
767f73401166e730249d7bcc505b0cf5eccadc5b1708c6bb0dcf51304b9ea3b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0
|
Security for context_logging 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.