logging helper for functions and methods
Project description
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 typestr
and log it. Can beprint
orlogging
module'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 typestr
and log it. Can beprint
orlogging
module'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 typestr
and log it. Can beprint
orlogging
module'sdebug
,info
, and more.
Note
- The part that
log4func.log_traceback
wraps 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
@classmethod
or@staticmethod
, it must be an inner decorator.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file log4func-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: log4func-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dfe154ff81d9fdf2089373f54dcc9dda6b09abf06a186967f47b4f477a9b632 |
|
MD5 | 85226cc84e0b90791cd643f0982b88f7 |
|
BLAKE2b-256 | bea069a80e89728a34ebce45ca33b6724ccd69545a41d8326a98f71d84faa4ea |