`logging.log` doesn't naively work as audit hook for `sys.addaudithook`
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- documentation, security
Hướng nghiên cứu
Tái hiện hiện tượng đệ quy bằng các ví dụ sys.addaudithook và logging.warning, sau đó đọc logging.init, đặc biệt là Logger._log, findCaller() và ghi chú _srcfile, cùng với tài liệu về sys.addaudithook. Xác định hành vi được đề xuất nào hoặc thay đổi tài liệu nào là chấp nhận được, đồng thời xác minh rằng việc ghi log của audit hook không còn đệ quy hoặc thất bại một cách im lặng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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,
loggingshould not attempt to gather caller info in an audit hook (however, this requires tight coupling of theloggingandsysmodules, 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._logcan inspect it, and the documentation forsys.addaudithookshould recommend to usefind_caller=Falsefor 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.addaudithookshould recommend above workaround, i.e. temporarily setlogging._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.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 558
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
link-check link-check:sphinx-theme
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100