chore: add API diagnostic request logging toggles
This commit is contained in:
@@ -36,6 +36,13 @@ except Exception:
|
|||||||
_API_REQUEST_TIMEOUT_SECONDS = 5.0
|
_API_REQUEST_TIMEOUT_SECONDS = 5.0
|
||||||
_API_REQUEST_TIMEOUT_SECONDS = max(3.0, _API_REQUEST_TIMEOUT_SECONDS)
|
_API_REQUEST_TIMEOUT_SECONDS = max(3.0, _API_REQUEST_TIMEOUT_SECONDS)
|
||||||
|
|
||||||
|
_API_DIAGNOSTIC_LOG = str(os.environ.get("API_DIAGNOSTIC_LOG", "")).strip().lower() in ("1", "true", "yes", "on")
|
||||||
|
try:
|
||||||
|
_API_DIAGNOSTIC_SLOW_MS = int(os.environ.get("API_DIAGNOSTIC_SLOW_MS", "0") or "0")
|
||||||
|
except Exception:
|
||||||
|
_API_DIAGNOSTIC_SLOW_MS = 0
|
||||||
|
_API_DIAGNOSTIC_SLOW_MS = max(0, _API_DIAGNOSTIC_SLOW_MS)
|
||||||
|
|
||||||
_cookie_domain_fallback = urlsplit(BASE_URL).hostname or "postoa.aidunsoft.com"
|
_cookie_domain_fallback = urlsplit(BASE_URL).hostname or "postoa.aidunsoft.com"
|
||||||
|
|
||||||
_api_browser_instances: "weakref.WeakSet[APIBrowser]" = weakref.WeakSet()
|
_api_browser_instances: "weakref.WeakSet[APIBrowser]" = weakref.WeakSet()
|
||||||
@@ -143,16 +150,31 @@ class APIBrowser:
|
|||||||
else:
|
else:
|
||||||
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
|
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
|
||||||
last_error = None
|
last_error = None
|
||||||
|
timeout_value = kwargs.get("timeout")
|
||||||
|
diag_enabled = _API_DIAGNOSTIC_LOG
|
||||||
|
slow_ms = _API_DIAGNOSTIC_SLOW_MS
|
||||||
|
|
||||||
for attempt in range(1, max_retries + 1):
|
for attempt in range(1, max_retries + 1):
|
||||||
|
start_ts = _time_module.time()
|
||||||
try:
|
try:
|
||||||
if method.lower() == 'get':
|
if method.lower() == 'get':
|
||||||
resp = self.session.get(url, **kwargs)
|
resp = self.session.get(url, **kwargs)
|
||||||
else:
|
else:
|
||||||
resp = self.session.post(url, **kwargs)
|
resp = self.session.post(url, **kwargs)
|
||||||
|
if diag_enabled:
|
||||||
|
elapsed_ms = int((_time_module.time() - start_ts) * 1000)
|
||||||
|
if slow_ms <= 0 or elapsed_ms >= slow_ms:
|
||||||
|
self.log(
|
||||||
|
f"[API][trace] {method.upper()} {url} ok status={resp.status_code} elapsed_ms={elapsed_ms} timeout={timeout_value} attempt={attempt}/{max_retries}"
|
||||||
|
)
|
||||||
return resp
|
return resp
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
last_error = e
|
last_error = e
|
||||||
|
if diag_enabled:
|
||||||
|
elapsed_ms = int((_time_module.time() - start_ts) * 1000)
|
||||||
|
self.log(
|
||||||
|
f"[API][trace] {method.upper()} {url} err={type(e).__name__} elapsed_ms={elapsed_ms} timeout={timeout_value} attempt={attempt}/{max_retries}"
|
||||||
|
)
|
||||||
if attempt < max_retries:
|
if attempt < max_retries:
|
||||||
self.log(f"[API] 请求超时,{retry_delay}秒后重试 ({attempt}/{max_retries})...")
|
self.log(f"[API] 请求超时,{retry_delay}秒后重试 ({attempt}/{max_retries})...")
|
||||||
import time
|
import time
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ services:
|
|||||||
# 日志配置
|
# 日志配置
|
||||||
- LOG_LEVEL=INFO
|
- LOG_LEVEL=INFO
|
||||||
- LOG_FILE=logs/app.log
|
- LOG_FILE=logs/app.log
|
||||||
|
- API_DIAGNOSTIC_LOG=0
|
||||||
|
- API_DIAGNOSTIC_SLOW_MS=0
|
||||||
# 知识管理平台配置
|
# 知识管理平台配置
|
||||||
- ZSGL_LOGIN_URL=https://postoa.aidunsoft.com/admin/login.aspx
|
- ZSGL_LOGIN_URL=https://postoa.aidunsoft.com/admin/login.aspx
|
||||||
- ZSGL_INDEX_URL_PATTERN=index.aspx
|
- ZSGL_INDEX_URL_PATTERN=index.aspx
|
||||||
|
|||||||
Reference in New Issue
Block a user