log4func
Overview
Log helpers for functions and methods.
License
Installation
pip
pip install log4func
github
pip install git+https://github.com/bugph0bia/log4func.git
Usage
@log_start_end
Decorator that logs out the start and end of the target function.
Log output using the function passed as argument.
Parameter
logging_func(Callable): Log output function, must be able to accept an argument of typestrand log it. Can beprintorloggingmodule'sdebug,info, and more.with_start(bool): Toggles whether or not the start log is output. Default isTrue.with_end(bool): Toggles whether or not the end log is output. Default isTrue.
Example
@log_start_end(print)
def foo(a, b):
print(f'calc {a} + {b}')
return a + b
foo(1, 2)
Output
foo start
calc 1 + 2
foo end
@log_args_return
Decorator that logs real arguments and return values of the target function.
Log output using the function passed as argument.
Parameter
logging_func(Callable): Log output function, must be able to accept an argument of typestrand log it. Can beprintorloggingmodule'sdebug,info, and more.with_args(bool): Toggles whether or not the arguments log is output. Default isTrue.with_return(bool): Toggles whether or not the return log is output. Default isTrue.oneline(bool): Toggles whether or not logs are combined into a single line. Default isFalse.
Example
@log_args_return(print)
def foo(a, b):
print(f'calc {a} + {b}')
return a + b
foo(1, b=2)
Output
foo args:
1
b=2
calc 1 + 2
foo return:
3
@log_args_return(print, oneline=True)
def foo(a, b):
print(f'calc {a} + {b}')
return a + b
foo(1, b=2)
Output
foo args: 1, b=2
calc 1 + 2
foo return: 3
@log_traceback
Decorator that logs out a traceback of exceptions raised by the target function.
Log output using the function passed as argument.
Traceback is output to the screen by default, but it is useful if you want to keep it in a file. (e.g. in the logging module)
Parameter
logging_func(Callable): Log output function, must be able to accept an argument of typestrand log it. Can beprintorloggingmodule'sdebug,info, and more.
Note
- The part that
log4func.log_tracebackwraps is also output to traceback.
Example
@log_traceback(print)
def foo(a, b):
return a / b
foo(1, 0)
Output
Traceback (most recent call last):
File "...\log4func.py", line 184, in wrapper
decorated_func(*args, **kwargs)
File "...\test.py", line 2, in foo
a / b
ZeroDivisionError: division by zero
@wraps_logging_params
Decorator that allows the original function name to be output when the function name is logged in the decorator.
Supplemental Explanation
There is a problem that if you apply your own decorator to a function, the function name of the decorator becomes the function name of the original function when you get the original function name with func.__name__.
To solve this problem, you can apply @functool.wraps decorator to your own decorator.
Similarly, if you apply your own decorator to a function, the function name (%(funcName)s) output inside the original function using the logging module will be the decorator's function name.
To solve this problem, you can apply @log4func.wraps_logging_params decorator to your own decorator.
Note
- If used with
@classmethodor@staticmethod, it must be an inner decorator.
Metadata
Release files for log4func 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| log4func-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / log4func-0.1.0-py3-none-any.whl
| Download URL | log4func-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6dfe154ff81d9fdf2089373f54dcc9dda6b09abf06a186967f47b4f477a9b632
|
|
BLAKE2b-256 checksum How to use checksums |
bea069a80e89728a34ebce45ca33b6724ccd69545a41d8326a98f71d84faa4ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|