diff --git a/news/+explicit-loglevel-info.bugfix.md b/news/+explicit-loglevel-info.bugfix.md new file mode 100644 index 00000000000..23b960d2943 --- /dev/null +++ b/news/+explicit-loglevel-info.bugfix.md @@ -0,0 +1 @@ +An explicit `--loglevel info` is no longer ignored when INFO is already the current level. Like any other `--loglevel` value, it now reaches `REFLEX_LOGLEVEL` and overrides a `loglevel` set in `rxconfig.py`. diff --git a/packages/reflex-hosting-cli/news/+explicit-loglevel-info.bugfix.md b/packages/reflex-hosting-cli/news/+explicit-loglevel-info.bugfix.md new file mode 100644 index 00000000000..8c1fd53f6ee --- /dev/null +++ b/packages/reflex-hosting-cli/news/+explicit-loglevel-info.bugfix.md @@ -0,0 +1 @@ +`reflex deploy --loglevel info` now shows the deployment progress and success messages after the export step, as `REFLEX_LOGLEVEL=info reflex deploy` does. The flag was ignored because INFO was already the current level. diff --git a/packages/reflex-hosting-cli/src/reflex_cli/utils/cli_options.py b/packages/reflex-hosting-cli/src/reflex_cli/utils/cli_options.py index 86809013a17..dc738c3106a 100644 --- a/packages/reflex-hosting-cli/src/reflex_cli/utils/cli_options.py +++ b/packages/reflex-hosting-cli/src/reflex_cli/utils/cli_options.py @@ -2,6 +2,7 @@ from __future__ import annotations +import os from typing import TYPE_CHECKING import click @@ -16,6 +17,9 @@ def set_loglevel(ctx: click.Context, self: click.Parameter, value: str | None): """Set the log level. + An explicit level is always exported as ``REFLEX_LOGLEVEL`` so the app's + config picks it up, even when it equals the current level. + Args: ctx: The click context. self: The click command. @@ -23,6 +27,7 @@ def set_loglevel(ctx: click.Context, self: click.Parameter, value: str | None): """ if value is not None: console.set_log_level(value) + os.environ["REFLEX_LOGLEVEL"] = value loglevel_option = click.option( diff --git a/reflex/utils/cli_options.py b/reflex/utils/cli_options.py index c1312a970a2..00bea491d07 100644 --- a/reflex/utils/cli_options.py +++ b/reflex/utils/cli_options.py @@ -2,6 +2,7 @@ from __future__ import annotations +import os from typing import TYPE_CHECKING import click @@ -15,6 +16,9 @@ def set_loglevel(ctx: click.Context, self: click.Parameter, value: str | None): """Set the log level. + An explicit level is always exported as ``REFLEX_LOGLEVEL`` so the config + picks it up, even when it equals the current level. + Args: ctx: The click context. self: The click command. @@ -23,6 +27,7 @@ def set_loglevel(ctx: click.Context, self: click.Parameter, value: str | None): if value is not None: loglevel = constants.LogLevel.from_string(value) console.set_log_level(loglevel) + os.environ["REFLEX_LOGLEVEL"] = value loglevel_option = click.option( diff --git a/tests/units/reflex_cli/utils/test_cli_options.py b/tests/units/reflex_cli/utils/test_cli_options.py new file mode 100644 index 00000000000..7e59ae47ec6 --- /dev/null +++ b/tests/units/reflex_cli/utils/test_cli_options.py @@ -0,0 +1,48 @@ +"""Tests for the shared click options of the hosting CLI.""" + +import os + +import click +import pytest +from click.testing import CliRunner +from reflex_base.config import Config +from reflex_base.constants import LogLevel +from reflex_base.utils import log +from reflex_cli.utils.cli_options import loglevel_option + + +@click.command() +@loglevel_option +def _loglevel_command(): + """Carry only the --loglevel option.""" + + +@pytest.mark.parametrize( + "rxconfig_loglevel", + [LogLevel.DEFAULT, LogLevel.WARNING], + ids=["rxconfig_default", "rxconfig_warning"], +) +def test_explicit_loglevel_is_honoured_when_level_is_unchanged( + monkeypatch: pytest.MonkeyPatch, rxconfig_loglevel: LogLevel +): + """An explicit --loglevel info reaches the config even when INFO is already the level. + + `reflex deploy` takes its --loglevel from this option, and the export step + reads the level back from the config. + + Args: + monkeypatch: The pytest monkeypatch fixture. + rxconfig_loglevel: The loglevel the app's rxconfig.py sets. + """ + # Set before deleting so teardown also removes the value the CLI writes. + monkeypatch.setenv("REFLEX_LOGLEVEL", "") + monkeypatch.delenv("REFLEX_LOGLEVEL") + monkeypatch.setattr(log, "_log_level", LogLevel.INFO) + + result = CliRunner().invoke(_loglevel_command, ["--loglevel", "info"]) + + assert result.exit_code == 0 + env_loglevel = os.environ.get("REFLEX_LOGLEVEL") + assert env_loglevel == "info" + assert Config(app_name="demo", loglevel=rxconfig_loglevel).loglevel is LogLevel.INFO + assert log.get_log_level() is LogLevel.INFO diff --git a/tests/units/utils/test_cli_options.py b/tests/units/utils/test_cli_options.py new file mode 100644 index 00000000000..4e26e188471 --- /dev/null +++ b/tests/units/utils/test_cli_options.py @@ -0,0 +1,46 @@ +"""Tests for the shared click options of the reflex CLI.""" + +import os + +import click +import pytest +from click.testing import CliRunner +from reflex_base.config import Config +from reflex_base.constants import LogLevel +from reflex_base.utils import log + +from reflex.utils.cli_options import loglevel_option + + +@click.command() +@loglevel_option +def _loglevel_command(): + """Carry only the --loglevel option.""" + + +@pytest.mark.parametrize( + "rxconfig_loglevel", + [LogLevel.DEFAULT, LogLevel.WARNING], + ids=["rxconfig_default", "rxconfig_warning"], +) +def test_explicit_loglevel_is_honoured_when_level_is_unchanged( + monkeypatch: pytest.MonkeyPatch, rxconfig_loglevel: LogLevel +): + """An explicit --loglevel info reaches the config even when INFO is already the level. + + Args: + monkeypatch: The pytest monkeypatch fixture. + rxconfig_loglevel: The loglevel the app's rxconfig.py sets. + """ + # Set before deleting so teardown also removes the value the CLI writes. + monkeypatch.setenv("REFLEX_LOGLEVEL", "") + monkeypatch.delenv("REFLEX_LOGLEVEL") + monkeypatch.setattr(log, "_log_level", LogLevel.INFO) + + result = CliRunner().invoke(_loglevel_command, ["--loglevel", "info"]) + + assert result.exit_code == 0 + env_loglevel = os.environ.get("REFLEX_LOGLEVEL") + assert env_loglevel == "info" + assert Config(app_name="demo", loglevel=rxconfig_loglevel).loglevel is LogLevel.INFO + assert log.get_log_level() is LogLevel.INFO