perf(logging): reduce allow-strategy log noise via env switch

This commit is contained in:
2026-02-07 17:35:28 +08:00
parent 43f1867033
commit 99ecbcf55e
2 changed files with 13 additions and 1 deletions

View File

@@ -53,6 +53,7 @@ services:
- MAX_IP_ATTEMPTS_PER_HOUR=10
# 日志配置
- LOG_LEVEL=INFO
- SECURITY_LOG_ALLOW_STRATEGY=0
- LOG_FILE=logs/app.log
- API_DIAGNOSTIC_LOG=0
- API_DIAGNOSTIC_SLOW_MS=0

View File

@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
import os
import random
import time
from dataclasses import dataclass
@@ -33,6 +34,12 @@ class ResponseHandler:
def __init__(self, *, rng: Optional[random.Random] = None) -> None:
self._rng = rng or random.SystemRandom()
self._logger = get_logger("app")
self._log_allow_strategy = str(os.environ.get("SECURITY_LOG_ALLOW_STRATEGY", "0")).strip().lower() in {
"1",
"true",
"yes",
"on",
}
def get_strategy(self, risk_score: int, is_banned: bool = False) -> ResponseStrategy:
"""
@@ -65,7 +72,11 @@ class ResponseHandler:
strategy.captcha_level = self._normalize_captcha_level(strategy.captcha_level)
self._logger.info(
log_func = self._logger.info
if strategy.action == ResponseAction.ALLOW and not self._log_allow_strategy:
log_func = self._logger.debug
log_func(
"响应策略: action=%s risk_score=%s delay=%.3f captcha_level=%s",
strategy.action.value,
score,