-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_logging_config.py
More file actions
125 lines (99 loc) · 4.17 KB
/
test_logging_config.py
File metadata and controls
125 lines (99 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Test that logging level configuration is properly applied.
"""
import os
import sys
import logging
from io import StringIO
def test_logging_level_configuration():
"""Test that LOG_LEVEL environment variable configures logging correctly."""
print("\n" + "="*70)
print("Testing Logging Level Configuration")
print("="*70)
# Test 1: Default INFO level
print("\nTest 1: Default INFO level")
os.environ["SEARCHAPI_API_KEY"] = "test_key_123"
if "LOG_LEVEL" in os.environ:
del os.environ["LOG_LEVEL"]
# Capture log output
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.DEBUG)
from config import load_config
config = load_config()
# Create logger and apply config level
test_logger = logging.getLogger("test_default")
test_logger.handlers.clear()
test_logger.addHandler(handler)
test_logger.setLevel(getattr(logging, config.log_level))
# Default should be INFO
assert config.log_level == "INFO", f"Expected INFO, got {config.log_level}"
# INFO and higher should be logged
test_logger.debug("This should NOT appear")
test_logger.info("This SHOULD appear")
test_logger.warning("This SHOULD appear")
output = log_capture.getvalue()
assert "This SHOULD appear" in output, "INFO messages should be logged"
assert "This should NOT appear" not in output, "DEBUG messages should not be logged"
print("✓ Default INFO level works correctly")
# Test 2: DEBUG level
print("\nTest 2: DEBUG level from environment")
os.environ["LOG_LEVEL"] = "DEBUG"
# Need to reload config module to pick up new env var
import importlib
import config as config_module
importlib.reload(config_module)
from config import load_config
config = load_config()
assert config.log_level == "DEBUG", f"Expected DEBUG, got {config.log_level}"
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.DEBUG)
test_logger = logging.getLogger("test_debug")
test_logger.handlers.clear()
test_logger.addHandler(handler)
test_logger.setLevel(getattr(logging, config.log_level))
# DEBUG and higher should all be logged
test_logger.debug("DEBUG message")
test_logger.info("INFO message")
output = log_capture.getvalue()
assert "DEBUG message" in output, "DEBUG messages should be logged when level=DEBUG"
assert "INFO message" in output, "INFO messages should be logged when level=DEBUG"
print("✓ DEBUG level from environment works correctly")
# Test 3: ERROR level
print("\nTest 3: ERROR level from environment")
os.environ["LOG_LEVEL"] = "ERROR"
importlib.reload(config_module)
from config import load_config
config = load_config()
assert config.log_level == "ERROR", f"Expected ERROR, got {config.log_level}"
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.DEBUG)
test_logger = logging.getLogger("test_error")
test_logger.handlers.clear()
test_logger.addHandler(handler)
test_logger.setLevel(getattr(logging, config.log_level))
# Only ERROR and higher should be logged
test_logger.debug("DEBUG should NOT appear")
test_logger.info("INFO should NOT appear")
test_logger.warning("WARNING should NOT appear")
test_logger.error("ERROR SHOULD appear")
output = log_capture.getvalue()
assert "ERROR SHOULD appear" in output, "ERROR messages should be logged when level=ERROR"
assert "DEBUG should NOT appear" not in output, "DEBUG should be filtered at ERROR level"
assert "INFO should NOT appear" not in output, "INFO should be filtered at ERROR level"
assert "WARNING should NOT appear" not in output, "WARNING should be filtered at ERROR level"
print("✓ ERROR level from environment works correctly")
print("\n" + "="*70)
print("All logging configuration tests passed!")
print("="*70)
if __name__ == "__main__":
try:
test_logging_level_configuration()
sys.exit(0)
except Exception as e:
print(f"\n✗ Test failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)