`logging.log` doesn't naively work as audit hook for `sys.addaudithook`
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 25/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- python
- Domaine
- documentation, security
Piste de recherche
Reproduisez la récursion à l’aide des exemples sys.addaudithook et logging.warning, puis lisez logging.init, en particulier Logger._log, findCaller() et la note sur _srcfile, ainsi que la documentation de sys.addaudithook. Déterminez quel comportement proposé ou quelle modification de la documentation est acceptable et vérifiez que la journalisation via l’audit hook n’entre plus en récursion et n’échoue plus silencieusement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Merge moyen
- 1 j 9 h
- PR mergées (30 j)
- 558
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/cpython
-
docs pending
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
stdlib type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
stdlib type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
build type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
stdlib topic-email type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Toutes les issues de python/cpython
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
zostera/django-bootstrap4#894 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
use-agent-os/agent-os#3276 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
zilliztech/memsearch#759 ·