sfun commited on
Commit
2e9046a
·
verified ·
1 Parent(s): a9aae54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -175,17 +175,19 @@ async def logging_middleware(request, handler):
175
  response = await handler(request)
176
  end_time = datetime.datetime.now()
177
 
178
- timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
179
- client_ip = get_client_ip(request)
180
- target_url = request.query.get('url', '-')
181
- no_cache = 'nocache' in request.query
182
- status_code = response.status
183
- proxy_count = response.headers.get('X-Proxy-Count', '0')
184
- cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
185
- cache_time = response.headers.get('X-Cache-Time', '-')
186
-
187
- log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}{'&nocache' if no_cache else ''}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NoCache: {'Yes' if no_cache else 'No'}"
188
- print(log_message, flush=True)
 
 
189
 
190
  return response
191
  except Exception as e:
 
175
  response = await handler(request)
176
  end_time = datetime.datetime.now()
177
 
178
+ # 只有当 URL 参数存在且不为 '-' 时才记录日志
179
+ if 'url' in request.query and request.query['url'] != '-':
180
+ timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
181
+ client_ip = get_client_ip(request)
182
+ target_url = request.query.get('url')
183
+ no_cache = 'nocache' in request.query
184
+ status_code = response.status
185
+ proxy_count = response.headers.get('X-Proxy-Count', '0')
186
+ cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
187
+ cache_time = response.headers.get('X-Cache-Time', '-')
188
+
189
+ log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}{'&nocache' if no_cache else ''}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NoCache: {'Yes' if no_cache else 'No'}"
190
+ print(log_message, flush=True)
191
 
192
  return response
193
  except Exception as e: