Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -5,7 +5,16 @@ import json
|
|
5 |
import base64
|
6 |
from io import BytesIO
|
7 |
from datetime import datetime
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
app = FastAPI()
|
10 |
|
11 |
|
@@ -25,13 +34,13 @@ def read_root():
|
|
25 |
|
26 |
@app.post("/upload")
|
27 |
async def upload_file(file: UploadFile = File(...)):
|
28 |
-
|
29 |
# 定义文件存储路径和名称
|
30 |
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
31 |
|
32 |
# 将上传的文件写入本地文件系统
|
33 |
with open(file_location, "wb") as buffer:
|
34 |
-
|
35 |
buffer.write(await file.read())
|
36 |
|
37 |
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
|
|
5 |
import base64
|
6 |
from io import BytesIO
|
7 |
from datetime import datetime
|
8 |
+
import logging
|
9 |
|
10 |
+
# 配置日志记录器
|
11 |
+
logging.basicConfig(
|
12 |
+
level=logging.INFO, # 设置日志级别为INFO
|
13 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", # 定义日志格式
|
14 |
+
)
|
15 |
+
|
16 |
+
# 获取 FastAPI 应用日志记录器
|
17 |
+
logger = logging.getLogger("fastapi")
|
18 |
app = FastAPI()
|
19 |
|
20 |
|
|
|
34 |
|
35 |
@app.post("/upload")
|
36 |
async def upload_file(file: UploadFile = File(...)):
|
37 |
+
logger.info(f'收到请求 {file.filename}')
|
38 |
# 定义文件存储路径和名称
|
39 |
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
40 |
|
41 |
# 将上传的文件写入本地文件系统
|
42 |
with open(file_location, "wb") as buffer:
|
43 |
+
logger.info(f'写入本地文件 {file.filename}')
|
44 |
buffer.write(await file.read())
|
45 |
|
46 |
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|