Skip to main content

Simple, useful decorators for logging and tallying function calls

Project description

funlog

funlog is a tiny but quite useful package that offers a few Python decorators to log function calls, with good control over what gets logged and when. It also times the function call and logs arguments briefly but clearly, abbreviating arguments like long strings or dataclasses.

It is fully customizable with optional decorator arguments. You can log only slow calls, only if a function modifies its first argument, or tally calls and log them later.

I'm publishing it standalone since I often like to drop this into projects and it simplifies print-debugging a lot of things or lets you do very lightweight profiling where get logs if certain functions are taking a lot of time, or tallies of function calls after a program runs a while or at exit.

Minimal dependencies (only the tiny strif).

Installation

pip install funlog

Usage

Suppose you have a few functions:

import time
import logging
from funlog import log_calls, log_if_modifies, log_tallies, tally_calls

logging.basicConfig(
    level=logging.DEBUG,
    format="%(levelname)s:%(message)s",
    force=True,
)

@log_calls()
def add(a, b):
    return a + b

@log_calls(level="warn", if_slower_than=0.1, show_return_value=False)
def slow_function(delay):
    time.sleep(delay)
    return f"Slept for {delay} seconds"

# Now call the functions.
add(2, 3)
slow_function(0.5)

Running that gives you:

INFO:≫ Call: __main__.add(2, 3)
INFO:≪ Call done: __main__.add() took 0.00ms: 5
WARNING:⏱ Call to __main__.slow_function(0.5) took 503ms

See test_examples.py for more examples and docstrings for more docs on all the options.


This project was built from simple-modern-poetry.

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

funlog-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

funlog-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file funlog-0.1.0.tar.gz.

File metadata

  • Download URL: funlog-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for funlog-0.1.0.tar.gz
Algorithm Hash digest
SHA256 565f1dfac984e5638441ea1a6c53a303b417313808f7335e994bce318dcf93a6
MD5 b8fdf974f359e480e80a56fdc3706637
BLAKE2b-256 d75e37e8e8d9422ede20fbf0b2249c5f639e0ad9a2572b11ee9251caf3104f1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for funlog-0.1.0.tar.gz:

Publisher: publish.yml on jlevy/funlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file funlog-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: funlog-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for funlog-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c1335a0f100547026bd18e02cc59d96f56ae7387e3917fc91f9b5f71fede0ed
MD5 60590e5f0a7bc91f478d5c0e6c1f2553
BLAKE2b-256 a317018d759256587dfcb1a19d52a944925840ffafedcef484d7d8eeea2e41ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for funlog-0.1.0-py3-none-any.whl:

Publisher: publish.yml on jlevy/funlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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