59 lines
2.0 KiB
PHP
Executable File
59 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
ini_set('log_errors', 1);
|
|
|
|
$logFile = __DIR__ . '/../logs/error.log';
|
|
|
|
if (!file_exists(dirname($logFile))) {
|
|
mkdir(dirname($logFile), 0755, true);
|
|
}
|
|
|
|
ini_set('error_log', $logFile);
|
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) use ($logFile) {
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$errorType = match($errno) {
|
|
E_ERROR => 'ERROR',
|
|
E_WARNING => 'WARNING',
|
|
E_PARSE => 'PARSE',
|
|
E_NOTICE => 'NOTICE',
|
|
E_CORE_ERROR => 'CORE_ERROR',
|
|
E_CORE_WARNING => 'CORE_WARNING',
|
|
E_COMPILE_ERROR => 'COMPILE_ERROR',
|
|
E_COMPILE_WARNING => 'COMPILE_WARNING',
|
|
E_USER_ERROR => 'USER_ERROR',
|
|
E_USER_WARNING => 'USER_WARNING',
|
|
E_USER_NOTICE => 'USER_NOTICE',
|
|
E_STRICT => 'STRICT',
|
|
E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
|
|
E_DEPRECATED => 'DEPRECATED',
|
|
E_USER_DEPRECATED => 'USER_DEPRECATED',
|
|
default => 'UNKNOWN'
|
|
};
|
|
|
|
$message = "[$timestamp] PHP $errorType: $errstr in $errfile on line $errline";
|
|
error_log($message . PHP_EOL, 3, $logFile);
|
|
|
|
return false;
|
|
});
|
|
|
|
set_exception_handler(function($exception) use ($logFile) {
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$message = "[$timestamp] UNCAUGHT EXCEPTION: " . get_class($exception) . ": " .
|
|
$exception->getMessage() . " in " . $exception->getFile() .
|
|
" on line " . $exception->getLine() . "\n" . $exception->getTraceAsString();
|
|
error_log($message . PHP_EOL, 3, $logFile);
|
|
});
|
|
|
|
register_shutdown_function(function() use ($logFile) {
|
|
$error = error_get_last();
|
|
if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$message = "[$timestamp] FATAL: " . $error['message'] . " in " .
|
|
$error['file'] . " on line " . $error['line'];
|
|
error_log($message . PHP_EOL, 3, $logFile);
|
|
}
|
|
});
|