|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace App; |
| 4 | + |
3 | 5 | // Turn on all errors and log them (no output on screen) |
4 | 6 | error_reporting(E_ALL); |
5 | 7 | ini_set('display_errors', '0'); |
6 | 8 | ini_set('log_errors', '1'); |
7 | | -ini_set('error_log', __DIR__ . '/logs/error_log.txt'); |
| 9 | +ini_set('error_log', __DIR__.'/logs/error_log.txt'); |
8 | 10 |
|
9 | 11 | // Ensure the logs directory exists |
10 | | -if (!is_dir(__DIR__ . '/logs')) { |
11 | | - mkdir(__DIR__ . '/logs', 0777, true); |
| 12 | +if (! is_dir(__DIR__.'/logs')) { |
| 13 | + mkdir(__DIR__.'/logs', 0777, true); |
12 | 14 | } |
13 | 15 |
|
14 | 16 | // Custom error handler |
15 | 17 | function errorHandler(string $errNo, string $errStr, string $errFile, string $errLine): mixed |
16 | 18 | { |
17 | 19 | $errorTypes = [ |
18 | | - E_ERROR => 'Error', |
19 | | - E_WARNING => 'Warning', |
20 | | - E_PARSE => 'Parse Error', |
21 | | - E_NOTICE => 'Notice', |
22 | | - E_CORE_ERROR => 'Core Error', |
23 | | - E_CORE_WARNING => 'Core Warning', |
24 | | - E_COMPILE_ERROR => 'Compile Error', |
25 | | - E_COMPILE_WARNING => 'Compile Warning', |
26 | | - E_USER_ERROR => 'User Error', |
27 | | - E_USER_WARNING => 'User Warning', |
28 | | - E_USER_NOTICE => 'User Notice', |
29 | | - E_STRICT => 'Strict', |
| 20 | + E_ERROR => 'Error', |
| 21 | + E_WARNING => 'Warning', |
| 22 | + E_PARSE => 'Parse Error', |
| 23 | + E_NOTICE => 'Notice', |
| 24 | + E_CORE_ERROR => 'Core Error', |
| 25 | + E_CORE_WARNING => 'Core Warning', |
| 26 | + E_COMPILE_ERROR => 'Compile Error', |
| 27 | + E_COMPILE_WARNING => 'Compile Warning', |
| 28 | + E_USER_ERROR => 'User Error', |
| 29 | + E_USER_WARNING => 'User Warning', |
| 30 | + E_USER_NOTICE => 'User Notice', |
| 31 | + E_STRICT => 'Strict', |
30 | 32 | E_RECOVERABLE_ERROR => 'Recoverable Error', |
31 | | - E_DEPRECATED => 'Deprecated', |
32 | | - E_USER_DEPRECATED => 'User Deprecated', |
| 33 | + E_DEPRECATED => 'Deprecated', |
| 34 | + E_USER_DEPRECATED => 'User Deprecated', |
33 | 35 | ]; |
34 | 36 |
|
35 | 37 | $type = $errorTypes[$errNo] ?? 'Unknown Error'; |
36 | 38 | $timestamp = date('Y-m-d H:i:s'); |
37 | | - $message = "[$timestamp] $type: $errStr in $errFile on line $errLine" . PHP_EOL; |
| 39 | + $message = "[$timestamp] $type: $errStr in $errFile on line $errLine".PHP_EOL; |
| 40 | + |
| 41 | + error_log($message, 3, __DIR__.'/logs/error_log.txt'); |
38 | 42 |
|
39 | | - error_log($message, 3, __DIR__ . '/logs/error_log.txt'); |
40 | 43 | return true; // Prevent PHP internal handler |
41 | 44 | } |
42 | 45 |
|
43 | 46 | // Custom exception handler |
44 | 47 | function exceptionHandler(Throwable $exception): void |
45 | 48 | { |
46 | 49 | $timestamp = date('Y-m-d H:i:s'); |
47 | | - $message = "[$timestamp] Uncaught Exception: " . $exception->getMessage() . |
48 | | - " in " . $exception->getFile() . |
49 | | - " on line " . $exception->getLine() . PHP_EOL . |
50 | | - $exception->getTraceAsString() . PHP_EOL; |
| 50 | + $message = "[$timestamp] Uncaught Exception: ".$exception->getMessage(). |
| 51 | + ' in '.$exception->getFile(). |
| 52 | + ' on line '.$exception->getLine().PHP_EOL. |
| 53 | + $exception->getTraceAsString().PHP_EOL; |
51 | 54 |
|
52 | | - error_log($message, 3, __DIR__ . '/logs/error_log.txt'); |
| 55 | + error_log($message, 3, __DIR__.'/logs/error_log.txt'); |
53 | 56 | } |
54 | 57 |
|
55 | 58 | set_error_handler('errorHandler'); |
|
0 commit comments