How can a custom error handler and exception handler be implemented in PHP for error logging?

To implement a custom error handler and exception handler in PHP for error logging, you can define your own functions to handle errors and exceptions. The custom error handler function should accept parameters for error level, error message, file name, and line number, while the custom exception handler function should accept the exception object. Inside these functions, you can log the errors or exceptions to a file or database for further analysis.

// Custom error handler function
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    $logMessage = "Error: [$errno] $errstr in $errfile on line $errline";
    error_log($logMessage, 3, "error.log");
}

// Custom exception handler function
function customExceptionHandler($exception) {
    $logMessage = "Exception: " . $exception->getMessage() . " in " . $exception->getFile() . " on line " . $exception->getLine();
    error_log($logMessage, 3, "error.log");
}

// Set custom error and exception handlers
set_error_handler("customErrorHandler");
set_exception_handler("customExceptionHandler");