PHP Errors catching and monitoring
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
CODEOWNERS
LICENSE
README.md
composer.json

README.md

Hawk PHP

PHP errors Catcher for Hawk.so.

Usage

  1. Register an account and get an Integration Token.

  2. Install module

Use composer to install Catcher

$ composer require codex-team/hawk.php
  1. Use as a standalone catcher or use with Monolog.

Standalone error catcher

Create an instance with Token at the entry point of your project.

\Hawk\HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef');

Enable handlers

By default Hawk will catch everything. You can run function with no params.

\Hawk\HawkCatcher::enableHandlers();

It's similar to

\Hawk\HawkCatcher::enableHandlers(
    TRUE,       // exceptions
    TRUE,       // errors
    TRUE        // shutdown
);

You can pass types of errors you want to track:

// Catch run-time warnings or compile-time parse errors
\Hawk\HawkCatcher::enableHandlers(
    TRUE,                // exceptions
    E_WARNING | E_PARSE, // errors
    TRUE                 // shutdown
);
// Catch everything except notices
\Hawk\HawkCatcher::enableHandlers(
    TRUE,              // exceptions
    E_ALL & ~E_NOTICE, // errors
    TRUE               // shutdown
);

Catch handled exceptions

You can catch exceptions manually with catchException method.

try {
    throw new Exception("Error Processing Request", 1);
} catch (Exception $e) {
    \Hawk\HawkCatcher::catchException($e);
}

Monolog support

Add a handler to the Monolog. It will catch errors/exception and ignore general logs.

$logger = new \Monolog\Logger('hawk-test');

$HAWK_TOKEN = 'abcd1234-1234-abcd-1234-123456abcdef';
$logger->pushHandler(new \Hawk\Monolog\Handler($HAWK_TOKEN, \Monolog\Logger::DEBUG));

Now you can use logger's functions to process handled exceptions. Pass it to context array in 'exception' field.

try {
   throw new Exception('Something went wrong');
} catch (\Exception $e) {
   $logger->error($e->getMessage(), ['exception' => $e]);
}

Default error catcher

Register Monolog's handler as catcher.

/** Set monolog as default error handler */
$handler = \Monolog\ErrorHandler::register($logger);

It catches all errors and sends them to Hawk.

Throwing unhandled error example (without try-catch construction):

/** Fatal Error: "Just an error in a high quality code" */
throw new Error('Just an error in a high quality code', E_USER_ERROR);

Issues and improvements

Feel free to ask questions or improve the project.

Links

Repository: https://github.com/codex-team/hawk.php

Report a bug: https://github.com/codex-team/hawk.php/issues

Composer Package: https://packagist.org/packages/codex-team/hawk.php

CodeX Team: https://ifmo.su

License

MIT