diff --git a/litellm/_logging.py b/litellm/_logging.py index 6d154033aa5..8974f85f517 100644 --- a/litellm/_logging.py +++ b/litellm/_logging.py @@ -908,7 +908,7 @@ def print_startup_message(message: str) -> None: stdout is read by a log pipeline, so the message goes out as one INFO record of the proxy logger instead, on stdout like the print whatever handlers the logging config gave that logger, with the ANSI color codes and surrounding blank lines removed, located at the caller and emitted at any log - level; a blank spacer produces no record. + level, whole and unredacted as the print showed it; a blank spacer produces no record. """ if not litellm.json_logs: print(message) # noqa: T201 # plain mode: this is the operator-terminal print it replaces @@ -928,20 +928,8 @@ def print_startup_message(message: str) -> None: None, func=caller.f_code.co_name if caller is not None else None, ) - # The print this replaces shows at any log level, so the record skips the level checks of the - # proxy logger (at the root's WARNING in the CLI process until initialize() runs) and of its JSON - # handler (LITELLM_LOG). It also skips the logger's stdout cap: that cap bounds echoed request - # payloads, while a startup message is operator text the print showed whole (the required migration - # SQL, the policy list). It skips secret redaction too, which the print never had and whose sk- - # pattern turns a model named risk-assessment-gpt4o into riREDACTED. The processed record counts as - # scrubbed, so the handler's secret filter and the formatter leave it alone too. if not _process_record(record, base64_limit=_get_max_base64_length_stdout_log(), text_limit=0, redact=False): return - # The print wrote to stdout whatever the logging config said, so the record goes through the proxy's - # own JSON handler on stdout rather than the proxy logger's handlers: uvicorn applies an operator - # --log_config after proxy_cli's turn_on_json() and before initialize() re-installs the JSON handler - # through load_config(), and in that window, where the banner and the feedback box are emitted, a - # config that names "LiteLLM Proxy" has left the logger with no handler or with the operator's own. _startup_json_handler().handle(record) diff --git a/litellm/proxy/policy_engine/init_policies.py b/litellm/proxy/policy_engine/init_policies.py index 82e090a1522..59d15b0ba0a 100644 --- a/litellm/proxy/policy_engine/init_policies.py +++ b/litellm/proxy/policy_engine/init_policies.py @@ -8,6 +8,7 @@ Configuration structure: import sys from collections.abc import Mapping, Sequence +from itertools import chain from typing import TYPE_CHECKING, Any, Final, Optional from litellm._logging import print_startup_message, verbose_proxy_logger @@ -26,6 +27,42 @@ _yellow_color_code: Final = "\033[93m" _reset_color_code: Final = "\033[0m" +def _policy_lines(policy_name: str, policy_data: Mapping[str, object]) -> tuple[str, ...]: + guardrails: Final = policy_data.get("guardrails", {}) + inherit: Final = policy_data.get("inherit") + condition: Final = policy_data.get("condition") + description: Final = policy_data.get("description") + + guardrails_add: Final = guardrails.get("add", []) if isinstance(guardrails, dict) else [] + guardrails_remove: Final = guardrails.get("remove", []) if isinstance(guardrails, dict) else [] + model_condition: Final = condition.get("model") if isinstance(condition, dict) else None + inherit_str: Final = f" (inherits: {inherit})" if inherit else "" + + return ( + f"{_blue_color_code} - {policy_name}{inherit_str}{_reset_color_code}", + *((f" description: {description}",) if description else ()), + *((f" guardrails.add: {guardrails_add}",) if guardrails_add else ()), + *((f" guardrails.remove: {guardrails_remove}",) if guardrails_remove else ()), + *((f" condition.model: {model_condition}",) if model_condition else ()), + ) + + +def _attachment_line(attachment: Mapping[str, object]) -> str: + policy: Final = attachment.get("policy", "unknown") + scope: Final = attachment.get("scope") + teams: Final = attachment.get("teams") + keys: Final = attachment.get("keys") + models: Final = attachment.get("models") + + scope_parts: Final = ( + *(("scope=* (global)",) if scope == "*" else ()), + *((f"teams={teams}",) if teams else ()), + *((f"keys={keys}",) if keys else ()), + *((f"models={models}",) if models else ()), + ) + return f" - {policy} -> {', '.join(scope_parts) if scope_parts else 'all'}" + + def _print_policies_on_startup( policies_config: Mapping[str, Mapping[str, object]], policy_attachments_config: Sequence[Mapping[str, object]] | None = None, @@ -33,60 +70,29 @@ def _print_policies_on_startup( """ Print loaded policies to console on startup (similar to model list). """ - lines: Final = [ + header: Final = ( f"{_green_color_code}\nLiteLLM Policy Engine: Loaded {len(policies_config)} policies{_reset_color_code}\n" - ] - - for policy_name, policy_data in policies_config.items(): - guardrails = policy_data.get("guardrails", {}) - inherit = policy_data.get("inherit") - condition = policy_data.get("condition") - description = policy_data.get("description") - - guardrails_add = guardrails.get("add", []) if isinstance(guardrails, dict) else [] - guardrails_remove = guardrails.get("remove", []) if isinstance(guardrails, dict) else [] - inherit_str = f" (inherits: {inherit})" if inherit else "" - - lines.append(f"{_blue_color_code} - {policy_name}{inherit_str}{_reset_color_code}") - if description: - lines.append(f" description: {description}") - if guardrails_add: - lines.append(f" guardrails.add: {guardrails_add}") - if guardrails_remove: - lines.append(f" guardrails.remove: {guardrails_remove}") - if condition: - model_condition = condition.get("model") if isinstance(condition, dict) else None - if model_condition: - lines.append(f" condition.model: {model_condition}") - - # Print attachments - if policy_attachments_config: - lines.append( - f"\n{_yellow_color_code}Policy Attachments: {len(policy_attachments_config)} attachment(s){_reset_color_code}" + ) + policy_lines: Final = tuple( + chain.from_iterable( + _policy_lines(policy_name, policy_data) for policy_name, policy_data in policies_config.items() ) - for attachment in policy_attachments_config: - policy = attachment.get("policy", "unknown") - scope = attachment.get("scope") - teams = attachment.get("teams") - keys = attachment.get("keys") - models = attachment.get("models") + ) - scope_parts = [] - if scope == "*": - scope_parts.append("scope=* (global)") - if teams: - scope_parts.append(f"teams={teams}") - if keys: - scope_parts.append(f"keys={keys}") - if models: - scope_parts.append(f"models={models}") - scope_str = ", ".join(scope_parts) if scope_parts else "all" - - lines.append(f" - {policy} -> {scope_str}") - lines.append("") - print_startup_message("\n".join(lines)) + if policy_attachments_config: + print_startup_message( + "\n".join( + ( + header, + *policy_lines, + f"\n{_yellow_color_code}Policy Attachments: {len(policy_attachments_config)} attachment(s){_reset_color_code}", + *(_attachment_line(attachment) for attachment in policy_attachments_config), + "", + ) + ) + ) else: - print_startup_message("\n".join(lines)) + print_startup_message("\n".join((header, *policy_lines))) print_startup_message( f"\n{_yellow_color_code}Warning: No policy_attachments configured. Policies will not be applied to any requests.{_reset_color_code}\n", ) diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.py index e4936ca5aac..316434cc60b 100644 --- a/litellm/proxy/proxy_cli.py +++ b/litellm/proxy/proxy_cli.py @@ -1323,9 +1323,6 @@ def run_server( import litellm litellm.json_logs = True - # Workers that uvicorn or granian spawn import litellm afresh and read this at import, - # so they log JSON from their first record instead of plain text until load_config - # reaches the json_logs key. os.environ["JSON_LOGS"] = "true" litellm.turn_on_json() diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 2f1f460fc8e..85dc28202eb 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9627,8 +9627,6 @@ async def initialize( prisma_client from litellm.proxy.common_utils.banner import LITELLM_BANNER, show_banner - # show_banner() also serves the `lite` shell, which runs against an uninitialized SDK (#46803), so - # the JSON-mode choice stays out of it. if litellm.json_logs: print_startup_message(LITELLM_BANNER) else: diff --git a/tests/integration/configuration/test_json_logs_startup.py b/tests/integration/configuration/test_json_logs_startup.py index f197386e9f3..de139b7986f 100644 --- a/tests/integration/configuration/test_json_logs_startup.py +++ b/tests/integration/configuration/test_json_logs_startup.py @@ -31,15 +31,10 @@ from tests.integration._support.process import owned_proxy_process, refused_boot JSON_RECORD: Final = TypeAdapter(dict[str, JsonValue]) PLAIN_LOG_LINE: Final = re.compile(r"^\d\d:\d\d:\d\d - LiteLLM") -# prisma-client-py starts the Prisma query engine on the proxy's own stdout, and the engine announces -# its build hash there in two writes (the text, then the newline), so with two workers the two -# announcements land glued together or glued to a neighbor's line. The text is not litellm's and is -# removed before the lines are read. PRISMA_ENGINE_TEXT: Final = re.compile(r"query-engine [0-9a-f]{40}") STARTED_WORKER: Final = "Started server process" MODEL_LIST_HEADER: Final = "Proxy initialized with Config, Set models:" CACHE_LINE: Final = "Setting Cache on Proxy" -# printed by the CLI process before any worker exists, so it proves the launcher itself logs JSON CONNECTION_BUDGET_LINE: Final = "Postgres connection budget" REDIS_AUTH_CACHE_LINE: Final = "enable_redis_auth_cache" BANNER_FRAGMENT: Final = "██╗" @@ -48,8 +43,6 @@ WORKER_COUNTS: Final = (1, 2) UNKNOWN_MODEL_STATUS: Final = 400 BURST_SIZE: Final = 16 CACHE_SETTINGS: Final[Mapping[str, JsonValue]] = {"cache": True, "cache_params": {"type": "redis"}} -# the policy list and the required migration SQL are the startup listings that outgrow the stdout cap -# (MAX_STRING_LENGTH_STDOUT_LOG, 4096 by default) that bounds echoed request payloads STDOUT_CAP: Final = 4096 POLICY_COUNT: Final = 60 POLICY_LIST_HEADER: Final = "LiteLLM Policy Engine: Loaded" @@ -57,8 +50,6 @@ TRUNCATION_MARKER: Final = "litellm_truncated" DEPRECATED_FLAG: Final = "--enforce_prisma_migration_check" DEPRECATED_FLAG_NOTICE: Final = "--enforce_prisma_migration_check is deprecated and has no effect" UNPARSEABLE_CONFIG: Final = "model_list: [\n - bad: yaml: here\n" -# an operator logging config of the usual shape: it sets the proxy logger's level and gives it no handler -# of its own, and formats everything else, uvicorn included, as JSON through the root handler OPERATOR_LOG_CONFIG: Final[Mapping[str, JsonValue]] = { "version": 1, "disable_existing_loggers": False, @@ -67,7 +58,6 @@ OPERATOR_LOG_CONFIG: Final[Mapping[str, JsonValue]] = { "loggers": {"LiteLLM Proxy": {"level": "INFO"}}, "root": {"level": "INFO", "handlers": ["stdout"]}, } -# an operator logging config that silences the proxy logger with a handler of its own and keeps the rest JSON OPERATOR_SILENCING_LOG_CONFIG: Final[Mapping[str, JsonValue]] = { "version": 1, "disable_existing_loggers": False, @@ -223,8 +213,6 @@ def test_json_logs_env_keeps_every_line_json(gateway: Gateway, tmp_path: Path, w @pytest.mark.parametrize("workers", WORKER_COUNTS) def test_json_logs_config_keeps_every_line_json(gateway: Gateway, tmp_path: Path, workers: int) -> None: models: Final = _models() - # cache is listed before json_logs on purpose: the cache branch logs the Redis auth-cache line - # while the config is still being read, so JSON mode must not depend on the key order. config: Final = _write_config(tmp_path, gateway.upstream_url, models, {**CACHE_SETTINGS, "json_logs": True}) log: Final = _boot_serve_and_stop(gateway, tmp_path, config, {"LITELLM_LOG": "INFO"}, workers, models) _assert_every_line_is_json(log, workers, models) diff --git a/tests/unit/proxy/db/conftest.py b/tests/unit/proxy/db/conftest.py index d2488df547c..4d775142dd5 100644 --- a/tests/unit/proxy/db/conftest.py +++ b/tests/unit/proxy/db/conftest.py @@ -100,8 +100,6 @@ if not earlier_calls and os.environ.get("FAKE_PRISMA_HANG_FIRST"): grandchild = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(600)"]) pathlib.Path(os.environ["FAKE_PRISMA_GRANDCHILD_PIDFILE"]).write_text(str(grandchild.pid)) time.sleep(600) -if os.environ.get("FAKE_PRISMA_STDOUT"): - sys.stdout.write(os.environ["FAKE_PRISMA_STDOUT"]) sys.exit(0) """ @@ -113,8 +111,6 @@ class FakePrismaCli: With FAKE_PRISMA_HANG_FIRST set it hangs on its first call from a process tree of its own, the way the real CLI wraps Node around a Rust schema engine, so a timeout that kills only the direct child leaves the rest of that tree running. - With FAKE_PRISMA_STDOUT set it writes that text to stdout, the way `migrate diff - --script` prints the SQL that would close a schema gap. """ calls_file: Path @@ -146,7 +142,6 @@ def fake_prisma_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Generato monkeypatch.setenv("FAKE_PRISMA_GRANDCHILD_PIDFILE", str(cli.grandchild_pidfile)) monkeypatch.setenv("LITELLM_PRISMA_COMMAND_TIMEOUT", "1") monkeypatch.delenv("FAKE_PRISMA_HANG_FIRST", raising=False) - monkeypatch.delenv("FAKE_PRISMA_STDOUT", raising=False) yield cli if cli.grandchild_pidfile.exists(): try: diff --git a/tests/unit/proxy/db/test_check_migration.py b/tests/unit/proxy/db/test_check_migration.py index 118d56faa69..b5685676941 100644 --- a/tests/unit/proxy/db/test_check_migration.py +++ b/tests/unit/proxy/db/test_check_migration.py @@ -77,35 +77,31 @@ def test_migrate_diff_without_the_prisma_runner_skips_instead_of_crashing_boot(m assert check_prisma_schema_diff_helper("postgresql://u:p@localhost:9/x") == (False, []) -def test_migrate_diff_announces_the_schema_changes_as_one_json_record(fake_prisma_cli, monkeypatch, capfd): +def test_a_skipped_migration_diff_is_one_json_record_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """ - The "Changes to DB Schema detected" notice and its SQL went to stdout as plaintext; - with json_logs on they are one INFO record, so a log pipeline keeps parsing. The - loggers sit at WARNING, as they do at boot without LITELLM_LOG, and the record - still goes out: the print it replaces had no level. + With json_logs on, the notice that the migration diff was skipped is one INFO record on stdout, + also while the loggers sit at WARNING, as they do at boot without LITELLM_LOG. """ from litellm.proxy.db.check_migration import check_prisma_schema_diff_helper - monkeypatch.setenv( - "FAKE_PRISMA_STDOUT", '-- AlterTable\nALTER TABLE "LiteLLM_UserTable" ADD COLUMN "nickname" TEXT;\n' - ) + monkeypatch.setitem(sys.modules, "litellm_proxy_extras.prisma_toolchain", None) monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() verbose_logger.setLevel(logging.WARNING) verbose_proxy_logger.setLevel(logging.WARNING) - assert check_prisma_schema_diff_helper("postgresql://u:p@localhost:9/x") == ( - True, - ['ALTER TABLE "LiteLLM_UserTable" ADD COLUMN "nickname" TEXT;'], - ) + assert check_prisma_schema_diff_helper("postgresql://u:p@localhost:9/x") == (False, []) out, err = capfd.readouterr() assert err == "" lines: Final = [raw for raw in out.splitlines() if raw.strip()] assert len(lines) == 1, lines record: Final = json.loads(lines[0]) - assert record["level"] == "INFO" - assert record["message"] == ( - 'Changes to DB Schema detected\nRequired SQL commands:\nALTER TABLE "LiteLLM_UserTable" ADD COLUMN "nickname" TEXT;' + assert (record["level"], record["message"]) == ( + "INFO", + "Skipping the migration diff: litellm-proxy-extras has no Prisma runner. " + "Error: import of litellm_proxy_extras.prisma_toolchain halted; None in sys.modules", ) diff --git a/tests/unit/proxy/policy_engine/test_init_policies.py b/tests/unit/proxy/policy_engine/test_init_policies.py index 2b6b2c85580..b9b5f278252 100644 --- a/tests/unit/proxy/policy_engine/test_init_policies.py +++ b/tests/unit/proxy/policy_engine/test_init_policies.py @@ -53,7 +53,12 @@ NO_ATTACHMENTS_WARNING: Final = ( ], ids=["with-attachments", "without-attachments"], ) -def test_policy_table_prints_unchanged_in_plain_mode(monkeypatch, capfd, attachments, expected): +def test_policy_table_prints_unchanged_in_plain_mode( + monkeypatch: pytest.MonkeyPatch, + capfd: pytest.CaptureFixture[str], + attachments: Sequence[Mapping[str, object]] | None, + expected: str, +) -> None: """Operators without JSON logs keep the colored policy table byte for byte.""" monkeypatch.setattr(litellm, "json_logs", False) @@ -62,10 +67,12 @@ def test_policy_table_prints_unchanged_in_plain_mode(monkeypatch, capfd, attachm assert capfd.readouterr() == (expected, "") -def test_policy_table_is_one_json_record_in_json_mode(monkeypatch, capfd): +def test_policy_table_is_one_json_record_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """With JSON logs on the whole table, attachments included, is one INFO record without color codes.""" monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() _print_policies_on_startup(POLICIES, ATTACHMENTS) @@ -88,7 +95,9 @@ def test_policy_table_is_one_json_record_in_json_mode(monkeypatch, capfd): ) -def test_missing_attachments_warning_is_a_second_json_record(monkeypatch, capfd): +def test_missing_attachments_warning_is_a_second_json_record( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """Without attachments the table and the yellow warning are two INFO records on stdout, where the two prints put them.""" monkeypatch.setattr(litellm, "json_logs", True) diff --git a/tests/unit/proxy/test_proxy_cli.py b/tests/unit/proxy/test_proxy_cli.py index f8da4ce496b..d77731aa83b 100644 --- a/tests/unit/proxy/test_proxy_cli.py +++ b/tests/unit/proxy/test_proxy_cli.py @@ -10,6 +10,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import click import fastapi +import httpx import pytest @@ -2878,20 +2879,24 @@ class TestRunServerDbSetup: mock_setup_database.assert_called_once_with(use_migrate=True, use_v2_resolver=True) assert ("--enforce_prisma_migration_check is deprecated and has no effect" in capsys.readouterr().out) is warned - @pytest.mark.parametrize("argument", ("--health", "--test"), ids=("health", "test")) - def test_the_retired_enforce_prisma_migration_check_opt_in_warns_on_health_and_test(self, argument, capsys): - """`--health` and `--test` return before the config is read, where a server start emits the - deprecation line; they still print it, as they did when the notice came before those returns.""" - from litellm.proxy.proxy_cli import ProxyInitializationHelpers, run_server + def test_the_retired_enforce_prisma_migration_check_opt_in_warns_on_health( + self, capsys: pytest.CaptureFixture[str] + ) -> None: + """`--health` returns before the config is read, where a server start emits the deprecation line; + it still prints it, as it did when the notice came before that return.""" + with patch("httpx.get", return_value=httpx.Response(200, json={})) as mock_get: + run_server.main(["--health", "--enforce_prisma_migration_check"], standalone_mode=False) - with ( - patch.object(ProxyInitializationHelpers, "run_health_check") as mock_health_check, - patch.object(ProxyInitializationHelpers, "run_test_chat_completion") as mock_test_chat, - ): - run_server.main([argument, "--enforce_prisma_migration_check"], standalone_mode=False) + mock_get.assert_called_once_with(url="http://0.0.0.0:4000/health") + assert "--enforce_prisma_migration_check is deprecated and has no effect" in capsys.readouterr().out + + def test_the_retired_enforce_prisma_migration_check_opt_in_warns_on_test( + self, capsys: pytest.CaptureFixture[str] + ) -> None: + """`--test` returns before the config is read too, and prints the deprecation line before its request.""" + with pytest.raises(ValueError, match="Invalid test value"): + run_server.main(["--test", "--enforce_prisma_migration_check"], standalone_mode=False) - expected_calls = (1, 0) if argument == "--health" else (0, 1) - assert (mock_health_check.call_count, mock_test_chat.call_count) == expected_calls assert "--enforce_prisma_migration_check is deprecated and has no effect" in capsys.readouterr().out @patch("subprocess.run") @@ -3956,43 +3961,35 @@ class TestValidateConfigFlag: mock_popen.assert_not_called() +def _boot_from_config_file(config_path: Path, *flags: str) -> str | None: + env: Final = {k: v for k, v in os.environ.items() if k not in ("DATABASE_URL", "DIRECT_URL", "JSON_LOGS")} + with patch.dict(os.environ, env, clear=True): + run_server.main(["--config", str(config_path), "--skip_server_startup", *flags], standalone_mode=False) + return os.environ.get("JSON_LOGS") + + class TestJsonLogsConfigReachesSpawnedWorkers: """`litellm_settings.json_logs: true` turns JSON on inside the CLI process, but the workers that uvicorn or granian spawn are fresh interpreters that pick plain or JSON from the JSON_LOGS environment variable at import. The CLI has to export it, or every worker logs plain text until its own load_config reaches the key.""" - @staticmethod - def _json_logs_env_after_boot(config_path: Path, litellm_settings: dict[str, object]) -> str | None: - config_path.write_text(yaml.dump({"model_list": [], "litellm_settings": litellm_settings})) - mock_proxy_config: Final = MagicMock() - mock_proxy_config.return_value.get_config = AsyncMock(return_value=yaml.safe_load(config_path.read_text())) - mock_proxy_module: Final = MagicMock( - app=MagicMock(), - ProxyConfig=mock_proxy_config, - KeyManagementSettings=MagicMock(), - save_worker_config=MagicMock(), - ) - clean_env: Final = {k: v for k, v in os.environ.items() if k not in ("DATABASE_URL", "DIRECT_URL", "JSON_LOGS")} - with ( - patch.dict(os.environ, clean_env, clear=True), - patch.dict( - "sys.modules", - {"proxy_server": mock_proxy_module, "litellm.proxy.proxy_server": mock_proxy_module}, - ), - patch("atexit.register"), - patch("litellm.turn_on_json"), # test-quality-ok: it swaps every logger handler in this pytest worker - ): - run_server.main(["--config", str(config_path), "--local", "--skip_server_startup"], standalone_mode=False) - return os.environ.get("JSON_LOGS") - - def test_json_logs_true_exports_json_logs_to_spawned_workers(self, tmp_path: Path, monkeypatch) -> None: - monkeypatch.setattr(litellm, "json_logs", False) # run_server flips it on; undone at teardown - assert self._json_logs_env_after_boot(tmp_path / "config.yaml", {"json_logs": True}) == "true" - - def test_plain_logs_config_leaves_json_logs_unset(self, tmp_path: Path, monkeypatch) -> None: + def test_json_logs_true_exports_json_logs_to_spawned_workers( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: monkeypatch.setattr(litellm, "json_logs", False) - assert self._json_logs_env_after_boot(tmp_path / "config.yaml", {}) is None + monkeypatch.setattr(sys, "excepthook", sys.excepthook) + config_path: Final = tmp_path / "config.yaml" + config_path.write_text(yaml.dump({"model_list": [], "litellm_settings": {"json_logs": True}})) + + assert _boot_from_config_file(config_path) == "true" + + def test_plain_logs_config_leaves_json_logs_unset(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(litellm, "json_logs", False) + config_path: Final = tmp_path / "config.yaml" + config_path.write_text(yaml.dump({"model_list": [], "litellm_settings": {}})) + + assert _boot_from_config_file(config_path) is None class TestEnforcePrismaMigrationCheckNoticeAroundTheConfigRead: @@ -4001,68 +3998,53 @@ class TestEnforcePrismaMigrationCheckNoticeAroundTheConfigRead: and a boot whose config read fails still prints it before the error, as it did before the wait.""" NOTICE: Final = "--enforce_prisma_migration_check is deprecated and has no effect" + FLAG: Final = "--enforce_prisma_migration_check" - @staticmethod - def _boot(config_path: Path, get_config: AsyncMock) -> None: - mock_proxy_config: Final = MagicMock() - mock_proxy_config.return_value.get_config = get_config - mock_proxy_module: Final = MagicMock( - app=MagicMock(), - ProxyConfig=mock_proxy_config, - KeyManagementSettings=MagicMock(), - save_worker_config=MagicMock(), - ) - clean_env: Final = {k: v for k, v in os.environ.items() if k not in ("DATABASE_URL", "DIRECT_URL", "JSON_LOGS")} - with ( - patch.dict(os.environ, clean_env, clear=True), - patch.dict( - "sys.modules", - {"proxy_server": mock_proxy_module, "litellm.proxy.proxy_server": mock_proxy_module}, - ), - patch("atexit.register"), - patch("litellm.turn_on_json"), # test-quality-ok: it swaps every logger handler in this pytest worker - ): - run_server.main( - ["--config", str(config_path), "--local", "--skip_server_startup", "--enforce_prisma_migration_check"], - standalone_mode=False, - ) - - def test_a_config_read_that_fails_still_prints_the_notice(self, tmp_path: Path, monkeypatch, capsys) -> None: + def test_a_config_read_that_fails_still_prints_the_notice( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] + ) -> None: monkeypatch.setattr(litellm, "json_logs", False) - missing: Final = tmp_path / "missing.yaml" - with pytest.raises(FileNotFoundError): - self._boot(missing, AsyncMock(side_effect=FileNotFoundError(f"Config file not found: {missing}"))) + with pytest.raises(Exception, match="Config file not found"): + _boot_from_config_file(tmp_path / "missing.yaml", self.FLAG) assert capsys.readouterr().out.count(self.NOTICE) == 1 - def test_a_plain_config_prints_the_notice_once(self, tmp_path: Path, monkeypatch, capsys) -> None: + def test_a_plain_config_prints_the_notice_once( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] + ) -> None: monkeypatch.setattr(litellm, "json_logs", False) + config_path: Final = tmp_path / "config.yaml" + config_path.write_text(yaml.dump({"model_list": [], "litellm_settings": {}})) - self._boot(tmp_path / "config.yaml", AsyncMock(return_value={"model_list": [], "litellm_settings": {}})) + _boot_from_config_file(config_path, self.FLAG) assert capsys.readouterr().out.count(self.NOTICE) == 1 def test_a_config_only_json_logs_makes_the_notice_one_json_record( - self, tmp_path: Path, monkeypatch, capsys + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: - monkeypatch.setattr(litellm, "json_logs", False) # run_server flips it on; undone at teardown - config: Final = {"model_list": [], "litellm_settings": {"json_logs": True}} + monkeypatch.setattr(litellm, "json_logs", False) + monkeypatch.setattr(sys, "excepthook", sys.excepthook) + config_path: Final = tmp_path / "config.yaml" + config_path.write_text(yaml.dump({"model_list": [], "litellm_settings": {"json_logs": True}})) - self._boot(tmp_path / "config.yaml", AsyncMock(return_value=config)) + _boot_from_config_file(config_path, self.FLAG) notice_lines: Final = [line for line in capsys.readouterr().out.splitlines() if self.NOTICE in line] assert len(notice_lines) == 1, notice_lines assert self.NOTICE in json.loads(notice_lines[0])["message"] -def test_granian_flag_notices_are_json_records_in_json_mode(monkeypatch, capfd): +def test_granian_flag_notices_are_json_records_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """The yellow notices for the flags Granian ignores (`--max_requests_before_restart`, `--ciphers`) are printed while the launcher starts; with json_logs on they are INFO records on stdout like every other line, not plaintext between the records.""" pytest.importorskip("granian") monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() with ( diff --git a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py index bcd010e3eed..dc542eb302b 100644 --- a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py +++ b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py @@ -15588,11 +15588,13 @@ async def test_spend_capture_rate_check_job_clears_the_gauge_once_the_setting_is ] -def test_feedback_box_is_one_json_record_in_json_mode(monkeypatch, capfd): +def test_feedback_box_is_one_json_record_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """The feedback box is a dozen terminal lines; with JSON logs on it has to land as one parseable record, not a plaintext line per row.""" monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() proxy_server_module.generate_feedback_box() @@ -15608,7 +15610,9 @@ def test_feedback_box_is_one_json_record_in_json_mode(monkeypatch, capfd): @pytest.mark.asyncio -async def test_initialize_prints_the_banner_between_blank_lines_in_plain_mode(monkeypatch, capfd): +async def test_initialize_prints_the_banner_between_blank_lines_in_plain_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """Operators without JSON logs see the banner exactly as before, through show_banner()'s flushing click.echo.""" monkeypatch.setattr(litellm, "json_logs", False) @@ -15622,11 +15626,13 @@ async def test_initialize_prints_the_banner_between_blank_lines_in_plain_mode(mo @pytest.mark.asyncio -async def test_initialize_prints_the_banner_as_one_json_record_in_json_mode(monkeypatch, capfd): +async def test_initialize_prints_the_banner_as_one_json_record_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """With JSON logs on, the six banner rows are one INFO record with their indentation kept, so the first line a worker writes parses like every other.""" monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) monkeypatch.setenv("LITELLM_DONT_SHOW_FEEDBACK_BOX", "true") turn_on_json() diff --git a/tests/unit/test_logging.py b/tests/unit/test_logging.py index 3174e93bc1f..36be864a6f7 100644 --- a/tests/unit/test_logging.py +++ b/tests/unit/test_logging.py @@ -1827,7 +1827,9 @@ def test_print_verbose_formats_args_only_when_set_verbose_is_on(monkeypatch, cap assert capsys.readouterr().out == "key k1 value cached-vector\n" -def test_print_startup_message_prints_plain_mode_text_unchanged(monkeypatch, capfd): +def test_print_startup_message_prints_plain_mode_text_unchanged( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """Operators without JSON logs keep the colored, spaced terminal output exactly as before.""" monkeypatch.setattr(litellm, "json_logs", False) @@ -1838,12 +1840,14 @@ def test_print_startup_message_prints_plain_mode_text_unchanged(monkeypatch, cap assert err == "" -def test_print_startup_message_is_one_json_record_in_json_mode(monkeypatch, capfd): +def test_print_startup_message_is_one_json_record_in_json_mode( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """With JSON logs on, a multi-line colored startup message is one INFO record with the colors and the surrounding blank lines removed, so a log pipeline parses it like every other line. The logger sits at WARNING, as it does in the CLI process, and the record still goes out: a print has no level.""" monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() verbose_proxy_logger.setLevel(logging.WARNING) @@ -1859,11 +1863,12 @@ def test_print_startup_message_is_one_json_record_in_json_mode(monkeypatch, capf assert record["message"] == "LiteLLM: Proxy initialized with Config, Set models:\n gpt-4o" assert record["level"] == "INFO" assert record["component"] == "LiteLLM Proxy" - # the record names the call site, not the helper, so operators can find the message assert str(record["logger"]).startswith("test_logging.py:"), record["logger"] -def test_print_startup_message_yellow_notice_is_an_info_record_on_stdout(monkeypatch, capfd): +def test_print_startup_message_yellow_notice_is_an_info_record_on_stdout( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """An operator notice printed in yellow (an ignored flag, a deprecation) stays on stdout in JSON mode, where the print put it, as one INFO record with the color codes removed.""" monkeypatch.setattr(litellm, "json_logs", True) @@ -1882,7 +1887,9 @@ def test_print_startup_message_yellow_notice_is_an_info_record_on_stdout(monkeyp assert record["level"] == "INFO" -def test_print_startup_message_shows_above_the_json_handler_level(monkeypatch, capfd): +def test_print_startup_message_shows_above_the_json_handler_level( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """`LITELLM_LOG=WARNING` sets the JSON handler to WARNING, which drops the INFO logger lines; the startup notices still go out, as the prints they replace did at every level.""" monkeypatch.setattr(litellm, "json_logs", True) @@ -1901,8 +1908,8 @@ def test_print_startup_message_shows_above_the_json_handler_level(monkeypatch, c def test_print_startup_message_emits_json_after_an_operator_log_config_removed_the_handlers( - request, monkeypatch, capfd -): + request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """uvicorn applies an operator `--log_config` after proxy_cli's turn_on_json() and before initialize() re-installs the JSON handler through load_config(); a config that names "LiteLLM Proxy" leaves the logger without handlers in that window, where the banner and the feedback box are emitted, and they @@ -1912,7 +1919,7 @@ def test_print_startup_message_emits_json_after_an_operator_log_config_removed_t monkeypatch.setattr(litellm, "json_logs", True) monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() - request.addfinalizer(turn_on_json) # put the JSON handler back for the tests that follow + request.addfinalizer(turn_on_json) logging.config.dictConfig( {"version": 1, "disable_existing_loggers": False, "loggers": {"LiteLLM Proxy": {"level": "INFO"}}} ) @@ -1943,8 +1950,8 @@ OPERATOR_HANDLERS_ON_THE_PROXY_LOGGER: Final = { @pytest.mark.parametrize("handler", sorted(OPERATOR_HANDLERS_ON_THE_PROXY_LOGGER)) def test_print_startup_message_stays_on_stdout_under_an_operator_handler_on_the_proxy_logger( - request, monkeypatch, capfd, handler: str -): + request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str], handler: str +) -> None: """An operator `--log_config` that gives "LiteLLM Proxy" a handler of its own, a NullHandler that silences it or a plain WARNING-level handler on stderr, is in force when the banner and the feedback box are emitted; the print they replace wrote to stdout whatever the logging config said, so the record still @@ -1954,7 +1961,7 @@ def test_print_startup_message_stays_on_stdout_under_an_operator_handler_on_the_ monkeypatch.setattr(litellm, "json_logs", True) monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() - request.addfinalizer(turn_on_json) # put the JSON handler back for the tests that follow + request.addfinalizer(turn_on_json) logging.config.dictConfig( { "version": 1, @@ -1980,13 +1987,15 @@ def test_print_startup_message_stays_on_stdout_under_an_operator_handler_on_the_ assert record["component"] == "LiteLLM Proxy" -def test_print_startup_message_keeps_a_listing_longer_than_the_stdout_cap_whole(monkeypatch, capfd): +def test_print_startup_message_keeps_a_listing_longer_than_the_stdout_cap_whole( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """The required migration SQL and the policy list run past MAX_STRING_LENGTH_STDOUT_LOG on a drifted deployment. The prints they replace showed them whole, and that cap exists for echoed request payloads, so the JSON record carries the whole listing too.""" monkeypatch.setenv("MAX_STRING_LENGTH_STDOUT_LOG", "500") monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() commands: Final = tuple(f'ALTER TABLE "LiteLLM_Table{n}" ADD COLUMN "column_{n}" TEXT;' for n in range(40)) listing: Final = "\n".join(("Changes to DB Schema detected", "Required SQL commands:", *commands)) @@ -2003,11 +2012,13 @@ def test_print_startup_message_keeps_a_listing_longer_than_the_stdout_cap_whole( assert record["message"] == listing -def test_print_startup_message_keeps_a_model_name_the_secret_patterns_would_match(monkeypatch, capfd): +def test_print_startup_message_keeps_a_model_name_the_secret_patterns_would_match( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """The model list printed `risk-assessment-gpt4o` as configured; the sk- key pattern matches inside that name, so the JSON record has to carry the name as the print did, not `riREDACTED`.""" monkeypatch.setattr(litellm, "json_logs", True) - monkeypatch.setattr(sys, "excepthook", sys.excepthook) # turn_on_json swaps it; put it back at teardown + monkeypatch.setattr(sys, "excepthook", sys.excepthook) turn_on_json() assert secret_redaction.redact_string("risk-assessment-gpt4o") != "risk-assessment-gpt4o" @@ -2020,7 +2031,9 @@ def test_print_startup_message_keeps_a_model_name_the_secret_patterns_would_matc assert json.loads(lines[0])["message"] == " risk-assessment-gpt4o" -def test_print_startup_message_blank_spacer_emits_no_json_record(monkeypatch, capfd): +def test_print_startup_message_blank_spacer_emits_no_json_record( + monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] +) -> None: """A spacer that only pads the terminal has nothing to say to a log pipeline.""" monkeypatch.setattr(litellm, "json_logs", True) monkeypatch.setattr(sys, "excepthook", sys.excepthook)