-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-logging-debugging.php
More file actions
136 lines (109 loc) · 3.64 KB
/
Copy path04-logging-debugging.php
File metadata and controls
136 lines (109 loc) · 3.64 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
136
<?php
declare(strict_types=1);
/**
* Tutorial 4: Logging and Debugging
*
* Learn how to configure PSR-3 logging, monitor error rates,
* and debug error patterns effectively.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use ClaudeAgents\Services\ErrorHandling\ErrorHandlingService;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
echo "Tutorial 4: Logging and Debugging\n";
echo str_repeat('=', 70) . "\n\n";
// Step 1: Setup PSR-3 Logging
echo "Step 1: Setting up PSR-3 Logger...\n";
echo str_repeat('-', 70) . "\n";
$logger = new Logger('error_handling');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$service = new ErrorHandlingService(
logger: $logger,
maxRetries: 2,
initialDelayMs: 100
);
$service->initialize();
echo "✓ Logger configured with Monolog\n\n";
// Step 2: Log errors with context
echo "Step 2: Logging Errors with Rich Context...\n";
echo str_repeat('-', 70) . "\n";
try {
throw new \RuntimeException('Test error for logging');
} catch (\Throwable $e) {
$details = $service->getErrorDetails($e);
// Log with additional context
$logger->error('Operation failed', array_merge($details, [
'user_id' => 'user_123',
'request_id' => uniqid(),
'environment' => 'development',
]));
}
echo "\n";
// Step 3: Error rate monitoring
echo "Step 3: Monitoring Error Rates...\n";
echo str_repeat('-', 70) . "\n";
class ErrorMonitor
{
private array $errors = [];
private int $totalRequests = 0;
public function trackRequest(callable $fn, $errorService): void
{
$this->totalRequests++;
try {
$fn();
} catch (\Throwable $e) {
$errorType = basename(str_replace('\\', '/', get_class($e)));
if (!isset($this->errors[$errorType])) {
$this->errors[$errorType] = 0;
}
$this->errors[$errorType]++;
// Log for monitoring
$details = $errorService->getErrorDetails($e);
error_log(json_encode([
'error_type' => $errorType,
'message' => $details['user_friendly_message'],
'timestamp' => time(),
]));
}
}
public function getStats(): array
{
$totalErrors = array_sum($this->errors);
$errorRate = $this->totalRequests > 0
? ($totalErrors / $this->totalRequests) * 100
: 0;
return [
'total_requests' => $this->totalRequests,
'total_errors' => $totalErrors,
'error_rate_percent' => round($errorRate, 2),
'errors_by_type' => $this->errors,
];
}
}
$monitor = new ErrorMonitor();
// Simulate requests
for ($i = 0; $i < 5; $i++) {
$monitor->trackRequest(function () use ($i) {
if ($i % 2 === 0) {
throw new \RuntimeException("Error {$i}");
}
return "Success {$i}";
}, $service);
}
$stats = $monitor->getStats();
echo "Total Requests: {$stats['total_requests']}\n";
echo "Total Errors: {$stats['total_errors']}\n";
echo "Error Rate: {$stats['error_rate_percent']}%\n";
if (!empty($stats['errors_by_type'])) {
echo "Errors by Type:\n";
foreach ($stats['errors_by_type'] as $type => $count) {
echo " - {$type}: {$count}\n";
}
}
echo "\n" . str_repeat('=', 70) . "\n";
echo "Tutorial 4 Complete!\n\n";
echo "Key Takeaways:\n";
echo "• Configure PSR-3 logger with Monolog or other implementations\n";
echo "• Use getErrorDetails() for structured logging\n";
echo "• Track error rates and types for monitoring\n";
echo "• Add custom context to error logs\n";