-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logging.py
More file actions
134 lines (116 loc) · 4.31 KB
/
Copy pathtest_logging.py
File metadata and controls
134 lines (116 loc) · 4.31 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
#!/usr/bin/env python3
"""
Quick test to verify structured logging is working.
"""
import os
import sys
import json
from pathlib import Path
# Add solution directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from utils import log_decision, log_tool_usage, log_routing, log_escalation, search_logs
def test_logging():
"""Test that all logging functions work correctly."""
print("🧪 Testing Structured Logging System")
print("=" * 70)
test_ticket_id = "test-logging-001"
# Test 1: Log a decision
print("\n1. Testing log_decision()...")
log_decision(
ticket_id=test_ticket_id,
agent="test_agent",
action="test_invoked",
input_data={"test_input": "value"},
output_data={"test_output": "result"},
confidence=0.95,
duration_ms=123.45,
metadata={"test": True}
)
print(" ✅ Decision logged")
# Test 2: Log a tool usage
print("\n2. Testing log_tool_usage()...")
log_tool_usage(
ticket_id=test_ticket_id,
agent="test_agent",
tool_name="test_tool",
parameters={"param1": "value1"},
result={"result_key": "result_value"},
success=True,
duration_ms=56.78
)
print(" ✅ Tool usage logged")
# Test 3: Log a routing decision
print("\n3. Testing log_routing()...")
log_routing(
ticket_id=test_ticket_id,
from_agent="supervisor",
to_agent="test_agent",
reasoning="This is a test routing decision",
classification={"ticket_type": "test", "urgency": "low"},
confidence=0.88
)
print(" ✅ Routing logged")
# Test 4: Log an escalation
print("\n4. Testing log_escalation()...")
log_escalation(
ticket_id=test_ticket_id,
reason="Test escalation",
summary="This is a test escalation summary",
classification={"ticket_type": "test", "urgency": "high"},
attempted_actions=["action1", "action2"],
confidence=0.35
)
print(" ✅ Escalation logged")
# Test 5: Verify log files exist
print("\n5. Verifying log files exist...")
logs_dir = Path("logs")
if logs_dir.exists():
log_files = list(logs_dir.glob("*.jsonl"))
print(f" ✅ Found {len(log_files)} log files:")
for log_file in sorted(log_files):
size = log_file.stat().st_size
print(f" - {log_file.name} ({size} bytes)")
else:
print(" ❌ Logs directory not found")
return False
# Test 6: Search logs
print("\n6. Testing log search...")
decisions = search_logs("decisions", ticket_id=test_ticket_id)
tools = search_logs("tools", ticket_id=test_ticket_id)
routing = search_logs("routing", ticket_id=test_ticket_id)
escalations = search_logs("escalations", ticket_id=test_ticket_id)
print(f" ✅ Found logs for test ticket:")
print(f" - Decisions: {len(decisions)}")
print(f" - Tools: {len(tools)}")
print(f" - Routing: {len(routing)}")
print(f" - Escalations: {len(escalations)}")
# Test 7: Verify log structure
print("\n7. Verifying log structure...")
if decisions:
sample = decisions[0]
required_fields = ["timestamp", "ticket_id", "agent", "action"]
missing = [f for f in required_fields if f not in sample]
if missing:
print(f" ❌ Missing fields: {missing}")
return False
print(f" ✅ Log structure valid")
print(f" Sample: {json.dumps(sample, indent=8)[:200]}...")
print("\n" + "=" * 70)
print("✅ All logging tests passed!")
print("\nStructured logging is fully operational:")
print(" - All log functions work correctly")
print(" - Logs are written to persistent JSONL files")
print(" - Logs are searchable via Python API")
print(" - Log structure is valid and complete")
print("\n📖 See LOGGING_GUIDE.md for usage examples")
print("📖 See LOGGING_IMPLEMENTATION.md for implementation details")
return True
if __name__ == "__main__":
try:
success = test_logging()
sys.exit(0 if success else 1)
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)