refactor(logging): extract _redact_and_sanitize helper for print_verbose stdout

Every print_verbose stdout path repeated _sanitize_log_message(redact_secrets(
str(x))). Extract a single _redact_and_sanitize() helper in _logging.py and
route all call-sites through it, so the redact->sanitize order is defined once.
No behavior change.
This commit is contained in:
yryzhan-vitech 2026-07-26 13:48:36 +02:00
parent 3c6d8754d7
commit 558f9a7caf
11 changed files with 31 additions and 42 deletions

View file

@ -424,12 +424,14 @@ def _sanitize_log_message(message: str) -> str:
return re.sub(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]", "?", message)
def _redact_and_sanitize(value) -> str:
return _sanitize_log_message(redact_secrets(str(value)))
def print_verbose(print_statement):
try:
if set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -18,7 +18,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
from pydantic import BaseModel
import litellm
from litellm._logging import _sanitize_log_message, redact_secrets, verbose_logger
from litellm._logging import _redact_and_sanitize, verbose_logger
from litellm.constants import CACHED_STREAMING_CHUNK_DELAY
from litellm.litellm_core_utils.model_param_helper import ModelParamHelper
from litellm.types.caching import *
@ -41,9 +41,7 @@ def print_verbose(print_statement):
try:
verbose_logger.debug(print_statement)
if litellm.set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -26,7 +26,7 @@ from pydantic import BaseModel
import litellm
from litellm import verbose_logger
from litellm._logging import _sanitize_log_message, redact_secrets
from litellm._logging import _redact_and_sanitize
from litellm._uuid import uuid
from litellm.litellm_core_utils.model_response_utils import (
is_model_response_stream_empty,
@ -94,9 +94,7 @@ def is_async_iterable(obj: Any) -> bool:
def print_verbose(print_statement):
try:
if litellm.set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -179,7 +179,7 @@ from litellm.utils import (
validate_openai_optional_params,
)
from ._logging import _sanitize_log_message, redact_secrets, verbose_logger
from ._logging import _redact_and_sanitize, verbose_logger
from .caching.caching import disable_cache, enable_cache, update_cache
from .litellm_core_utils.core_helpers import safe_deep_copy
from .litellm_core_utils.fallback_utils import (
@ -8381,9 +8381,7 @@ def print_verbose(print_statement):
try:
verbose_logger.debug(print_statement)
if litellm.set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -31,8 +31,7 @@ import aiohttp
import litellm
from litellm import get_secret
from litellm._logging import (
_sanitize_log_message,
redact_secrets,
_redact_and_sanitize,
verbose_proxy_logger,
)
from litellm.types.utils import GenericGuardrailAPIInputs
@ -1324,9 +1323,7 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail):
try:
verbose_proxy_logger.debug(print_statement)
if litellm.set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -10,8 +10,7 @@ from fastapi import HTTPException
import litellm
from litellm._logging import (
_sanitize_log_message,
redact_secrets,
_redact_and_sanitize,
verbose_proxy_logger,
)
from litellm.caching.caching import DualCache, InMemoryCache, RedisCache
@ -35,9 +34,7 @@ class _PROXY_BatchRedisRequests(CustomLogger):
elif debug_level == "INFO":
verbose_proxy_logger.debug(print_statement)
if litellm.set_verbose is True:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
async def async_pre_call_hook(
self,

View file

@ -9,8 +9,7 @@ from typing_extensions import TypedDict
import litellm
from litellm import DualCache, EmbeddingResponse, ModelResponse, TextCompletionResponse
from litellm._logging import (
_sanitize_log_message,
redact_secrets,
_redact_and_sanitize,
verbose_proxy_logger,
)
from litellm.integrations.custom_logger import CustomLogger
@ -55,9 +54,7 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger):
try:
verbose_proxy_logger.debug(print_statement)
if litellm.set_verbose:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -14,8 +14,7 @@ from fastapi import HTTPException
import litellm
from litellm._logging import (
_sanitize_log_message,
redact_secrets,
_redact_and_sanitize,
verbose_proxy_logger,
)
from litellm.caching.caching import DualCache
@ -76,9 +75,7 @@ class _OPTIONAL_PromptInjectionDetection(CustomLogger):
verbose_proxy_logger.debug(print_statement)
if litellm.set_verbose is True:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
def update_environment(self, router: Optional[Router] = None):
self.llm_router = router

View file

@ -89,8 +89,8 @@ from litellm import (
Router,
)
from litellm._logging import (
_redact_and_sanitize,
_redact_string,
_sanitize_log_message,
verbose_proxy_logger,
)
from litellm._service_logger import ServiceLogging, ServiceTypes
@ -201,7 +201,7 @@ def print_verbose(print_statement):
verbose_proxy_logger.debug("{}\n{}".format(print_statement, traceback.format_exc()))
if litellm.set_verbose:
print( # noqa: T201
f"LiteLLM Proxy: {_sanitize_log_message(_redact_string(str(print_statement)))}"
f"LiteLLM Proxy: {_redact_and_sanitize(print_statement)}"
)

View file

@ -397,8 +397,7 @@ from litellm.llms.base_llm.skills.transformation import BaseSkillsAPIConfig
from ._logging import (
_is_debugging_on,
_sanitize_log_message,
redact_secrets,
_redact_and_sanitize,
verbose_logger,
)
from .caching.caching import (
@ -497,9 +496,7 @@ def print_verbose(
elif log_level == "ERROR":
verbose_logger.error(print_statement)
if litellm.set_verbose is True and logger_only is False:
print( # noqa: T201
_sanitize_log_message(redact_secrets(str(print_statement)))
)
print(_redact_and_sanitize(print_statement)) # noqa: T201
except Exception:
pass

View file

@ -480,6 +480,14 @@ def test_proxy_hook_print_verbose_methods_redact_and_sanitize(capsys, monkeypatc
assert "\\r\\n" in out and "\x9b" not in out, cls.__name__
def test_redact_and_sanitize_helper_combines_both_stages():
from litellm._logging import _redact_and_sanitize
out = _redact_and_sanitize({"api_key": SECRET_VALUE, "note": "a\r\nb\x9b"})
assert SECRET_VALUE not in out and "REDACTED" in out
assert "\\r\\n" in out and "\x9b" not in out
def test_utils_print_verbose_skips_stdout_when_not_verbose(capsys, monkeypatch):
from litellm.utils import print_verbose as utils_print_verbose