|
1 | 1 | # funlog
|
2 | 2 |
|
3 |
| -`funlog` is a tiny but useful package that offers a few Python decorators to log |
4 |
| -function calls, with good control over what gets logged and when. |
| 3 | +`funlog` is a tiny but useful package that offers a few Python decorators to log or |
| 4 | +tally function calls, with good control over what gets logged and when. |
| 5 | + |
| 6 | +## Why Decorator Logging? |
5 | 7 |
|
6 | 8 | We all do quick print debugging sometimes.
|
7 |
| -Sometimes this is via log statements or other times simply with print(). Logging |
8 |
| -decorators are a nice compromise between the simplicity of print debugging with more |
9 |
| -complex or careful log statements. |
| 9 | +Sometimes this is via log statements or other times simply with `print()`. |
| 10 | + |
| 11 | +Logging decorators are a nice compromise between the simplicity of print debugging with |
| 12 | +more complex or careful log statements: |
| 13 | + |
| 14 | +```python |
| 15 | +@log_calls() |
| 16 | +def add(a, b): |
| 17 | + return a + b |
| 18 | +``` |
| 19 | + |
| 20 | +Then in the logs you will have: |
| 21 | +``` |
| 22 | +INFO:≫ Call: __main__.add(5, 5) |
| 23 | +INFO:≪ Call done: __main__.add() took 0.00ms: 10 |
| 24 | +``` |
| 25 | + |
| 26 | +In addition to logging function calls, `funlog` decorators also time the function call |
| 27 | +and can log arguments briefly but clearly, abbreviating arguments like long strings or |
| 28 | +dataclasses. |
| 29 | + |
| 30 | +The decorator is simple with reasonable defaults but is also fully customizable with |
| 31 | +optional arguments to the decorator. |
| 32 | +You can control whether to show arg values and return values: |
| 33 | + |
| 34 | +- `show_args` to log the function arguments (truncating at `truncate_length`) |
| 35 | + |
| 36 | +- `show_return_value` to log the return value (truncating at `truncate_length`) |
| 37 | + |
| 38 | +By default both calls and returns are logged, but this is also customizable: |
| 39 | + |
| 40 | +- `show_calls_only=True` to log only calls |
| 41 | + |
| 42 | +- `show_returns_only=True` to log only returns |
| 43 | + |
| 44 | +- `show_timing_only=True` only logs the timing of the call very briefly |
| 45 | + |
| 46 | +If `if_slower_than_sec` is set, only log calls that take longer than that number of |
| 47 | +seconds. |
| 48 | + |
| 49 | +By default, uses standard logging with the given `level`, but you can pass in a custom |
| 50 | +`log_func` to override that. |
10 | 51 |
|
11 |
| -In addition to logging function calls, `funlog` also times the function call and logs |
12 |
| -arguments briefly but clearly, abbreviating arguments like long strings or dataclasses. |
| 52 | +By default, it shows values using `quote_if_needed()`, which is brief and very readable. |
| 53 | +You can pass in a custom `repr_func` to change that. |
13 | 54 |
|
14 |
| -The decorator is simple but fully customizable with optional arguments. |
15 |
| -You can log only slow calls, log only calls or only returns or only call timings, or |
16 |
| -tally calls and log them later. |
| 55 | +I'm publishing it standalone since I have found over the years I frequently want to drop |
| 56 | +it into projects. It's often even easier to use than quick print debugging. |
17 | 57 |
|
18 |
| -I'm publishing it standalone since I often like to drop this into projects. |
19 |
| -It's often even faster than quick print-debugging and it lets you do very lightweight |
20 |
| -profiling by getting when certain functions are taking a lot of time and tallies of |
21 |
| -function calls and runtimes per function after a program runs a while or at exit. |
| 58 | +It also lets you do very lightweight profiling by having warnings in production when |
| 59 | +certain functions are taking a lot of time. |
| 60 | +Finally, is easy to get tallies of function calls and runtimes per function after a |
| 61 | +program runs a while or at exit. |
22 | 62 |
|
23 | 63 | It deliberately has **zero dependencies** and is a single file with ~500 lines of code.
|
24 | 64 |
|
|
0 commit comments