Skip to main content

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


Download files

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

Source Distribution

template_string_logger-0.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

template_string_logger-0.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file template_string_logger-0.0.tar.gz.

File metadata

  • Download URL: template_string_logger-0.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for template_string_logger-0.0.tar.gz
Algorithm Hash digest
SHA256 a93e1ea3e30c77728d525ea09445e4f5d6f42ec4a9c8c4e9ca48f0180098c684
MD5 6858faf5cb4e99d362c6a56ca55d27f9
BLAKE2b-256 f6be62e6be1a042045b77487cde76439f85d8ac2926b5eae37358dd9ff053f65

See more details on using hashes here.

File details

Details for the file template_string_logger-0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for template_string_logger-0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc50eeed073142597599fa51be20aabb11b5f44bbf5fcae098c5b629d12890d
MD5 a1ec655bc157593ace04ed73a90da54b
BLAKE2b-256 b044051eed10632b1a024a2f847c435790e9ebda6a3aa9acca7be2cf55333e17

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