-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logging.py
More file actions
135 lines (104 loc) · 3.91 KB
/
test_logging.py
File metadata and controls
135 lines (104 loc) · 3.91 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
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Test script to validate logging configuration
Run this to test if structlog is working correctly before running the full application
"""
import os
import sys
# Add app directory to Python path
sys.path.append(os.path.join(os.path.dirname(__file__), "app"))
import structlog
from business_logic import (
simulate_authentication,
simulate_data_analytics,
simulate_file_upload,
simulate_order_processing,
simulate_user_registration,
)
from log_config import setup_logging
def test_logging_setup():
"""Test basic logging configuration"""
print("🧪 Testing logging configuration...")
# Setup logging with file output for testing
setup_logging(log_level="DEBUG", enable_file_logs=True, log_file="logs/test.log")
logger = structlog.get_logger(__name__)
# Test basic logging
logger.info("Testing basic structured logging", test_id="LOG001")
logger.warning("Testing warning level", test_id="LOG002", component="test")
logger.error("Testing error level", test_id="LOG003", error_code="TEST_ERROR")
logger.debug("Testing debug level", test_id="LOG004", details="verbose_info")
print("✅ Basic logging test completed")
def test_business_logic():
"""Test business logic functions"""
print("🧪 Testing business logic simulations...")
logger = structlog.get_logger(__name__)
# Test user registration
user_data = {"username": "test_user", "email": "test@example.com"}
result = simulate_user_registration(user_data)
logger.info("User registration test", result=result)
# Test order processing
order_data = {
"order_id": 12345,
"user_id": 1001,
"amount": 99.99,
"product_id": 501,
"payment_method": "credit_card",
}
result = simulate_order_processing(order_data)
logger.info("Order processing test", result=result)
# Test authentication
result = simulate_authentication("test_user", "password123")
logger.info("Authentication test", result=result)
# Test analytics
result = simulate_data_analytics("simple")
logger.info("Analytics test", result=result)
# Test file upload
result = simulate_file_upload("test_document.pdf", 5.2)
logger.info("File upload test", result=result)
print("✅ Business logic test completed")
def test_structured_data():
"""Test complex structured data logging"""
print("🧪 Testing structured data logging...")
logger = structlog.get_logger(__name__)
# Test with complex data structures
complex_data = {
"user_profile": {
"user_id": 12345,
"preferences": ["email", "sms"],
"settings": {"theme": "dark", "notifications": True},
},
"transaction": {
"amount": 150.75,
"currency": "USD",
"items": [
{"id": 1, "name": "Product A", "qty": 2},
{"id": 2, "name": "Product B", "qty": 1},
],
},
"metadata": {"source": "mobile_app", "version": "2.1.0", "platform": "ios"},
}
logger.info("Complex structured data test", **complex_data)
# Test sensitive data masking
sensitive_data = {
"username": "test_user",
"password": "secret123",
"api_key": "sk_test_123456",
"email": "user@example.com",
"credit_card": "4111-1111-1111-1111",
}
logger.info("Sensitive data masking test", **sensitive_data)
print("✅ Structured data test completed")
if __name__ == "__main__":
print("🚀 Starting logging tests...")
print("=" * 50)
try:
test_logging_setup()
test_business_logic()
test_structured_data()
print("=" * 50)
print("🎉 All tests completed successfully!")
print("📂 Check logs/test.log for output")
print("💡 Now run: docker-compose up --build")
except Exception as e:
print(f"❌ Test failed: {e}")
sys.exit(1)