Tiny, useful decorators for logging and tallying function calls
Low maintenance activity. Last release 20 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install funlogREADME as published on PyPI with version 0.2.1.
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.