2026년 7월 21일 화요일

app/macro.py



import logging
import time
from functools import wraps
from pathlib import Path

from app.colors import ANSI

# 셋팅하신 파일 로거 지정
logger = logging.getLogger("uvicorn_file_logger")


def trace(func):
  @wraps(func)
  def wrapper(*args, **kwargs):

    start_time = time.perf_counter()
    result = func(*args, **kwargs)
    end_time = time.perf_counter()

    # 경과 시간 계산 (밀리초 단위)
    elapsed = (end_time - start_time) * 1000

    full_path = Path(func.__code__.co_filename)
    func_name = func.__name__
    line = func.__code__.co_firstlineno

    logger.info(
      f'File "{full_path}", line {line} in {func_name}() {ANSI.BOLD}{ANSI.YELLOW}({elapsed:.2f}ms){ANSI.RESET}'
    )
    return result

  return wrapper


database.py

import os from contextlib import contextmanager # contextmanager 임포트 from typing import Generator from dotenv...