This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Allow easy display of local variables in log messages?
Type: enhancement Stage: patch review
Components: Versions: Python 3.11
Status: open Resolution:
Dependencies: 22936 Superseder:
Assigned To: Nosy List: cvrebert, iritkatriel, moi90, ncoghlan, rbcollins
Priority: normal Keywords: patch

Created on 2015-03-06 02:12 by ncoghlan, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 29299 moi90, 2021-10-29 06:27
Messages (4)
msg237319 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-03-06 02:12
Issue #22936 added support for easily displaying local variables in tracebacks to the unittest and traceback modules.

Would it be worth also making this capability readily available in the logging APIs that display traceback messages?

The main argument against it is that automatically logging local variables in tracebacks is a *really* good way to introduce sensitive information leaks into your log messages. "Safe repr" type objects (which automatically mask sensitive details in __repr__()) can mitigate this, but we don't provide such an object in the standard library (not even for bytes, bytearray or str, which are the typical containers for potentially sensitive data like passwords).
msg237323 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-03-06 02:27
Yes, for debugging etc this can be very useful. I suggest further extending the new traceback interface to allow a filtering/transform hook of some sort, to allow folk more granular control than just repr overloading.
msg404737 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-10-22 09:42
See also 43656.
msg405287 - (view) Author: Martin (moi90) * Date: 2021-10-29 06:27
In this pull request[1] I introduced a `format_locals` which can be provided to customize the serialization of the local variables of each frame. This could be used to filter out variables with certain names, for example. In this context, you could use a certain naming convention for variables with sensitive information and filter them out:

def _format_locals(filename, lineno, name, locals):
    return {k: repr(v) for k,v in locals.items() if not k.endsswith("_sensitive")}

traceback.TracebackException.from_exception(e, capture_locals=True, format_locals=format_locals).format()

(This should be exactly what Robert was suggesting.)

Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67785
2021-10-29 06:27:23moi90setnosy: + moi90
messages: + msg405287
pull_requests: + pull_request27572

keywords: + patch
stage: patch review
2021-10-22 09:42:19iritkatrielsetnosy: + iritkatriel
messages: + msg404737
2021-10-22 09:41:33iritkatrielsettype: enhancement
versions: + Python 3.11, - Python 3.5
2015-08-26 00:33:02cvrebertsetnosy: + cvrebert
2015-03-06 02:27:44rbcollinssetnosy: + rbcollins
messages: + msg237323
2015-03-06 02:13:00ncoghlansetdependencies: + traceback module has no way to show locals
2015-03-06 02:12:46ncoghlancreate