Skip to content

Commit

Permalink
Merge pull request #18 from codex-team/update
Browse files Browse the repository at this point in the history
improve catcher
  • Loading branch information
talyguryn authored Jun 5, 2018
2 parents 8b0e7e9 + 9ab77bf commit 96c7bea
Show file tree
Hide file tree
Showing 9 changed files with 721 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
composer.lock
vendor/
.idea/
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 CodeX
Copyright (c) 2018 CodeX

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
111 changes: 78 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,40 @@
# hawk.php
# Hawk PHP

PHP errors Catcher module for [Hawk.so](https://hawk.so)
PHP errors Catcher for [Hawk.so](https://hawk.so).

![](https://capella.pics/c0fe5eeb-027d-427a-9e0d-b2e1dcaaf303)

## Usage

[Register](https://hawk.so/join) an account and get a project token.
1. [Register](https://hawk.so/join) an account and get an Integration Token.

### Install module
2. Install module

Use [composer](https://getcomposer.org) to install Catcher

```bash
$ composer require codex-team/hawk.php
$ composer install
```

#### Download and require php file

You can download this repository and require `Hawk.php` file in your project.
3. Use as a [standalone catcher](#standalone-error-catcher) or use with [Monolog](#monolog-support).

```php
require './hawk.php/src/Hawk.php';
```
## Standalone error catcher

### Init HawkCatcher

Create an instance with token to the entry point of your project (usually `index.php` or `bootstrap.php`).
Create an instance with Token at the entry point of your project.

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

You can store token in the environment file

```php
\Hawk\HawkCatcher::instance($_SERVER['HAWK_TOKEN']);
```

#### Custom Hawk server
### Enable handlers

If you want to use custom Hawk server then pass a url to this catcher.
By default Hawk will catch everything. You can run function with no params.

```php
\Hawk\HawkCatcher::instance(
'abcd1234-1234-abcd-1234-123456abcdef',
'http://myownhawk.com/catcher/php'
);
\Hawk\HawkCatcher::enableHandlers();
```

### Enable handlers

If you want to catch error automatically run the following command with boolean params to enable some handlers.
It's similar to

```php
\Hawk\HawkCatcher::enableHandlers(
Expand All @@ -60,15 +44,29 @@ If you want to catch error automatically run the following command with boolean
);
```

By default Hawk will catch everything. You can run function with no params.
You can pass types of errors you want to track:

```php
\Hawk\HawkCatcher::enableHandlers();
// Catch run-time warnings or compile-time parse errors
\Hawk\HawkCatcher::enableHandlers(
TRUE, // exceptions
E_WARNING | E_PARSE, // errors
TRUE // shutdown
);
```

### Catch exception
```php
// Catch everything except notices
\Hawk\HawkCatcher::enableHandlers(
TRUE, // exceptions
E_ALL & ~E_NOTICE, // errors
TRUE // shutdown
);
```

### Catch handled exceptions

You can catch exceptions by yourself without enabling handlers.
You can catch exceptions manually with `catchException` method.

```php
try {
Expand All @@ -78,6 +76,49 @@ try {
}
```

## Monolog support

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

```php
$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.

```php
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.

```php
/** 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):

```php
/** 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
Expand All @@ -87,3 +128,7 @@ 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "library",
"license": "MIT",
"require": {
"ext-curl": "*",
"php": ">=5.3"
},
"autoload": {
Expand Down
Loading

0 comments on commit 96c7bea

Please sign in to comment.