refactor: ログを auth/error カテゴリに統一、auth_check 成功ログ削除
Browse files
app.py
CHANGED
|
@@ -102,8 +102,8 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
|
| 102 |
# print(f"[RESPONSE] method={method} path={path} status={response.status_code} duration={duration:.3f}s{user_tag}")
|
| 103 |
if response.status_code >= 400:
|
| 104 |
log_event(
|
| 105 |
-
"
|
| 106 |
-
|
| 107 |
level="WARNING",
|
| 108 |
metadata={"method": method, "path": path, "status": response.status_code, "duration": round(duration, 3)},
|
| 109 |
)
|
|
@@ -112,10 +112,10 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
|
| 112 |
duration = time.time() - start_time
|
| 113 |
print(f"[RESPONSE] method={method} path={path} status=500 duration={duration:.3f}s error={e}{user_tag}")
|
| 114 |
log_event(
|
| 115 |
-
"
|
| 116 |
-
|
| 117 |
level="ERROR",
|
| 118 |
-
metadata={"method": method, "path": path, "status": 500, "duration": round(duration, 3)},
|
| 119 |
)
|
| 120 |
raise
|
| 121 |
finally:
|
|
@@ -140,14 +140,14 @@ print("[MIDDLEWARE] RequestLoggingMiddleware added")
|
|
| 140 |
def handle_login(email, password):
|
| 141 |
"""Handle login attempt via Supabase"""
|
| 142 |
print(f"[AUTH] Login attempt for: {email}")
|
| 143 |
-
log_event("
|
| 144 |
try:
|
| 145 |
res = supabase.auth.sign_in_with_password({"email": email, "password": password})
|
| 146 |
if res.session:
|
| 147 |
print(f"[AUTH] Login successful: {email}")
|
| 148 |
user_ctx = {"user_id": str(res.user.id), "email": email}
|
| 149 |
log_event(
|
| 150 |
-
"
|
| 151 |
user_override=user_ctx, metadata={"email": email},
|
| 152 |
)
|
| 153 |
return (
|
|
@@ -158,7 +158,7 @@ def handle_login(email, password):
|
|
| 158 |
except Exception as e:
|
| 159 |
print(f"[AUTH] Login failed for {email}: {e}")
|
| 160 |
log_event(
|
| 161 |
-
"
|
| 162 |
level="WARNING", metadata={"email": email, "error": str(e)},
|
| 163 |
)
|
| 164 |
return gr.update(), gr.update(value=f"❌ エラー: {str(e)}"), None
|
|
@@ -193,16 +193,11 @@ def get_current_user(request: Request):
|
|
| 193 |
}
|
| 194 |
|
| 195 |
print(f"[AUTH_CHECK] Success: user={user_dict['email']} role={user_dict['role']}")
|
| 196 |
-
log_event(
|
| 197 |
-
"auth_check", f"Authenticated: {user_dict['email']}",
|
| 198 |
-
user_override=user_dict,
|
| 199 |
-
metadata={"role": user_dict.get("role"), "org_name": user_dict.get("org_name")},
|
| 200 |
-
)
|
| 201 |
return user_dict
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
print(f"[AUTH_CHECK] Failed: {e}")
|
| 205 |
-
log_event("
|
| 206 |
return None
|
| 207 |
|
| 208 |
# --- Create UI instances ---
|
|
@@ -262,7 +257,7 @@ async def logout():
|
|
| 262 |
"""Logout route - clear cookie and redirect to login"""
|
| 263 |
user = get_user_context()
|
| 264 |
print(f"[ROUTE] /logout accessed")
|
| 265 |
-
log_event("
|
| 266 |
response = RedirectResponse(url="/login/")
|
| 267 |
response.delete_cookie("sb_access_token")
|
| 268 |
return response
|
|
|
|
| 102 |
# print(f"[RESPONSE] method={method} path={path} status={response.status_code} duration={duration:.3f}s{user_tag}")
|
| 103 |
if response.status_code >= 400:
|
| 104 |
log_event(
|
| 105 |
+
"error",
|
| 106 |
+
"http_response_error",
|
| 107 |
level="WARNING",
|
| 108 |
metadata={"method": method, "path": path, "status": response.status_code, "duration": round(duration, 3)},
|
| 109 |
)
|
|
|
|
| 112 |
duration = time.time() - start_time
|
| 113 |
print(f"[RESPONSE] method={method} path={path} status=500 duration={duration:.3f}s error={e}{user_tag}")
|
| 114 |
log_event(
|
| 115 |
+
"error",
|
| 116 |
+
"http_response_error",
|
| 117 |
level="ERROR",
|
| 118 |
+
metadata={"method": method, "path": path, "status": 500, "duration": round(duration, 3), "error": str(e)},
|
| 119 |
)
|
| 120 |
raise
|
| 121 |
finally:
|
|
|
|
| 140 |
def handle_login(email, password):
|
| 141 |
"""Handle login attempt via Supabase"""
|
| 142 |
print(f"[AUTH] Login attempt for: {email}")
|
| 143 |
+
log_event("auth", "login_attempt", metadata={"email": email})
|
| 144 |
try:
|
| 145 |
res = supabase.auth.sign_in_with_password({"email": email, "password": password})
|
| 146 |
if res.session:
|
| 147 |
print(f"[AUTH] Login successful: {email}")
|
| 148 |
user_ctx = {"user_id": str(res.user.id), "email": email}
|
| 149 |
log_event(
|
| 150 |
+
"auth", "login_success",
|
| 151 |
user_override=user_ctx, metadata={"email": email},
|
| 152 |
)
|
| 153 |
return (
|
|
|
|
| 158 |
except Exception as e:
|
| 159 |
print(f"[AUTH] Login failed for {email}: {e}")
|
| 160 |
log_event(
|
| 161 |
+
"auth", "login_failure",
|
| 162 |
level="WARNING", metadata={"email": email, "error": str(e)},
|
| 163 |
)
|
| 164 |
return gr.update(), gr.update(value=f"❌ エラー: {str(e)}"), None
|
|
|
|
| 193 |
}
|
| 194 |
|
| 195 |
print(f"[AUTH_CHECK] Success: user={user_dict['email']} role={user_dict['role']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
return user_dict
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
print(f"[AUTH_CHECK] Failed: {e}")
|
| 200 |
+
log_event("auth", "token_verify_fail", level="WARNING", metadata={"error": str(e)})
|
| 201 |
return None
|
| 202 |
|
| 203 |
# --- Create UI instances ---
|
|
|
|
| 257 |
"""Logout route - clear cookie and redirect to login"""
|
| 258 |
user = get_user_context()
|
| 259 |
print(f"[ROUTE] /logout accessed")
|
| 260 |
+
log_event("auth", "logout", user_override=user)
|
| 261 |
response = RedirectResponse(url="/login/")
|
| 262 |
response.delete_cookie("sb_access_token")
|
| 263 |
return response
|