Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logging a function
#4
If you are looking for a scalable way to log function calls - I would suggest a  decorator in that style

import logging

from functools import wraps
logging.basicConfig(level=logging.DEBUG)

def logging_wrapper(func):
    @wraps(func)
    def logging_inner(*args, **kwargs):
        kwargs_formatted = ['{}={}'.format(k, repr(v)) for k, v in kwargs.items()]
        call_line = 'Func call: {}({})'.format(func.__name__, 
                                    ', '.join([repr(v) for v in args] + kwargs_formatted))
        try:
            res = func(*args, **kwargs)
            logging.debug('{} - returns {}'.format(call_line, repr(res)))
            return res
        except Exception as exc:
            logging.exception(call_line + ' caused exception!')
    return logging_inner
             
Now, decorate function of your choice
@logging_wrapper
def add(x, y):
    return x + y
Shall we test?  
>>> add(1, 4)
DEBUG:root:Func call: add(1, 4) - returns 5
5
>>> add(1, '4')
ERROR:root:Func call: add(1, '4') caused exception!
Traceback (most recent call last):
  File "<stdin>", line 8, in logging_inner
  File "<stdin>", line 3, in add
TypeError: unsupported operand type(s) for +: 'int' and 'str'
But what about named arguments?
>>> @logging_wrapper
... def add(first, second=None):
...     return first + second
...
>>> add(1,4)
DEBUG:root:Func call: add(1, 4) - returns 5
5
>>> add(1, second=4)
DEBUG:root:Func call: add(1, second=4) - returns 5
5
>>> add(1, second='4')
ERROR:root:Func call: add(1, second='4') caused exception!
Traceback (most recent call last):
  File "<stdin>", line 8, in logging_inner
  File "<stdin>", line 3, in add
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
Logging a function - by harrington40 - Apr-16-2017, 09:14 PM
RE: Logging a function - by Mekire - Apr-17-2017, 06:00 AM
RE: Logging a function - by snippsat - Apr-17-2017, 11:15 AM
RE: Logging a function - by volcano63 - Apr-17-2017, 06:49 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020