Skip to main content

Add support for using t-strings for logging

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tstringlogger-1.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tstringlogger-1.1.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file tstringlogger-1.1.0.tar.gz.

File metadata

  • Download URL: tstringlogger-1.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for tstringlogger-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4c4cb8d138de50efc413af1b61431b701d50e9315e4476f1f0a76c2f262d92b6
MD5 dca1fc38431133da8e25322d12906938
BLAKE2b-256 be7dede8707155285c2ff6fa33d4c330a6ed45528ab074d7a8eaddf4487e61ce

See more details on using hashes here.

File details

Details for the file tstringlogger-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tstringlogger-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for tstringlogger-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e264c80c84970a58fc96543c2122cf34b3610b0dc25d536478e8d49050edd342
MD5 d7d70db3095de78a005548d689b63a71
BLAKE2b-256 9bd3efdd461403ea700256e1dd9f579d451448e3f0b2448af9a0062b914df878

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page