Reference:aspectlib.debug¶
aspectlib.debug.log | Decoratesfunc to have logging. |
aspectlib.debug.format_stack | Returns a one-line string with the current callstack. |
aspectlib.debug.frame_iterator | Yields frames till there are no more. |
aspectlib.debug.strip_non_ascii | Convert to string (usingstr) and replace non-ascii characters with a dot (.). |
aspectlib.debug.format_stack(skip=0,length=6,_sep='/')[source]¶Returns a one-line string with the current callstack.
aspectlib.debug.log(func=None,stacktrace=10,stacktrace_align=60,attributes=(),module=True,call=True,call_args=True,call_args_repr=<built-in function repr>,result=True,exception=True,exception_repr=<built-in function repr>,result_repr=<function strip_non_ascii>,use_logging='CRITICAL',print_to=None)[source]¶Decoratesfunc to have logging.
- Args
- func (function):
- Function to decorate. If missing log returns a partial which you can use as a decorator.
- stacktrace (int):
- Number of frames to show.
- stacktrace_align (int):
- Column to align the framelist to.
- attributes (list):
- List of instance attributes to show, in case the function is a instance method.
- module (bool):
- Show the module.
- call (bool):
- If
True, then show calls. IfFalseonly show the call details on exceptions (ifexceptionisenabled) (default:True) - call_args (bool):
- If
True, then show call arguments. (default:True) - call_args_repr (bool):
- Function to convert one argument to a string. (default:
repr) - result (bool):
- If
True, then show result. (default:True) - exception (bool):
- If
True, then show exceptions. (default:True) - exception_repr (function):
- Function to convert an exception to a string. (default:
repr) - result_repr (function):
- Function to convert the result object to a string. (default:
strip_non_ascii- likestrbut nonasciicharacters are replaced with dots.) - use_logging (string):
- Emit log messages with the given loglevel. (default:
"CRITICAL") - print_to (fileobject):
- File object to write to, in case you don’t want to use logging module. (default:
None- printing isdisabled)
Returns: A decorator or a wrapper. Example:
>>>@log(print_to=sys.stdout)...defa(weird=False):...ifweird:...raiseRuntimeError('BOOM!')>>>a()a() <<< ...a => None>>>try:...a(weird=True)...exceptException:...pass# naughty code!a(weird=True) <<< ...a ~ raised RuntimeError('BOOM!',)
You can conveniently use this to logs just errors, or just results, example:
>>>importaspectlib>>>withaspectlib.weave(float,log(call=False,result=False,print_to=sys.stdout)):...try:...float('invalid')...exceptExceptionase:...pass# naughty code!float('invalid') <<< ...float ~ raised ValueError(...float...invalid...)
This makes debugging naughty code easier.
PS: Without the weaving it looks like this:
>>>try:...log(call=False,result=False,print_to=sys.stdout)(float)('invalid')...exceptException:...pass# naughty code!float('invalid') <<< ...float ~ raised ValueError(...float...invalid...)
Changed in version 0.5.0:Renamedarguments tocall_args.Renamedarguments_repr tocall_args_repr.Addedcall option.
