日志调整

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