Skip to content

Instantly share code, notes, and snippets.

@ckesanapalli
Last active March 19, 2022 23:15
Show Gist options
  • Save ckesanapalli/878c379de6be758d45de73f6310181c0 to your computer and use it in GitHub Desktop.
Save ckesanapalli/878c379de6be758d45de73f6310181c0 to your computer and use it in GitHub Desktop.
Logging entry, exit and runtime of functions with a decorator using loguru module
import time
from loguru import logger
from more_loguru import logger_wraps
logger.add('test.log', mode='w')
@logger_wraps(debug_io=True)
def my_func(a, b, c=1, d="Text"):
"""A test function"""
time.sleep(1)
return str([a, b, c, d])
func = my_func
my_func("hello", "logger", c="print this", d="keyword arg")
# Logging Existing library functions
import numpy as np
logger_wraps(debug_io=True)(np.random.rand)(10,10)
logger_wraps(log_full_doc=True)(np.sum)(np.random.rand(10,10))
from datetime import datetime
import functools
from loguru import logger
def dict2str(a_dict, indent=4):
"""Return formatted string version of dict"""
if len(a_dict) == 0:
return "{}"
max_key_len = max(len(key) for key in a_dict)
line_indent = "\n"+" "*indent
str_items = line_indent.join(f"{key}"+ " "*(max_key_len-len(key)) +f": {value},"
for key, value in a_dict.items())
return "{" + line_indent + str_items + line_indent + "}"
def iter2str(a_list, indent=4, open_bracket="(", close_bracket=")"):
"""Return formatted string version of an iterator"""
if len(a_list) == 0:
return open_bracket+close_bracket
line_indent = "\n"+" "*indent
str_items = line_indent.join(str(value)+',' for value in a_list)
return open_bracket + line_indent + str_items + line_indent + close_bracket
def get_func_name(func):
"""Return the name of the given function including module name"""
if hasattr(func, '__module__') and func.__module__ != None:
func_name = '.'.join([func.__module__, func.__qualname__])
else:
func_name = func.__qualname__
return func_name
def get_func_small_doc(func):
"""Return first two lines from the function documentation"""
try:
split_doc = func.__doc__.split("\n")
if len(split_doc) < 2:
doc = ' '.join(split_doc).strip()
else:
doc = ' '.join(split_doc[:2]).strip()
except:
doc = None
return doc
def logger_wraps(*, func_entry=True, func_exit=True, level="DEBUG",
debug_io=False, debug_args=False, debug_kwargs=False,
debug_result=False, log_full_doc=False):
"""
A wrapper function to log the function entry, exit and runtime outputting arguments and results
Parameters
----------
func_entry : bool, optional
Whether to log the entry of function. The default is True.
func_exit : bool, optional
Whether to log the exit of function. The default is True.
level : str, optional
logging level. The default is "DEBUG".
debug_io : bool, optional
Whether to log function inputs and output parameters. The default is False.
debug_args : bool, optional
Whether to log function arguments. The default is False.
debug_kwargs : bool, optional
Whether to log function keyword arguments. The default is False.
debug_result : bool, optional
Whether to log function result. The default is False.
is_full_doc : bool, optional
Whether to log full documentation of function.
If False, first two lines of function documentation are logged.
The default is False.
Returns
-------
decorator:
An object that can be used to decorate a function.
"""
def wrapper(func):
name = get_func_name(func)
doc = func.__doc__ if log_full_doc else get_func_small_doc(func)
@functools.wraps(func)
def wrapped(*args, **kwargs):
logger_ = logger.opt(depth=1)
if func_entry:
entry_dict = {
"State": "ENTRY",
"Function": name,
"Description": doc,
}
if debug_args or debug_io:
entry_dict["Args"] = iter2str(args, indent=22)
if debug_kwargs or debug_io:
entry_dict["Kwargs"] = dict2str(kwargs, indent=22)
entry_str = dict2str(entry_dict)
logger_.log(level, entry_str)
start = datetime.now()
result = func(*args, **kwargs)
end = datetime.now()
runtime = end - start
if func_exit:
exit_dict = {
"State": "EXIT",
"Function": name,
"Runtime": runtime,
}
if debug_result or debug_io:
exit_dict["Result"] = str(result)
exit_str = dict2str(exit_dict)
logger_.log(level, exit_str)
return result
return wrapped
return wrapper
2022-03-17 13:35:57.119 | DEBUG | __main__:<module>:15 - {
State : ENTRY
Function : __main__.my_func
Description: A test function
Args : (
hello
logger
)
Kwargs : {
c: print this
d: keyword arg
}
}
2022-03-17 13:35:58.136 | DEBUG | __main__:<module>:15 - {
State : EXIT
Function: __main__.my_func
Runtime : 0:00:01.013995
Result : ['hello', 'logger', 'print this', 'keyword arg']
}
2022-03-17 13:35:58.138 | DEBUG | __main__:<module>:19 - {
State : ENTRY
Function : RandomState.rand
Description: rand(d0, d1, ..., dn)
Args : (
10
10
)
Kwargs : {}
}
2022-03-17 13:35:58.141 | DEBUG | __main__:<module>:19 - {
State : EXIT
Function: RandomState.rand
Runtime : 0:00:00.001005
Result : [[0.15421369 0.10137913 0.91046302 0.43190587 0.29174799 0.71519251
0.52935884 0.06936744 0.26859926 0.73353876]
[0.42048735 0.49197483 0.63040623 0.28178557 0.068379 0.06677066
0.26446492 0.1479684 0.73640894 0.67122753]
[0.73802108 0.93509084 0.25294261 0.05860547 0.65150791 0.18761106
0.48256235 0.2612798 0.31041145 0.2340653 ]
[0.52375654 0.68371864 0.30928983 0.87638674 0.70446178 0.2054611
0.26661087 0.52606977 0.88117853 0.80641446]
[0.87759751 0.1872547 0.73896022 0.2782295 0.40346596 0.78815338
0.88895959 0.31498619 0.12840834 0.8945755 ]
[0.48283961 0.16861856 0.66444653 0.76056871 0.96547068 0.71580742
0.44246482 0.86323614 0.47825779 0.54422304]
[0.26766282 0.29110596 0.2603545 0.50927323 0.32419506 0.0094917
0.05879885 0.03719602 0.49640424 0.12116557]
[0.9772078 0.37718526 0.64472585 0.14811863 0.94161187 0.86663402
0.39839245 0.77827622 0.1294619 0.95028546]
[0.56660615 0.94056333 0.83124345 0.62046998 0.37744329 0.7341235
0.4192672 0.3835191 0.53189709 0.76989918]
[0.83758544 0.57230601 0.62635092 0.02452058 0.78512488 0.28017589
0.35995325 0.75439324 0.43808403 0.73764402]]
}
2022-03-17 13:35:58.142 | DEBUG | __main__:<module>:20 - {
State : ENTRY
Function : numpy.sum
Description:
Sum of array elements over a given axis.
Parameters
----------
a : array_like
Elements to sum.
axis : None or int or tuple of ints, optional
Axis or axes along which a sum is performed. The default,
axis=None, will sum all of the elements of the input array. If
axis is negative it counts from the last to the first axis.
.. versionadded:: 1.7.0
If axis is a tuple of ints, a sum is performed on all of the axes
specified in the tuple instead of a single axis or all the axes as
before.
dtype : dtype, optional
The type of the returned array and of the accumulator in which the
elements are summed. The dtype of `a` is used by default unless `a`
has an integer dtype of less precision than the default platform
integer. In that case, if `a` is signed then the platform integer
is used while if `a` is unsigned then an unsigned integer of the
same precision as the platform integer is used.
out : ndarray, optional
Alternative output array in which to place the result. It must have
the same shape as the expected output, but the type of the output
values will be cast if necessary.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `sum` method of sub-classes of
`ndarray`, however any non-default value will be. If the
sub-class' method does not implement `keepdims` any
exceptions will be raised.
initial : scalar, optional
Starting value for the sum. See `~numpy.ufunc.reduce` for details.
.. versionadded:: 1.15.0
where : array_like of bool, optional
Elements to include in the sum. See `~numpy.ufunc.reduce` for details.
.. versionadded:: 1.17.0
Returns
-------
sum_along_axis : ndarray
An array with the same shape as `a`, with the specified
axis removed. If `a` is a 0-d array, or if `axis` is None, a scalar
is returned. If an output array is specified, a reference to
`out` is returned.
See Also
--------
ndarray.sum : Equivalent method.
add.reduce : Equivalent functionality of `add`.
cumsum : Cumulative sum of array elements.
trapz : Integration of array values using the composite trapezoidal rule.
mean, average
Notes
-----
Arithmetic is modular when using integer types, and no error is
raised on overflow.
The sum of an empty array is the neutral element 0:
>>> np.sum([])
0.0
For floating point numbers the numerical precision of sum (and
``np.add.reduce``) is in general limited by directly adding each number
individually to the result causing rounding errors in every step.
However, often numpy will use a numerically better approach (partial
pairwise summation) leading to improved precision in many use-cases.
This improved precision is always provided when no ``axis`` is given.
When ``axis`` is given, it will depend on which axis is summed.
Technically, to provide the best speed possible, the improved precision
is only used when the summation is along the fast axis in memory.
Note that the exact precision may vary depending on other parameters.
In contrast to NumPy, Python's ``math.fsum`` function uses a slower but
more precise approach to summation.
Especially when summing a large number of lower precision floating point
numbers, such as ``float32``, numerical errors can become significant.
In such cases it can be advisable to use `dtype="float64"` to use a higher
precision for the output.
Examples
--------
>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
>>> np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)
array([1., 5.])
If the accumulator is too small, overflow occurs:
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
-128
You can also start the sum with a value other than zero:
>>> np.sum([10], initial=5)
15
}
2022-03-17 13:35:58.144 | DEBUG | __main__:<module>:20 - {
State : EXIT
Function: numpy.sum
Runtime : 0:00:00
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment