MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / wrapper

Function wrapper

keepercommander/service/decorators/api_logging.py:65–120  ·  view source on GitHub ↗
(*args: Any, **kwargs: Any)

Source from the content-addressed store, hash-verified

63 """Log API request information in a single line with color-coded status"""
64 @wraps(fn)
65 def wrapper(*args: Any, **kwargs: Any) -> Any:
66 ip = request.headers.get('X-Forwarded-For', request.remote_addr)
67 if ip is None:
68 ip = request.remote_addr
69
70 start_time = time.time()
71 try:
72 result = fn(*args, **kwargs)
73 duration = time.time() - start_time
74
75 status_code = result[1] if isinstance(result, tuple) else 200
76
77 # Color code based on status
78 if status_code < 200:
79 status_color = "\033[94m" # Blue for informational
80 elif status_code < 300:
81 status_color = "\033[92m" # Green for success
82 elif status_code < 400:
83 status_color = "\033[96m" # Cyan for redirection
84 elif status_code < 500:
85 status_color = "\033[91m" # Red for client errors
86 else:
87 status_color = "\033[93m" # Yellow for server errors
88
89 # Sanitize request data to hide passwords (only for JSON requests)
90 data_str = _get_sanitized_request_data()
91
92 log_parts = [
93 f"\033[94m{request.method}\033[0m", # Blue method
94 f"\033[96m{request.path}\033[0m", # Cyan path
95 f"\033[95m{ip}\033[0m", # Magenta IP
96 data_str,
97 str(status_code),
98 f"{duration:.2f}s"
99 ]
100
101 logger.info(" | ".join(log_parts))
102 return result
103
104 except Exception as ex:
105 duration = time.time() - start_time
106
107 # Sanitize request data for error logs too (only for JSON requests)
108 data_str = _get_sanitized_request_data()
109
110 log_parts = [
111 request.method,
112 request.path,
113 ip,
114 data_str,
115 "500",
116 f"{duration:.2f}s",
117 f"error='{str(ex)}'"
118 ]
119 logger.error(" | ".join(log_parts))
120 raise
121
122 return wrapper

Callers

nothing calls this directly

Calls 4

getMethod · 0.45
infoMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected