|
| 1 | +import pytest |
| 2 | + |
| 3 | +from guidellm import LoggerConfig, configure_logger, logger |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def reset_logger(): |
| 8 | + # Ensure logger is reset before each test |
| 9 | + logger.remove() |
| 10 | + yield |
| 11 | + logger.remove() |
| 12 | + |
| 13 | + |
| 14 | +def test_default_logger_settings(capsys): |
| 15 | + configure_logger() |
| 16 | + |
| 17 | + # Default settings should log to console with INFO level and no file logging |
| 18 | + logger.info("Info message") |
| 19 | + logger.debug("Debug message") |
| 20 | + |
| 21 | + captured = capsys.readouterr() |
| 22 | + assert captured.out.count("Info message") == 1 |
| 23 | + assert "Debug message" not in captured.out |
| 24 | + |
| 25 | + |
| 26 | +def test_configure_logger_console_settings(capsys): |
| 27 | + # Test configuring the logger to change console log level |
| 28 | + config = LoggerConfig(console_log_level="DEBUG") |
| 29 | + configure_logger(config=config) |
| 30 | + logger.info("Info message") |
| 31 | + logger.debug("Debug message") |
| 32 | + |
| 33 | + captured = capsys.readouterr() |
| 34 | + assert captured.out.count("Info message") == 1 |
| 35 | + assert captured.out.count("Debug message") == 1 |
| 36 | + |
| 37 | + |
| 38 | +def test_configure_logger_file_settings(tmp_path): |
| 39 | + # Test configuring the logger to log to a file |
| 40 | + log_file = tmp_path / "test.log" |
| 41 | + config = LoggerConfig(log_file=str(log_file), log_file_level="DEBUG") |
| 42 | + configure_logger(config=config) |
| 43 | + logger.info("Info message") |
| 44 | + logger.debug("Debug message") |
| 45 | + |
| 46 | + with open(log_file, "r") as f: |
| 47 | + log_contents = f.read() |
| 48 | + assert log_contents.count('"message": "Info message"') == 1 |
| 49 | + assert log_contents.count('"message": "Debug message"') == 1 |
| 50 | + |
| 51 | + |
| 52 | +def test_configure_logger_console_and_file(capsys, tmp_path): |
| 53 | + # Test configuring the logger to change both console and file settings |
| 54 | + log_file = tmp_path / "test.log" |
| 55 | + config = LoggerConfig( |
| 56 | + console_log_level="ERROR", log_file=str(log_file), log_file_level="INFO" |
| 57 | + ) |
| 58 | + configure_logger(config=config) |
| 59 | + logger.info("Info message") |
| 60 | + logger.error("Error message") |
| 61 | + |
| 62 | + captured = capsys.readouterr() |
| 63 | + assert "Info message" not in captured.out |
| 64 | + assert captured.out.count("Error message") == 1 |
| 65 | + |
| 66 | + with open(log_file, "r") as f: |
| 67 | + log_contents = f.read() |
| 68 | + assert log_contents.count('"message": "Info message"') == 1 |
| 69 | + assert log_contents.count('"message": "Error message"') == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_environment_variable_override(monkeypatch, capsys, tmp_path): |
| 73 | + # Test environment variables override settings |
| 74 | + monkeypatch.setenv("GUIDELLM_LOG_LEVEL", "ERROR") |
| 75 | + monkeypatch.setenv("GUIDELLM_LOG_FILE", str(tmp_path / "env_test.log")) |
| 76 | + monkeypatch.setenv("GUIDELLM_LOG_FILE_LEVEL", "DEBUG") |
| 77 | + |
| 78 | + configure_logger(config=LoggerConfig()) |
| 79 | + logger.info("Info message") |
| 80 | + logger.error("Error message") |
| 81 | + logger.debug("Debug message") |
| 82 | + |
| 83 | + captured = capsys.readouterr() |
| 84 | + assert "Info message" not in captured.out |
| 85 | + assert captured.out.count("Error message") == 1 |
| 86 | + assert "Debug message" not in captured.out |
| 87 | + |
| 88 | + with open(tmp_path / "env_test.log", "r") as f: |
| 89 | + log_contents = f.read() |
| 90 | + assert log_contents.count('"message": "Error message"') == 1 |
| 91 | + assert log_contents.count('"message": "Info message"') == 1 |
| 92 | + assert log_contents.count('"message": "Debug message"') == 1 |
| 93 | + |
| 94 | + |
| 95 | +def test_environment_variable_disable_logging(monkeypatch, capsys): |
| 96 | + # Test environment variable to disable logging |
| 97 | + monkeypatch.setenv("GUIDELLM_LOG_DISABLED", "true") |
| 98 | + |
| 99 | + configure_logger(config=LoggerConfig()) |
| 100 | + logger.info("Info message") |
| 101 | + logger.error("Error message") |
| 102 | + |
| 103 | + captured = capsys.readouterr() |
| 104 | + assert captured.out == "" |
| 105 | + assert captured.err == "" |
0 commit comments