I've found myself writing code like this several times:
def my_func(a, b, *args, **kwargs):
saved_args = locals() # Learned about this from http://stackoverflow.com/a/3137022/2829764
local_var = "This is some other local var that I don't want to log"
try:
a/b
except Exception as e:
logging.exception("Oh no! My args were: " + str(saved_args))
raise
Running my_func(1, 0, "spam", "ham", my_kwarg="eggs")
gives this output on stderr:
ERROR:root:Oh no! My args were: {'a': 1, 'args': (u'spam', u'ham'), 'b': 0, 'kwargs': {'my_kwarg': u'eggs'}}
Traceback (most recent call last):
File "/Users/kuzzooroo/Desktop/question.py", line 17, in my_func
a/b
ZeroDivisionError: division by zero
My question is, can I write something reusable so that I don't have to save locals() at the top of the function? And can it be done in a nice Pythonic way?
EDIT: one more request in response to @mtik00: ideally I'd like some way to access saved_args or the like from within my_func so that I can do something other than log uncaught exceptions (maybe I want to catch the exception in my_func, log an error, and keep going).