PHP code example of zkwbbr / whoops-helper

1. Go to this page and download the library: Download zkwbbr/whoops-helper 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/ */

    

zkwbbr / whoops-helper example snippets




\error_reporting(E_ALL);

use Zkwbbr\WhoopsHelper;

// the following constants are arbitrary and not 
define('APP_LOG_DIR', '/path/to/logs/');
define('APP_LOG_TIME_ZONE', 'UTC');
define('APP_URL', 'example.com');
define('APP_SMTP_HOST', 'example.com');
define('APP_SMTP_USER', 'user');
define('APP_SMTP_PASS', 'pass');
define('APP_SMTP_PORT', '25');
define('APP_SMTP_ENCR', 'TLS');
define('APP_PUSHOVER_APP_KEY', 'example');
define('APP_PUSHOVER_USER_KEY', 'example');

$whoops = new \Whoops\Run;

if (APP_DEV_MODE) {

    \ini_set('display_errors', '1');

    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);

} else {

    ini_set('display_errors', '0');

    $whoops->pushHandler(function ($ex) {

        $logger = new WhoopsHelper\Logger\Filesystem\FileSytemLogger(APP_LOG_DIR, APP_LOG_TIME_ZONE);

        $handler = new WhoopsHelper\Handler($ex, $logger, APP_LOG_TIME_ZONE);

        // optionally remove sensitive info from $_SERVER var in the log
        $sampleSensitiveInfo = ['PHP_AUTH_PW'];
        $handler->setItemsToRemoveFromServerVar($sampleSensitiveInfo);

        $handler->process();

        // ----------------------------------------------
        // optionally send email on first instance of an error
        // ----------------------------------------------

        $smtpServers = [
            0 => (new \MetaRush\EmailFallback\Server)
                ->setHost(APP_SMTP_HOST)
                ->setUser(APP_SMTP_USER)
                ->setPass(APP_SMTP_PASS)
                ->setPort(APP_SMTP_PORT)
                ->setEncr(APP_SMTP_ENCR)
        ];

        $mailBuilder = (new \MetaRush\EmailFallback\Builder)
            ->setServers($smtpServers)
            ->setAdminEmails([APP_ADMIN_EMAIL])
            ->setNotificationFromEmail('[email protected]')
            ->setFromEmail('[email protected]')
            ->setAppName(APP_URL)
            ->setTos([APP_ADMIN_EMAIL]);

        $action = new WhoopsHelper\Actions\Email\Action(APP_URL, $mailBuilder);

        $handler->invokeActionOnEvent(
            WhoopsHelper\Handler::LOGGED_EVENT,
            $action
        );

        // ----------------------------------------------
        // optionally send Pushover notification on first instance of an error
        // ----------------------------------------------

        $action = new WhoopsHelper\Actions\Pushover\Action(
            APP_URL, APP_PUSHOVER_APP_KEY, APP_PUSHOVER_USER_KEY
        );

        $handler->invokeActionOnEvent(
            WhoopsHelper\Handler::LOGGED_EVENT,
            $action
        );

        // ----------------------------------------------
        // send response to user/client
        // ----------------------------------------------

        \header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
        \header('Status: 500 Internal Server Error');

        echo 'Sorry an error occurred, our admins have been notified';
    });
}

$whoops->register();


$dataMapper = (new \MetaRush\DataMapper\DataMapper)
    ->setDsn('mysql:host=locolhost;dbname=you_db_name')
    ->setDbUser('your_db_user')
    ->setDbPass('your_db_pass')
    ->build();

$logger = new WhoopsHelper\Logger\Pdo\PdoLogger($dataMapper, 'your_db_table', APP_LOG_TIME_ZONE);