日志调整

This commit is contained in:
fuzhongyun 2026-04-01 11:52:45 +08:00
parent 4ba2045328
commit ef2b0e4e72
1 changed files with 13 additions and 17 deletions

30
main.py
View File

@ -3,17 +3,17 @@
import asyncio
import os
import logging
from typing import Optional
import time
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Request
from playwright.async_api import async_playwright
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("fingerprint_service")
logger = logging.getLogger("headPickerService")
TARGET_PAGE = os.getenv("TARGET_PAGE")
TARGET_API = os.getenv("TARGET_API")
@ -28,7 +28,6 @@ RESOURCE_CACHE = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
global playwright_instance, browser
logger.info("Starting Playwright instance...")
playwright_instance = await async_playwright().start()
browser = await playwright_instance.chromium.launch(
headless=True,
@ -39,12 +38,9 @@ async def lifespan(app: FastAPI):
"--disable-software-rasterizer",
]
)
logger.info("Playwright browser launched successfully.")
yield
logger.info("Closing Playwright browser...")
await browser.close()
await playwright_instance.stop()
logger.info("Playwright instance stopped.")
app = FastAPI(
@ -55,8 +51,16 @@ app = FastAPI(
)
@app.middleware("http")
async def log_request_time(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = (time.time() - start_time) * 1000
logger.info(f"{request.method} {request.url.path} - {response.status_code} - {process_time:.2f}ms")
return response
async def get_fingerprint():
logger.info("Opening new browser page...")
page = await browser.new_page()
try:
# [新增] 拦截无用资源,极大提升加载速度
@ -93,7 +97,6 @@ async def get_fingerprint():
await route.fulfill(response=response, body=body)
return
except Exception as e:
logger.warning(f"Failed to fetch script {url}: {e}")
pass # 抓取失败,降级给底层处理
await route.continue_()
@ -106,18 +109,14 @@ async def get_fingerprint():
if TARGET_API in request.url and not fingerprint_future.done():
headers = request.headers
if FINGERPRINT_HEADER in headers:
logger.info("Successfully intercepted target API request with required header.")
fingerprint_future.set_result(headers[FINGERPRINT_HEADER])
page.on("request", handle_request)
# [修改] 并发执行 goto 和 wait_for。拿到结果就立刻返回不再死等 goto 结束
logger.info(f"Navigating to target page: {TARGET_PAGE}")
goto_task = asyncio.create_task(page.goto(TARGET_PAGE, wait_until="domcontentloaded"))
logger.info("Waiting for header generation...")
fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15)
logger.info("Header successfully generated and retrieved.")
return fingerprint
except asyncio.TimeoutError:
logger.error("Timeout while waiting for header generation.")
@ -126,7 +125,6 @@ async def get_fingerprint():
logger.error(f"Unexpected error during generation: {str(e)}")
return None
finally:
logger.info("Closing browser page...")
await page.close()
@ -142,11 +140,9 @@ async def health():
@app.get("/fingerprint")
async def get_fingerprint_endpoint():
logger.info("Received request for new header signature")
try:
fingerprint = await get_fingerprint()
if fingerprint:
logger.info("Successfully handled signature request")
return {
"status": "success",
"data": {