In python, I want to create logging function such as returning function's name, its arguments,and the value of function. The output I want with sys.stderr like
"test: f(arg='1', arg2='a')\ntest: f -> '1a'"
The code is like below ```
def log(prefix):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kws):
code *here
return wrapper
return decorator
@log
function(arg="1", arg2="a")
return arg + arg2
```
I thought of some code like below, but it won't work especially at arg="1" arg2="a". I dont know how to unpack kws arguments ( dict here) at just one time.
1.
print("{}: {}({})\n{}: {} -> {}".format(prefix, func.__name__, kws, prefix, func.__name__, func(**kws)), file=sys.stderr)
2.
print("{}: {}({})\n{}: {} -> {}".format(prefix, func.__name__, **kws, prefix, func.__name__, func(**kws)), file=sys.stderr)
If you are familiar with this, could you give me some solutions or hint?