`logging.log` doesn't naively work as audit hook for `sys.addaudithook`

未关闭
#98,105 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

使用 sys.addaudithook 和 logging.warning 示例重现递归,然后阅读 logging.init,尤其是 Logger._log、findCaller() 和 _srcfile 注释,同时参阅 sys.addaudithook 文档。确定提出的行为或文档更改中哪些是可接受的,并验证 audit hook 日志记录不再递归或静默失败。

由索引模型根据 Issue 内容生成。

描述

stdlib type-bug

Bug report

Documentation for sys.addaudithook states "Hooks can then log the event [...]".
This works when I don't log, but instead print:

sys.addaudithook(print)
id(0)  # printed

However, nothing gets logged when I follow the suggestion to log, like:

sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s'))
id(0)  # NOT logged

The cause is that logging.Logger._log (called by logging.warning) tries to find the caller (to populate module, lineno, funcName log format fields). For this, logging.Logger._log itself then causes audit events, which again call the audit hook. That results in infinite recursion, i.e. a RecursionError exception, which aborts the audit hook, and silences logging.

Workaround

By (temporarily) modifying logging._srcfile, the attempt to find the caller is skipped (and the log format fields are instead populated as "unknown"), infinite recursion and exception are prevented, and logging does happen:

sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s'))
logging._srcfile = None  # prevent finding caller
id(0)  # logged

This is existing functionality, only (?) documented in source (file logging.__init__, line 188: "Setting _srcfile to None will prevent findCaller() from being called. This way, you can avoid the overhead of fetching caller information"). Though it works, the workaround is hard to find and not ideal.

Solutions

  • Ideally, logging should not attempt to gather caller info in an audit hook (however, this requires tight coupling of the logging and sys modules, so they understand each others' internals)
  • Equally user-friendly, audit events would not be generated within audit hooks (that risks incompleteness of auditing, but so does silencing logging)
  • Less user-friendly, a keyword parameter (e.g. find_caller: bool = True) could be added to the signature of each log function, logging.Logger._log can inspect it, and the documentation for sys.addaudithook should recommend to use find_caller=False for logging in audit hooks
# logging.Logger._log
if logging._srcfile and find_caller:
    ...

# to use
sys.addaudithook(functools.partial(logging.warning, 'Audit hook %s %s', find_caller=False))
id(0)  # logged
  • At the minimum, the documentation for sys.addaudithook should recommend above workaround, i.e. temporarily set logging._srcfile = None
def my_log(event, args):
    save_srcfile = logging._srcfile
    logging._srcfile = None
    logging.warning('Audit hook %s %s', event, args)
    logging._srcfile = save_srcfile

sys.addaudithook(my_log)
id(0)  # logged

Environment

The above issue exists in all Python versions supporting sys.addaudithook, i.e. 3.8, 3.9, 3.10, and 3.11.
I tested on 3.8.10, 3.9.13, 3.10.4, and 3.110rc2 on Windows 11 64 bit.

主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。