-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: support log level via ServerOptions and LIVEKIT_LOG_LEVEL env var #5112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
theomonnom
merged 6 commits into
livekit:main
from
onurburak9:feat/log-level-worker-options
Mar 18, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6cf760
feat: support log level via ServerOptions and LIVEKIT_LOG_LEVEL env var
onur-yildirim-infinitusai a2ec948
fix: extract log_level default to module-level singleton to satisfy B…
onur-yildirim-infinitusai 498eada
style: apply ruff formatting
onur-yildirim-infinitusai 26887d1
fix: validate log level at construction time, reject invalid values
onur-yildirim-infinitusai 7a3a083
fix: normalize lowercase log levels to uppercase at construction time
onur-yildirim-infinitusai bbdeb23
Added log_level to console command
onur-yildirim-infinitusai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from typer.testing import CliRunner | ||
|
|
||
| from livekit.agents.cli.cli import _build_cli | ||
| from livekit.agents.worker import AgentServer, ServerEnvOption, ServerOptions | ||
|
|
||
|
|
||
| def _make_server(**kwargs) -> AgentServer: | ||
| async def _fake_entrypoint(ctx): | ||
| pass | ||
|
|
||
| opts = ServerOptions(entrypoint_fnc=_fake_entrypoint, **kwargs) | ||
| return AgentServer.from_server_options(opts) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def runner(): | ||
| return CliRunner() | ||
|
|
||
|
|
||
| class TestServerOptionsLogLevel: | ||
| def test_default_uses_server_env_option(self): | ||
| opts = ServerOptions(entrypoint_fnc=lambda ctx: None) | ||
| assert isinstance(opts.log_level, ServerEnvOption) | ||
| assert ServerEnvOption.getvalue(opts.log_level, False) == "INFO" | ||
| assert ServerEnvOption.getvalue(opts.log_level, True) == "DEBUG" | ||
|
|
||
| def test_custom_string_log_level(self): | ||
| opts = ServerOptions(entrypoint_fnc=lambda ctx: None, log_level="WARN") | ||
| assert opts.log_level == "WARN" | ||
| assert ServerEnvOption.getvalue(opts.log_level, False) == "WARN" | ||
| assert ServerEnvOption.getvalue(opts.log_level, True) == "WARN" | ||
|
|
||
| def test_custom_server_env_option(self): | ||
| opts = ServerOptions( | ||
| entrypoint_fnc=lambda ctx: None, | ||
| log_level=ServerEnvOption(dev_default="ERROR", prod_default="CRITICAL"), | ||
| ) | ||
| assert ServerEnvOption.getvalue(opts.log_level, False) == "CRITICAL" | ||
| assert ServerEnvOption.getvalue(opts.log_level, True) == "ERROR" | ||
|
|
||
|
|
||
| class TestAgentServerLogLevel: | ||
| def test_default_log_level(self): | ||
| server = AgentServer() | ||
| assert isinstance(server.log_level, ServerEnvOption) | ||
| assert ServerEnvOption.getvalue(server.log_level, False) == "INFO" | ||
| assert ServerEnvOption.getvalue(server.log_level, True) == "DEBUG" | ||
|
|
||
| def test_custom_log_level(self): | ||
| server = AgentServer(log_level="WARN") | ||
| assert server.log_level == "WARN" | ||
|
|
||
| def test_from_server_options_passes_log_level(self): | ||
| server = _make_server(log_level="ERROR") | ||
| assert server.log_level == "ERROR" | ||
|
|
||
|
|
||
| class TestStartCommandLogLevel: | ||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_default_log_level_is_info(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start"]) | ||
| assert result.exit_code == 0 | ||
| args = mock_run_worker.call_args | ||
| assert args.kwargs["args"].log_level == "INFO" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_cli_arg_overrides_default(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start", "--log-level", "error"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "ERROR" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_env_var_overrides_default(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start"], env={"LIVEKIT_LOG_LEVEL": "CRITICAL"}) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "CRITICAL" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_cli_arg_overrides_env_var(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke( | ||
| app, | ||
| ["start", "--log-level", "warn"], | ||
| env={"LIVEKIT_LOG_LEVEL": "CRITICAL"}, | ||
| ) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "WARN" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_server_opts_log_level_used_when_no_cli_or_env(self, mock_run_worker, runner): | ||
| server = _make_server(log_level="ERROR") | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "ERROR" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_cli_arg_overrides_server_opts(self, mock_run_worker, runner): | ||
| server = _make_server(log_level="ERROR") | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start", "--log-level", "debug"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "DEBUG" | ||
|
|
||
|
|
||
| class TestDevCommandLogLevel: | ||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_default_log_level_is_debug(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["dev", "--no-reload"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "DEBUG" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_cli_arg_overrides_default(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["dev", "--no-reload", "--log-level", "info"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "INFO" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_env_var_overrides_default(self, mock_run_worker, runner): | ||
| server = _make_server() | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["dev", "--no-reload"], env={"LIVEKIT_LOG_LEVEL": "ERROR"}) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "ERROR" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_server_opts_log_level_used_when_no_cli_or_env(self, mock_run_worker, runner): | ||
| server = _make_server(log_level="CRITICAL") | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["dev", "--no-reload"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "CRITICAL" | ||
|
|
||
|
|
||
| class TestLogLevelValidation: | ||
| def test_invalid_string_rejected_by_server_options(self): | ||
| with pytest.raises(ValueError, match="Invalid log level"): | ||
| ServerOptions(entrypoint_fnc=lambda ctx: None, log_level="WARNING") | ||
|
|
||
| def test_invalid_string_rejected_by_agent_server(self): | ||
| with pytest.raises(ValueError, match="Invalid log level"): | ||
| AgentServer(log_level="NOTSET") | ||
|
|
||
| def test_invalid_server_env_option_rejected(self): | ||
| with pytest.raises(ValueError, match="Invalid log level"): | ||
| ServerOptions( | ||
| entrypoint_fnc=lambda ctx: None, | ||
| log_level=ServerEnvOption(dev_default="WARNING", prod_default="INFO"), | ||
| ) | ||
|
|
||
| def test_valid_levels_accepted_by_server_options(self): | ||
| for level in ("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"): | ||
| opts = ServerOptions(entrypoint_fnc=lambda ctx: None, log_level=level) | ||
| assert opts.log_level == level | ||
|
|
||
| def test_valid_levels_accepted_by_agent_server(self): | ||
| for level in ("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"): | ||
| server = AgentServer(log_level=level) | ||
| assert server.log_level == level | ||
|
|
||
| def test_lowercase_normalized_to_uppercase_server_options(self): | ||
| opts = ServerOptions(entrypoint_fnc=lambda ctx: None, log_level="info") | ||
| assert opts.log_level == "INFO" | ||
|
|
||
| def test_lowercase_normalized_to_uppercase_agent_server(self): | ||
| server = AgentServer(log_level="debug") | ||
| assert server.log_level == "DEBUG" | ||
|
|
||
| def test_lowercase_server_env_option_normalized(self): | ||
| opts = ServerOptions( | ||
| entrypoint_fnc=lambda ctx: None, | ||
| log_level=ServerEnvOption(dev_default="debug", prod_default="error"), | ||
| ) | ||
| assert ServerEnvOption.getvalue(opts.log_level, True) == "DEBUG" | ||
| assert ServerEnvOption.getvalue(opts.log_level, False) == "ERROR" | ||
|
|
||
| @patch("livekit.agents.cli.cli._run_worker") | ||
| def test_lowercase_server_opts_works_through_cli(self, mock_run_worker, runner): | ||
| server = _make_server(log_level="error") | ||
| app = _build_cli(server) | ||
| result = runner.invoke(app, ["start"]) | ||
| assert result.exit_code == 0 | ||
| assert mock_run_worker.call_args.kwargs["args"].log_level == "ERROR" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add
log_levelfor console mode as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ @longcw