From 99ecbcf55e309cf2a1efd6797de0b6d3e6f316e7 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Sat, 7 Feb 2026 17:35:28 +0800 Subject: [PATCH] perf(logging): reduce allow-strategy log noise via env switch --- docker-compose.yml | 1 + security/response_handler.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9590acb..3fbdd51 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/security/response_handler.py b/security/response_handler.py index 6d781c9..5462670 100644 --- a/security/response_handler.py +++ b/security/response_handler.py @@ -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,