Python logger that supports t-strings
Project description
tstring-logger
Python logging with template strings with support for conditional (lazy) execution of functions in log messages.
Provides TStringLogger(logging.Logger) and sets it as the default logger class upon import tstring_logger
A Template String may embed function calls with a !fn format specifier. They will only be executed if the logger is active.
Example
import logging
import tstring_logger
def expensive_function(x):
time.sleep(1)
return 2 * x
logging.basicConfig()
lg = logging.getLogger("demo")
hour = datetime.datetime.now().hour
print(1)
lg.debug(test := t"The thing happened with {expensive_function:!fn} {7} at hour {hour}.")
print(2)
lg.setLevel(logging.DEBUG)
lg.debug(test)
print(3)
will quickly print 1 and 2, then pause a second before 3 appears after DEBUG:demo:The thing happened with 14 at hour 16. Since expensive_function takes a single argument, the 7 is passed to the function while hour renders in the usual way.
The logger uses the embed function of tstring-util.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file template_string_logger-1.0.tar.gz.
File metadata
- Download URL: template_string_logger-1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c1d87cae84e46aebffbfdee2e82d535b180384e3e4ef301b06439615afefd92
|
|
| MD5 |
3d0a5276f1468a98501f5adccb649367
|
|
| BLAKE2b-256 |
795c8cfa48dc79684332f0e0b25fcecebb28d9d7c8cb301be7bc657cb949a001
|
File details
Details for the file template_string_logger-1.0-py3-none-any.whl.
File metadata
- Download URL: template_string_logger-1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cddf2e62efd4ecb8ed7e891eb1e2b2af51741f1bee171a6103602fe2d13f7039
|
|
| MD5 |
a6bf42298026a91a530987508e51a956
|
|
| BLAKE2b-256 |
d013cd71977b913573a07b98e1dbff871daeb1910fdface0f8d9ef65984d255d
|