Tiny, useful decorators for logging and tallying function calls
Project description
funlog
funlog is a tiny but useful package that offers a few Python decorators to log or
tally function calls, with good control over what gets logged and when.
Why Decorator Logging?
We all do quick print debugging sometimes.
Sometimes this is via log statements or other times simply with print().
Logging decorators are a nice compromise between the simplicity of print debugging with more complex or careful log statements:
@log_calls()
def add(a, b):
return a + b
Then in the logs you will have:
INFO:≫ Call: __main__.add(5, 5)
INFO:≪ Call done: __main__.add() took 0.00ms: 10
In addition to logging function calls, funlog decorators also time the function call
and can log arguments briefly but clearly, abbreviating arguments like long strings or
dataclasses.
The decorator is simple with reasonable defaults but is also fully customizable with optional arguments to the decorator. You can control whether to show arg values and return values:
-
show_argsto log the function arguments (truncating attruncate_length) -
show_return_valueto log the return value (truncating attruncate_length)
By default both calls and returns are logged, but this is also customizable:
-
show_calls_only=Trueto log only calls -
show_returns_only=Trueto log only returns -
show_timing_only=Trueonly logs the timing of the call very briefly
If if_slower_than_sec is set, only log calls that take longer than that number of
seconds.
By default, uses standard logging with the given level, but you can pass in a custom
log_func to override that.
By default, it shows values using quote_if_needed(), which is brief and very readable.
You can pass in a custom repr_func to change that.
I'm publishing it standalone since I have found over the years I frequently want to drop it into projects. It's often even easier to use than quick print debugging.
It also lets you do very lightweight profiling by having warnings in production when certain functions are taking a lot of time. Finally, is easy to get tallies of function calls and runtimes per function after a program runs a while or at exit.
It deliberately has zero dependencies and is a single file with ~500 lines of code.
Installation
Add the funlog package to your environment in the
usual way with pip install funlog, poetry add funlog, or uv add funlog.
Or if for some reason you prefer not to change the dependencies of your project at all,
just copy the single file funlog.py.
Usage
Here is a more complex example with tallies:
import time
import logging
from funlog import log_calls, log_tallies, tally_calls
# Set up logging however you like.
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s:%(message)s", force=True)
@log_calls()
def add(a, b):
return a + b
@tally_calls()
def sleep(n):
time.sleep(0.01 * n)
@tally_calls()
def fibonacci(n):
if n <= 1:
return n
sleep(n)
return fibonacci(n - 1) + fibonacci(n - 2)
@log_calls()
def long_range(n):
time.sleep(0.01 * n)
return " ".join(str(i) for i in range(int(n)))
# Now call the functions:
long_range(fibonacci(add(add(5, 5), 2)))
# And then log tallies of all calls:
log_tallies()
Running that gives you:
INFO:≫ Call: __main__.add(5, 5)
INFO:≪ Call done: __main__.add() took 0.00ms: 10
INFO:≫ Call: __main__.add(10, 2)
INFO:≪ Call done: __main__.add() took 0.00ms: 12
INFO:⏱ __main__.sleep() took 125ms, now called 1 times, 125ms avg per call, total time 125ms
INFO:⏱ __main__.sleep() took 114ms, now called 2 times, 119ms avg per call, total time 239ms
INFO:⏱ __main__.sleep() took 95.03ms, now called 4 times, 109ms avg per call, total time 438ms
INFO:⏱ __main__.sleep() took 55.05ms, now called 8 times, 89.25ms avg per call, total time 714ms
INFO:⏱ __main__.fibonacci() took 0.00ms, now called 1 times, 0.00ms avg per call, total time 0.00ms
INFO:⏱ __main__.fibonacci() took 0.00ms, now called 2 times, 0.00ms avg per call, total time 0.00ms
INFO:⏱ __main__.fibonacci() took 25.22ms, now called 3 times, 8.41ms avg per call, total time 25.22ms
INFO:⏱ __main__.fibonacci() took 59.16ms, now called 5 times, 16.88ms avg per call, total time 84.38ms
INFO:⏱ __main__.fibonacci() took 128ms, now called 9 times, 26.41ms avg per call, total time 238ms
INFO:⏱ __main__.fibonacci() took 243ms, now called 15 times, 37.59ms avg per call, total time 564ms
INFO:⏱ __main__.sleep() took 33.76ms, now called 16 times, 60.92ms avg per call, total time 975ms
INFO:⏱ __main__.fibonacci() took 429ms, now called 25 times, 49.00ms avg per call, total time 1.23s
INFO:⏱ __main__.fibonacci() took 741ms, now called 41 times, 61.40ms avg per call, total time 2.52s
INFO:⏱ __main__.sleep() took 32.68ms, now called 32 times, 48.04ms avg per call, total time 1.54s
INFO:⏱ __main__.fibonacci() took 23.35ms, now called 75 times, 67.37ms avg per call, total time 5.05s
INFO:⏱ __main__.sleep() took 24.54ms, now called 64 times, 43.64ms avg per call, total time 2.79s
INFO:⏱ __main__.fibonacci() took 60.07ms, now called 129 times, 78.67ms avg per call, total time 10.15s
INFO:⏱ __main__.fibonacci() took 55.71ms, now called 223 times, 91.26ms avg per call, total time 20.35s
INFO:⏱ __main__.sleep() took 44.42ms, now called 128 times, 40.64ms avg per call, total time 5.20s
INFO:⏱ __main__.fibonacci() took 2.07s, now called 396 times, 107ms avg per call, total time 42.19s
INFO:≫ Call: __main__.long_range(144)
INFO:≪ Call done: __main__.long_range() took 1.45s: '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 …' (465 chars)
INFO:⏱ Function tallies:
__main__.fibonacci() was called 465 times, total time 59.73s, avg per call 128ms
__main__.sleep() was called 232 times, total time 9.11s, avg per call 39.25ms
There are several other options. See docstrings and test_examples.py for more docs and examples on all the options.
This project was built from simple-modern-uv.
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
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 funlog-0.2.1.tar.gz.
File metadata
- Download URL: funlog-0.2.1.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16f89e9c499077b374b4986a7c7dfc9fcd21ef40eb757fefff1a3631fdddf16f
|
|
| MD5 |
1f9bfc584f00f3d0ccf82c22e15f63a2
|
|
| BLAKE2b-256 |
22464ab8e6d87ab6aae6f1eafdd2f891c009e95e295bec2df8cd0b2f06870417
|
Provenance
The following attestation bundles were made for funlog-0.2.1.tar.gz:
Publisher:
publish.yml on jlevy/funlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
funlog-0.2.1.tar.gz -
Subject digest:
16f89e9c499077b374b4986a7c7dfc9fcd21ef40eb757fefff1a3631fdddf16f - Sigstore transparency entry: 189504039
- Sigstore integration time:
-
Permalink:
jlevy/funlog@e0db60e3f7d3fa5c17b731f146d445b45d5d06d8 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/jlevy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e0db60e3f7d3fa5c17b731f146d445b45d5d06d8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file funlog-0.2.1-py3-none-any.whl.
File metadata
- Download URL: funlog-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eed8d206c21ee8dc96137b4df51689470682d4700f6f99a1a6133a0e065f3798
|
|
| MD5 |
756de892bb812dd65952b19636374729
|
|
| BLAKE2b-256 |
d8af420b08227bd021f80008520aa20a001120e71042342801d0c782a6615a39
|
Provenance
The following attestation bundles were made for funlog-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on jlevy/funlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
funlog-0.2.1-py3-none-any.whl -
Subject digest:
eed8d206c21ee8dc96137b4df51689470682d4700f6f99a1a6133a0e065f3798 - Sigstore transparency entry: 189504040
- Sigstore integration time:
-
Permalink:
jlevy/funlog@e0db60e3f7d3fa5c17b731f146d445b45d5d06d8 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/jlevy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e0db60e3f7d3fa5c17b731f146d445b45d5d06d8 -
Trigger Event:
release
-
Statement type: