-
Notifications
You must be signed in to change notification settings - Fork 0
[FEATURE] Logging 추가하기 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from logging.config import dictConfig | ||
|
|
||
| """ | ||
| 애플리케이션 전체에서의 로깅 레벨 설정 | ||
| """ | ||
| def setup_logging(): | ||
| dictConfig({ | ||
| "version": 1, | ||
| "disable_existing_loggers": False, | ||
| "formatters": { | ||
| "default": { | ||
| "format": "%(asctime)s %(levelname)s [%(name)s] %(message)s", | ||
| }, | ||
| }, | ||
| "handlers": { | ||
| "console": { | ||
| "class": "logging.StreamHandler", | ||
| "formatter": "default", | ||
| }, | ||
| }, | ||
| "root": { | ||
| "level": "INFO", | ||
| "handlers": ["console"], | ||
| }, | ||
| "loggers": { | ||
| "uvicorn": {"level": "INFO"}, | ||
| "uvicorn.error": {"level": "INFO"}, | ||
| "uvicorn.access": {"level": "INFO"}, | ||
| "app": {"level": "INFO", "handlers": ["console"], "propagate": False}, | ||
| }, | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,13 +8,16 @@ | |||||
| from app.services.openai_service import checker | ||||||
| from app.services.predictor_service import predictor_service | ||||||
| from app.services.s3_service import s3_service | ||||||
| import logging | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| """ | ||||||
| 이미지를 다운 -> 다운 한 것에 대하여 모델 분석 요청 | ||||||
| """ | ||||||
| async def process_image_scan(job: ImageJob, redis_client: redis.Redis): | ||||||
| correlationId = job.correlationId | ||||||
| print(f"[task] Start image scan for job_id={correlationId}") | ||||||
| logging.info(f"[task] Start image scan for job_id={correlationId}") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그 메시지에
Suggested change
|
||||||
| try: | ||||||
|
|
||||||
| stream_file = await asyncio.to_thread( | ||||||
|
|
@@ -28,7 +31,7 @@ async def process_image_scan(job: ImageJob, redis_client: redis.Redis): | |||||
| predictor_service.predict, | ||||||
| stream_file | ||||||
| ) | ||||||
| print(f"[task] Start Asking GPT for job_id={correlationId}") | ||||||
| logging.info(f"[task] Start Asking GPT for job_id={correlationId}") | ||||||
| description, isSafe = checker.ask_chatgpt_about_pregnancy_safety(pillName) | ||||||
| finishedAt = datetime.utcnow().isoformat() | ||||||
|
|
||||||
|
|
@@ -50,9 +53,9 @@ async def process_image_scan(job: ImageJob, redis_client: redis.Redis): | |||||
| approximate=True, | ||||||
| ) | ||||||
|
|
||||||
| print(f"[task] Image scan successfully finished for job_id={correlationId}") | ||||||
| logging.info(f"[task] Image scan successfully finished for job_id={correlationId}") | ||||||
|
|
||||||
| except Exception as e: | ||||||
| print(f"[task] Failed to process job_id={correlationId}: {e}") | ||||||
| logging.warning(f"[task] Failed to process job_id={correlationId}: {e}") | ||||||
| finally: | ||||||
| print(f"[task] Image scan finished for job_id={correlationId}") | ||||||
| logging.info(f"[task] Image scan finished for job_id={correlationId}") | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,9 @@ | |||||
| from app.core.config import settings | ||||||
| from app.schemas.job import ImageJob | ||||||
| from app.worker.tasks import process_image_scan | ||||||
| import logging | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| """ | ||||||
| Redis Stream에 정의한 유효한 형식 메시지를 위한 전처리 함수 | ||||||
|
|
@@ -49,7 +52,7 @@ def __init__(self, redis_client: redis_client): | |||||
| self.redis_client = redis_client | ||||||
|
|
||||||
| async def run(self): | ||||||
| print(f"[worker] start consumer={settings.CONSUMER_NAME} group={settings.GROUP_NAME} stream={settings.STREAM_JOB}") | ||||||
| logging.info(f"[worker] start consumer={settings.CONSUMER_NAME} group={settings.GROUP_NAME} stream={settings.STREAM_JOB}") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그 메시지에
Suggested change
|
||||||
| reclaim_every_sec = 30 | ||||||
| last_reclaim = 0.0 | ||||||
|
|
||||||
|
|
@@ -82,12 +85,12 @@ async def run(self): | |||||
|
|
||||||
| # 최종 반환 data | ||||||
| data = json.loads(payload_str) | ||||||
| print(f"Job received id={msg_id} correlationId={correlation_id} payload={data}") | ||||||
| logging.info(f"Job received id={msg_id} correlationId={correlation_id} payload={data}") | ||||||
|
|
||||||
| job = ImageJob.model_validate(data) | ||||||
| # XADD까지 호출 | ||||||
| task = asyncio.create_task(process_image_scan(job, redis_client)) | ||||||
| print(f"[worker] {task} 발행 성공") | ||||||
| logging.info(f"[worker] {task} 발행 성공") | ||||||
|
|
||||||
| # 처리 성공 시에만 ack 후 del | ||||||
| task.add_done_callback(lambda t: asyncio.create_task( | ||||||
|
|
@@ -132,7 +135,7 @@ async def run(self): | |||||
| job = ImageJob.model_validate_json(payload) | ||||||
|
|
||||||
| task = asyncio.create_task(process_image_scan(job, self.redis_client)) | ||||||
| print(f"[worker] {task} 발행 성공") | ||||||
| logging.info(f"[worker] {task} 발행 성공") | ||||||
|
|
||||||
| def _on_done(t: asyncio.Task, *, msg_id=msg_id, fields=fields): | ||||||
| async def _ack_or_dlq(): | ||||||
|
|
@@ -155,8 +158,8 @@ async def _ack_or_dlq(): | |||||
| await self.redis_client.xadd(f"{settings.STREAM_JOB}:DLQ", clean) | ||||||
|
|
||||||
| except asyncio.CancelledError: | ||||||
| print("[worker] cancelled; bye") | ||||||
| logging.warning("[worker] cancelled; bye") | ||||||
| break | ||||||
| except Exception as e: | ||||||
| print(f"[worker] error: {e}") | ||||||
| logging.warning(f"[worker] error: {e}") | ||||||
| await asyncio.sleep(1) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch.load에서weights_only=False를 사용하는 것은 신뢰할 수 없는 소스의 모델 파일을 로드할 때 임의 코드 실행으로 이어질 수 있는 보안 위험을 초래할 수 있습니다. 모델의 state_dict만 로드하는 것이 더 안전합니다.weights_only=True로 설정하고 state_dict를 로드하도록 코드를 수정하는 것을 강력히 권장합니다.