PHP code example of wubinworks / error-handler

1. Go to this page and download the library: Download wubinworks/error-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wubinworks / error-handler example snippets


// Set the error reporting level you prefer
$previousErrorReportingLevel = error_reporting(/** This is just an example level */ E_ALL & ~E_WARNING);

set_error_handler([new \Wubinworks\ErrorHandler\ErrorHandler(), 'handle']);
try {
    // Code like `fopen` that can trigger a traditional runtime error
    // Here we use `trigger_error` for convenience
    trigger_error('The error message.', E_USER_NOTICE);
} catch (\ErrorException $e) {
    var_dump($e->getMessage());
    // The error message.
    var_dump($e->getSeverity());
    // Value of predefined constant E_USER_NOTICE(1024)
}

// Error control operator
@trigger_error('You will not see this message. No error, no exception.', E_USER_NOTICE);

restore_error_handler();
error_reporting($previousErrorReportingLevel);

try {
    ...
    if (...) {
        throw new \Exception('The exception message.');
    }
    ...
} catch (\Throwable $e) {
    $logger = new \Monolog\Logger('example-logger');
    $logger->pushHandler(new \Monolog\Handler\StreamHandler('<path/to/your.log>'));
    $exceptionHandler = new \Wubinworks\ErrorHandler\ExceptionHandler(
        false, // No output
        $logger, // PSR-3 Logger Interface 
        false // Stack trace does not 

// Replace the PHP's built-in exception handler
set_exception_handler([new \Wubinworks\ErrorHandler\ExceptionHandler(true, null, false), 'handle']);