跳转到主要内容

通过人类可读的方式装饰日志函数参数和返回值的装饰器

项目描述

logwrap

https://github.com/python-useful-helpers/logwrap/workflows/Python%20package/badge.svg https://coveralls.io/repos/github/python-useful-helpers/logwrap/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/logwrap.svg https://img.shields.io/pypi/pyversions/logwrap.svg https://img.shields.io/pypi/status/logwrap.svg https://img.shields.io/github/license/python-useful-helpers/logwrap.svg https://img.shields.io/badge/code%20style-black-000000.svg

logwrap是一个辅助工具,用于在函数调用时以人类可读的格式记录函数参数和调用结果。为什么?因为随着项目的增长,对*cite>*args, **kwargs的日志记录变得毫无意义,您需要更多的调用日志详情。

缺点

  • 日志记录不是单行。

优点

本包包括辅助工具

  • logwrap - 主要辅助工具。与 LogWrap 相同。

  • LogWrap - 具有logwrap实现的类。可以直接使用。

  • pretty_repr

  • pretty_str

  • PrettyFormat

  • LogOnAccess - 在成功获取/设置/删除或失败时进行记录的属性。

用法

logwrap

主要装饰器。可以作为无参数使用(@logwrap.logwrap)和有参数使用(@logwrap.logwrap())。无参数使用简单调用,所有位置使用默认值。

使用签名中的参数的有参数用法

@logwrap.logwrap(
    log=None,  # if not set: try to find LOGGER, LOG, logger or log object in target module and use it if it logger instance. Fallback: logger named logwrap
    log_level=logging.DEBUG,
    exc_level=logging.ERROR,
    max_indent=20,  # forwarded to the pretty_repr,
    max_iter=0,  # forwarded to the pretty_repr, max number of items in the Iterable before ellipsis. Unlimited if 0.
    blacklisted_names=None,  # list argument names, which should be dropped from log
    blacklisted_exceptions=None,  # Exceptions to skip details in log (no traceback, no exception details - just class name)
    log_call_args=True,  # Log call arguments before call
    log_call_args_on_exc=True,  # Log call arguments if exception happens
    log_traceback = True,  # Log traceback if exception happens
    log_result_obj=True,  # Log result object
)

用法示例

@logwrap.logwrap()
def foo():
    pass

等同于

@logwrap.logwrap
def foo():
    pass

获取无参数使用的装饰器

get_logs = logwrap.logwrap()  # set required parameters via arguments

type(get_logs) == LogWrap  # All logic is implemented in LogWrap class starting from version 2.2.0

@get_logs
def foo():
    pass

调用示例(Python 3.8)

import logwrap

@logwrap.logwrap
def example_function1(
        arg0: str,
        /,
        arg1: str,
        arg2: str='arg2',
        *args,
        kwarg1: str,
        kwarg2: str='kwarg2',
        **kwargs
) -> tuple():
    return (arg0, arg1, arg2, args, kwarg1, kwarg2, kwargs)

example_function1('arg0', 'arg1', kwarg1='kwarg1', kwarg3='kwarg3')

此代码在执行过程中将生成日志记录

Calling:
'example_function1'(
    # POSITIONAL_ONLY:
    arg0='arg0',  # type: str
    # POSITIONAL_OR_KEYWORD:
    arg1='arg1',  # type: str
    arg2='arg2',  # type: str
    # VAR_POSITIONAL:
    args=(),
    # KEYWORD_ONLY:
    kwarg1='kwarg1',  # type: str
    kwarg2='kwarg2',  # type: str
    # VAR_KEYWORD:
    kwargs={
        'kwarg3': 'kwarg3',
    },
)
Done: 'example_function1' with result:

 (
    'arg0',
    'arg1',
    'arg2',
    (),
    'kwarg1',
    'kwarg2',
    {
        'kwarg3': 'kwarg3',
    },
 )

LogWrap

示例构造和从测试中读取

log_call = logwrap.LogWrap()
log_call.log_level == logging.DEBUG
log_call.exc_level == logging.ERROR
log_call.max_indent == 20
log_call.blacklisted_names == []
log_call.blacklisted_exceptions == []
log_call.log_call_args == True
log_call.log_call_args_on_exc == True
log_call.log_traceback == True
log_call.log_result_obj == True

在对象变化时,变量类型将被验证。

在特殊情况下,当需要对参数进行特殊处理以记录日志(在日志中隐藏或更改参数)时,可以通过覆盖 pre_process_parampost_process_param 来实现。

有关详细信息,请参阅API文档。

pretty_repr

这是用于在复杂对象上生成人类可读repr的指定辅助工具。签名是自文档的

def pretty_repr(
    src,  # object for repr
    indent=0,  # start indent
    no_indent_start=False,  # do not indent the first level
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)

pretty_str

这是用于在复杂对象上生成人类可读str的指定辅助工具。签名是自文档的

def pretty_str(
    src,  # object for __str__
    indent=0,  # start indent
    no_indent_start=False,  # do not indent the first level
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)
限制

类似于字典的对象总是用 {} 标记以供阅读,即使它是 collections.OrderedDict(标准表示为元组列表)。

可迭代类型没有声明,仅使用括号。

字符串和字节看起来相同(它的 __str__,而不是 __repr__)。

PrettyFormat

PrettyFormat 是主要的格式化实现类。pretty_reprpretty_str 使用此类的子类 PrettyReprPrettyStr 的实例。此类主要是出于类型的原因而公开的。对象签名

def __init__(
    self,
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)

可调用对象(PrettyFormat 实例)签名

def __call__(
    self,
    src,  # object for repr
    indent=0,  # start indent
    no_indent_start=False  # do not indent the first level
)

采用您的代码

可以通过实现特定的魔法方法来覆盖您的类的 pretty_repr 行为

def __pretty_repr__(
    self,
    parser  # PrettyFormat class instance,
    indent  # start indent,
    no_indent_start  # do not indent the first level
):
    return ...

此方法将在您的对象上替代 __repr__ 执行。

def __pretty_str__(
    self,
    parser  # PrettyFormat class instance,
    indent  # start indent,
    no_indent_start  # do not indent the first level
):
    return ...

此方法将在您的对象上替代 __str__ 执行。

LogOnAccess

这种特殊的属性在需要大量类似方式记录属性但不需要编写大量代码的情况下非常有用。

基本API符合 property,但还可以自定义记录器、日志级别和日志条件。

用法示例

  1. 简单用法。所有默认。重用实例中的记录器

    • 如果存在,则使用名称 loggerlog,

    • 如果存在,则使用实例模块中的名称 LOGGERlog,

    • 否则使用内部 logwrap.log_on_access 记录器。

import logging

class Target(object):

    def init(self, val='ok')
        self.val = val
        self.logger = logging.get_logger(self.__class__.__name__)  # Single for class, follow subclassing

    def __repr__(self):
        return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

    @logwrap.LogOnAccess
    def ok(self):
        return self.val

    @ok.setter
    def ok(self, val):
        self.val = val

    @ok.deleter
    def ok(self):
        self.val = ""
  1. 用于类的全局记录器

class Target(object):

  def init(self, val='ok')
      self.val = val

  def __repr__(self):
      return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

  @logwrap.LogOnAccess
  def ok(self):
      return self.val

  @ok.setter
  def ok(self, val):
      self.val = val

  @ok.deleter
  def ok(self):
      self.val = ""

  ok.logger = 'test_logger'
  ok.log_level = logging.INFO
  ok.exc_level = logging.ERROR
  ok.log_object_repr = True  # As by default
  ok.log_before = True  # As by default
  ok.log_success = True  # As by default
  ok.log_failure = True  # As by default
  ok.log_traceback = True  # As by default
  ok.override_name = None  # As by default: use original name

测试

本包 logwrap 的主要测试机制是使用 tox。可以通过 tox -l 收集可用的环境。

CI/CD 系统

GitHub: 用于功能测试。

项目详情


发布历史 发布通知 | RSS 源

下载文件

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

源分布

logwrap-11.1.0.tar.gz (42.8 kB 查看哈希值)

上传时间

构建分布

logwrap-11.1.0-py3-none-any.whl (27.6 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页