tstringlogger
Adds support for using t-strings in log messages while preserving all interpolated values.
Usage example:
>>> import logging
>>> from tstringlogger import TStringLogRecord
>>> test1, test2 = "hello", "world"
>>>
>>> # Set the default log record factory to one that supports t-strings
>>> logging.setLogRecordFactory(TStringLogRecord)
>>>
>>> logging.info(t"{test1=}, {test2}")
INFO:root:test1='hello', world
>>>
>>> # Standard logging still works normally
>>> logging.info("test1=%r, %s", test1, test2)
INFO:root:test1='hello', world
In this example the log record that was produced by the t-string message will have the data from
the t-string available as record.args. In this case that would be:
{"test1": "hello", "test2": "world"}.
Extracting these values from the t-string allows other parts of the logging system to use the values just as if they were provided as parameters to a normal logging call.
[!NOTE] The request to add native t-string logging support to the standard library can be found here: https://github.com/python/cpython/issues/134394
Alternative methods
Using the TStringLogRecordMixin
If your application already uses a custom LogRecord class, the TStringLogRecordMixin can be used
to easily add t-string functionality to it.
>>> import logging
>>> from tstringlogger import TStringLogRecordMixin
>>>
>>> # example custom logger that adds a tag to every message
>>> class MyCustomLogRecord(TStringLogRecordMixin, logging.LogRecord):
... def getMessage(self):
... msg = super().getMessage() # get the formatting t-string
... return f"[tag] {msg}"
>>>
>>> logging.setLogRecordFactory(MyCustomLogRecord)
>>> test1, test2 = "hello", "world"
>>> logging.info(t"{test1=}, {test2}")
INFO:root:[tag] test1='hello', world
Globally enable t-string support
Support for t-string logging can be globally enabled/disabled by calling
tstringlogger.enable()/tstringlogger.disable(). This adds support for t-strings by directly
patching the LogRecord.__init__ and LogRecord.getMessage functions.
>>> import logging
>>> import tstringlogger
>>>
>>> tstringlogger.enable() # apply patches
>>>
>>> test1, test2 = "hello", "world"
>>> logging.info(t"{test1=}, {test2}")
INFO:root:test1='hello', world
>>>
>>> tstringlogger.disable() # remove patches
>>> logging.info(t"{test1=}, {test2}")
INFO:root:Template(strings=('test1=', ', ', ''), interpolations=(Interpolation...
Related work
bracelogger is a library that allows using new-style string
formatting in log messages instead of %-based formatting.
Metadata
Release files for tstringlogger 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tstringlogger-1.1.0.tar.gz | 3.8 kB | Without provenance | Details for tstringlogger-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tstringlogger-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for tstringlogger-1.1.0-py3-none-any.whl |
Total release size: 6.9 kB
Release files / tstringlogger-1.1.0.tar.gz
| Download URL | tstringlogger-1.1.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4c4cb8d138de50efc413af1b61431b701d50e9315e4476f1f0a76c2f262d92b6
|
|
BLAKE2b-256 checksum How to use checksums |
be7dede8707155285c2ff6fa33d4c330a6ed45528ab074d7a8eaddf4487e61ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / tstringlogger-1.1.0-py3-none-any.whl
| Download URL | tstringlogger-1.1.0-py3-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e264c80c84970a58fc96543c2122cf34b3610b0dc25d536478e8d49050edd342
|
|
BLAKE2b-256 checksum How to use checksums |
9bd3efdd461403ea700256e1dd9f579d451448e3f0b2448af9a0062b914df878
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for tstringlogger 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.