add loggers for kakao login

This commit is contained in:
Dohyun Lim committed 2026-01-21 17:24:07 +09:00
1 parent 219d8798ad
commit 7038faaf74
3 files changed
+62 -3

No files matched your search

+26 -1
View File
@@ -4,10 +4,14 @@
카카오 로그인 인증 흐름을 처리하는 클라이언트입니다.
"""
import logging
import aiohttp
from config import kakao_settings
logger = logging.getLogger(__name__)
from app.user.exceptions import KakaoAPIError, KakaoAuthFailedError
from app.user.schemas.user_schema import KakaoTokenResponse, KakaoUserInfo
@@ -39,12 +43,15 @@ class KakaoOAuthClient:
Returns:
카카오 OAuth 인증 페이지 URL
"""
return (
auth_url = (
f"{self.AUTH_URL}"
f"?client_id={self.client_id}"
f"&redirect_uri={self.redirect_uri}"
f"&response_type=code"
)
logger.info(f"[KAKAO] 인증 URL 생성 - redirect_uri: {self.redirect_uri}")
logger.debug(f"[KAKAO] 인증 URL 상세 - auth_url: {auth_url}")
return auth_url
async def get_access_token(self, code: str) -> KakaoTokenResponse:
"""
@@ -60,6 +67,7 @@ class KakaoOAuthClient:
KakaoAuthFailedError: 토큰 발급 실패 시
KakaoAPIError: API 호출 오류 시
"""
logger.info(f"[KAKAO] 액세스 토큰 요청 시작 - code: {code[:20]}...")
try:
async with aiohttp.ClientSession() as session:
data = {
@@ -72,20 +80,27 @@ class KakaoOAuthClient:
if self.client_secret:
data["client_secret"] = self.client_secret
logger.debug(f"[KAKAO] 토큰 요청 데이터 - redirect_uri: {self.redirect_uri}, client_id: {self.client_id[:10]}...")
async with session.post(self.TOKEN_URL, data=data) as response:
result = await response.json()
logger.debug(f"[KAKAO] 토큰 응답 상태 - status: {response.status}")
if "error" in result:
error_desc = result.get(
"error_description", result.get("error", "알 수 없는 오류")
)
logger.error(f"[KAKAO] 토큰 발급 실패 - error: {result.get('error')}, description: {error_desc}")
raise KakaoAuthFailedError(f"카카오 토큰 발급 실패: {error_desc}")
logger.info("[KAKAO] 액세스 토큰 발급 성공")
logger.debug(f"[KAKAO] 토큰 정보 - token_type: {result.get('token_type')}, expires_in: {result.get('expires_in')}")
return KakaoTokenResponse(**result)
except KakaoAuthFailedError:
raise
except Exception as e:
logger.error(f"[KAKAO] API 호출 오류 - error: {str(e)}")
raise KakaoAPIError(f"카카오 API 호출 중 오류 발생: {str(e)}")
async def get_user_info(self, access_token: str) -> KakaoUserInfo:
@@ -102,21 +117,31 @@ class KakaoOAuthClient:
KakaoAuthFailedError: 사용자 정보 조회 실패 시
KakaoAPIError: API 호출 오류 시
"""
logger.info("[KAKAO] 사용자 정보 조회 시작")
try:
async with aiohttp.ClientSession() as session:
headers = {"Authorization": f"Bearer {access_token}"}
async with session.get(self.USER_INFO_URL, headers=headers) as response:
result = await response.json()
logger.debug(f"[KAKAO] 사용자 정보 응답 상태 - status: {response.status}")
if "id" not in result:
logger.error(f"[KAKAO] 사용자 정보 조회 실패 - response: {result}")
raise KakaoAuthFailedError("카카오 사용자 정보를 가져올 수 없습니다.")
kakao_id = result.get("id")
kakao_account = result.get("kakao_account", {})
profile = kakao_account.get("profile", {})
logger.info(f"[KAKAO] 사용자 정보 조회 성공 - kakao_id: {kakao_id}")
logger.debug(f"[KAKAO] 사용자 상세 정보 - nickname: {profile.get('nickname')}, email: {kakao_account.get('email')}")
return KakaoUserInfo(**result)
except KakaoAuthFailedError:
raise
except Exception as e:
logger.error(f"[KAKAO] API 호출 오류 - error: {str(e)}")
raise KakaoAPIError(f"카카오 API 호출 중 오류 발생: {str(e)}")