-
Notifications
You must be signed in to change notification settings - Fork 0
/
logfc.py
49 lines (37 loc) · 1.31 KB
/
logfc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging as l
import os
import inspect
import traceback
# Configure the logging with a custom format that includes the timestamp
l.basicConfig(
filename='function_calls.log',
level=l.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logging = l.getLogger("logfc")
# Global list to store the last 5 tracebacks
# log function calls
def logfc(func):
def wrapper(*args, **kwargs):
# Get the function name and the module name
function_name = func.__name__
module_name = func.__module__
# Log the function call
logging.info(f"Calling function {function_name} in module {module_name}")
try:
# Execute the function
result = func(*args, **kwargs)
# Log the return value
logging.info(
f"Function {function_name} returned: {result}\n" +
''.join(traceback.format_stack()[:-1]).replace(os.getcwd(),"")
)
except Exception as e:
# Log the exception
logging.error(f"Exception occurred in function {function_name}: {e}")
logging.error(traceback.format_exc())
# Re-raise the exception
raise e
return result
return wrapper