yangtb2024 commited on
Commit
04860cd
·
1 Parent(s): 12c8c95

fix: 改进 POST 请求体日志记录格式

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -49,10 +49,13 @@ async def reverse_proxy(request: Request, path: str):
49
  # 如果是 POST 请求,记录请求体内容
50
  if request.method == "POST":
51
  try:
52
- # 尝试解码为 UTF-8 文本进行记录,如果失败则记录原始字节
53
  body_text = body.decode('utf-8')
54
- logging.info(f"Received POST request to {path}. Body: {body_text}")
 
 
55
  except UnicodeDecodeError:
 
56
  logging.info(f"Received POST request to {path}. Body (bytes): {body}")
57
 
58
  try:
 
49
  # 如果是 POST 请求,记录请求体内容
50
  if request.method == "POST":
51
  try:
52
+ # 尝试解码为 UTF-8 文本进行记录
53
  body_text = body.decode('utf-8')
54
+ # 先记录请求信息,然后单独记录请求体,以便正确处理换行
55
+ logging.info(f"Received POST request to {path}. Body follows:")
56
+ logging.info(body_text) # 直接记录文本,日志系统会处理换行
57
  except UnicodeDecodeError:
58
+ # 如果解码失败,记录原始字节信息
59
  logging.info(f"Received POST request to {path}. Body (bytes): {body}")
60
 
61
  try: