跳转到主要内容

用于轻松记录当前上下文信息的工具

项目描述

context_logging

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

描述

用于轻松记录当前上下文信息的工具。

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

安装

pip install context_logging

用法

使用上下文设置记录

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}

作为上下文管理器

from context_logging import Context, current_context

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

assert 'var' not in current_context

允许任何上下文嵌套

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

作为装饰器

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

f()
assert 'var' not in current_context

使用开始/结束 [已弃用]

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

向/从当前上下文添加/删除值

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

显式上下文名称(否则将使用Python模块的路径)

with Context('my_context'):
    pass

在退出上下文时记录执行时间(可以通过log_execution_time=False参数禁用)

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

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

可以通过环境变量更改log_execution_time参数的默认值

export CONTEXT_LOGGING_LOG_EXECUTION_TIME_DEFAULT=0

上下文异常将填充当前上下文(可以通过fill_exception_context=False参数禁用)

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

可以通过环境变量更改fill_exception_context参数的默认值

export CONTEXT_LOGGING_FILL_EXEPTIONS_DEFAULT=0

我们可以设置数据到根上下文,它永远不会关闭

from context_logging import root_context

root_context['env'] = 'test'

如果您想将上下文传递给其他线程,请使用ContextVarExecutor

from context_logging import ContextVarExecutor

with ContextVarExecutor() as executor:
    executor.submit(...)

# OR

loop.set_default_executor(ContextVarExecutor())  # for asyncio loop

对于开发者

创建venv并安装依赖项

make init

安装git预提交钩子

make precommit_hook

运行linters,autoformat,测试等。

make pretty lint test

提升新版本

make bump_major
make bump_minor
make bump_patch

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源码分发

context_logging-1.2.0.tar.gz (5.1 kB 查看哈希值)

上传时间 源码

构建分发

context_logging-1.2.0-py3-none-any.whl (6.6 kB 查看哈希值)

上传时间 Python 3

由以下支持