-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (34 loc) · 1.11 KB
/
Copy pathmain.py
File metadata and controls
42 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from fastapi import FastAPI, Request
import logging
import os
from datetime import datetime
# Ensure the log directory exists
os.makedirs("logs", exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file_path = f"logs/app_{timestamp}.log"
# 配置日志处理器,明确指定UTF-8编码
file_handler = logging.FileHandler(log_file_path, encoding='utf-8')
stream_handler = logging.StreamHandler()
# 创建格式化器
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
# 配置根日志记录器
logging.basicConfig(
level=logging.DEBUG,
handlers=[file_handler, stream_handler]
)
# 创建FastAPI应用
app = FastAPI()
logger = logging.getLogger(__name__)
@app.post("/")
async def submit(request: Request):
try:
data = await request.json()
logger.debug(f"Received data: {data}")
except Exception as e:
logger.error(f"Error processing request: {str(e)}")
# 运行应用
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)