-
Notifications
You must be signed in to change notification settings - Fork 222
[Feature]: Default CLI log level is DEBUG; WARNING and above go to STDOUT, DEBUG logs to a file #2940
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
Merged
[Feature]: Default CLI log level is DEBUG; WARNING and above go to STDOUT, DEBUG logs to a file #2940
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,103 @@ | ||
| import os | ||
| from datetime import datetime, timedelta, timezone | ||
| from pathlib import Path | ||
| from typing import Generator | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from dstack._internal.cli.utils.common import _get_cli_log_file | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_dstack_dir(tmp_path: Path) -> Generator[Path, None, None]: | ||
| with patch("dstack._internal.cli.utils.common.get_dstack_dir") as mock: | ||
| mock.return_value = tmp_path | ||
| yield tmp_path | ||
|
|
||
|
|
||
| class TestGetCliLogFile: | ||
| def test_no_existing_dir(self, mock_dstack_dir: Path): | ||
| log_dir = mock_dstack_dir / "logs" / "cli" | ||
| expected_log_file = log_dir / "latest.log" | ||
| assert not log_dir.exists() | ||
|
|
||
| result = _get_cli_log_file() | ||
|
|
||
| assert log_dir.exists() | ||
| assert result == expected_log_file | ||
|
|
||
| def test_no_rotation_needed_for_today_file(self, mock_dstack_dir: Path): | ||
| log_dir = mock_dstack_dir / "logs" / "cli" | ||
| log_dir.mkdir(parents=True, exist_ok=True) | ||
| latest_log = log_dir / "latest.log" | ||
| latest_log.touch() | ||
|
|
||
| result = _get_cli_log_file() | ||
|
|
||
| assert result == latest_log | ||
| assert latest_log.exists(), "latest.log should not have been renamed" | ||
|
|
||
| @patch("dstack._internal.cli.utils.common.datetime") | ||
| def test_simple_rotation(self, mock_datetime, mock_dstack_dir: Path): | ||
| # Mock "now" to be a specific date | ||
| now = datetime(2023, 10, 27, 10, 0, 0, tzinfo=timezone.utc) | ||
| mock_datetime.now.return_value = now | ||
| # Ensure fromtimestamp still works correctly for the System Under Test | ||
| mock_datetime.fromtimestamp.side_effect = lambda ts, tz: datetime.fromtimestamp(ts, tz) | ||
|
|
||
| log_dir = mock_dstack_dir / "logs" / "cli" | ||
| log_dir.mkdir(parents=True, exist_ok=True) | ||
| latest_log = log_dir / "latest.log" | ||
| latest_log.touch() | ||
|
|
||
| # Set the modification time to yesterday | ||
| yesterday = now - timedelta(days=1) | ||
| mtime = yesterday.timestamp() | ||
| os.utime(latest_log, (mtime, mtime)) | ||
|
|
||
| # The expected rotated file name is based on the modification time (yesterday) | ||
| date_str = yesterday.strftime("%Y-%m-%d") | ||
| expected_rotated_log = log_dir / f"{date_str}.log" | ||
|
|
||
| result = _get_cli_log_file() | ||
|
|
||
| assert result == log_dir / "latest.log" | ||
| assert not latest_log.exists(), "The original latest.log should have been renamed" | ||
| assert expected_rotated_log.exists(), "The log file should have been rotated" | ||
|
|
||
| @patch("dstack._internal.cli.utils.common.datetime") | ||
| def test_rotation_with_conflict(self, mock_datetime, mock_dstack_dir: Path): | ||
| now = datetime(2023, 10, 27, 10, 0, 0, tzinfo=timezone.utc) | ||
| yesterday = now - timedelta(days=1) | ||
| mock_datetime.now.return_value = now | ||
| mock_datetime.fromtimestamp.side_effect = lambda ts, tz: datetime.fromtimestamp(ts, tz) | ||
|
|
||
| log_dir = mock_dstack_dir / "logs" / "cli" | ||
| log_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Create the old 'latest.log' and set its modification time to yesterday | ||
| latest_log = log_dir / "latest.log" | ||
| latest_log.touch() | ||
| mtime = yesterday.timestamp() | ||
| os.utime(latest_log, (mtime, mtime)) | ||
|
|
||
| # Create conflicting files that already exist from a previous rotation | ||
| date_str = yesterday.strftime("%Y-%m-%d") | ||
| conflicting_log_1 = log_dir / f"{date_str}.log" | ||
| conflicting_log_1.touch() | ||
| conflicting_log_2 = log_dir / f"{date_str}-1.log" | ||
| conflicting_log_2.touch() | ||
|
|
||
| # We expect the file to be rotated to the next available counter | ||
| expected_rotated_log = log_dir / f"{date_str}-2.log" | ||
|
|
||
| result = _get_cli_log_file() | ||
|
|
||
| assert result == log_dir / "latest.log" | ||
| assert not latest_log.exists(), "The original latest.log should have been renamed" | ||
| assert conflicting_log_1.exists(), "Existing rotated log should be untouched" | ||
| assert conflicting_log_2.exists(), "Existing rotated log with counter should be untouched" | ||
| assert expected_rotated_log.exists(), ( | ||
| "The log should have rotated to the next available counter" | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.