Skip to content

Commit

Permalink
Merge pull request #10 from tylercd100/dev-record-extra-information
Browse files Browse the repository at this point in the history
Merging for 3.0.0 Release
  • Loading branch information
tylercd100 committed Mar 27, 2016
2 parents 209b8bf + 1b6f3d8 commit 1df4394
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 42 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `LERN` will be documented in this file.

### 3.0.0
- When enabled in the config file you can now collect:
- user_id - The id of the currently logged in user.
- method - Then method of the request: GET, POST, DELETE, PUT, etc...
- url - The full URL of the request.
- data - The input data of the request, if any.

*__Reason for Major release: 3.0.0 introduces a new migration file and structure changes that could cause issues for 2.x users__*

### 2.3.0
- Added support for [Twilio](https://www.twilio.com/) an SMS messaging service.

Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Currently supported notification channels via [Monolog](https://github.com/Selda
- [Plivo](https://www.plivo.com/) an SMS messaging service.
- [Twilio](https://www.twilio.com/) an SMS messaging service.

## Migrating from `2.x` to `3.x`
Version 3.x introduces the ability to collect more information from the error such as the user_id, url, method, and input data. In order to use 3.x you will need to copy over the new [config file](https://github.com/tylercd100/lern/blob/master/config/lern.php), the migration file and then migrate it.
```php
# This will only copy over the migration file. For the config file you can either include the --force flag (Which will overwrite it) or copy it manually from github
php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"
php artisan migrate
```

## Installation

Install via [composer](https://getcomposer.org/) - In the terminal:
Expand All @@ -32,12 +40,12 @@ Now add the following to the `providers` array in your `config/app.php`
```php
Tylercd100\LERN\LERNServiceProvider::class
```

and this to the `aliases` array in `config/app.php`
```php
"LERN" => "Tylercd100\LERN\Facades\LERN",
```


Then you will need to run these commands in the terminal in order to copy the config and migration files
```bash
php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"
Expand Down Expand Up @@ -79,7 +87,21 @@ You can call `LERN::record($exception);` to record an Exception to the database.
To query any Exception that has been recorded you can use `ExceptionModel` which is an Eloquent Model
```php
use Tylercd100\LERN\Model\ExceptionModel;
$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first()
$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first();
```

To change what is recorded in to the database take a look at `config/lern.php`
```php
'record'=>[
'table'=>'vendor_tylercd100_lern_exceptions',
'collect'=>[
'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc...
'data'=>false, //When true it will collect Input data
'status_code'=>true,
'user_id'=>false,
'url'=>false,
],
],
```

### Notifications
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"require-dev": {
"orchestra/testbench": "^3.1.0",
"phpunit/phpunit": "^4.8 || ^5.0"
"phpunit/phpunit": "^4.8 || ^5.0",
"doctrine/dbal": "~2.3"
},
"suggest": {
}
Expand Down
7 changes: 7 additions & 0 deletions config/lern.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

'record'=>[
'table'=>'vendor_tylercd100_lern_exceptions',
'collect'=>[
'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc...
'data'=>false, //When true it will collect Input data
'status_code'=>true,
'user_id'=>false,
'url'=>false,
],
],

'notify'=>[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddUserDataAndUrlToLERNTables extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table(config('lern.record.table'), function(Blueprint $table) {
$table->integer('user_id')->nullable();
$table->text('data')->nullable();
$table->string('url')->nullable();
$table->string('method')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table(config('lern.record.table'), function(Blueprint $table) {
$table->dropColumn('user_id');
$table->dropColumn('data');
$table->dropColumn('url');
$table->dropColumn('method');
});
}

}
4 changes: 2 additions & 2 deletions src/Notifications/Notifier.php → src/Components/Notifier.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Tylercd100\LERN\Notifications;
namespace Tylercd100\LERN\Components;

use Exception;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Tylercd100\LERN\Notifications\MonologHandlerFactory;
use Tylercd100\LERN\Factories\MonologHandlerFactory;

class Notifier {
protected $config;
Expand Down
150 changes: 150 additions & 0 deletions src/Components/Recorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Tylercd100\LERN\Components;

use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Tylercd100\LERN\Models\ExceptionModel;

class Recorder {

/**
* @var mixed
*/
protected $config = [];

/**
* The constructor
*/
public function __construct() {
$this->config = config('lern.record');
}

/**
* Records an Exception to the database
* @param Exception $e The exception you want to record
* @return ExceptionModel
*/
public function record(Exception $e)
{
$opts = [
'class' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'code' => $e->getCode(),
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
];


$configDependant = ['user_id', 'status_code', 'method', 'data', 'url'];

foreach ($configDependant as $key) {
if ($this->canCollect($key)) {
$opts[$key] = $this->collect($key, $e);
}
}

return ExceptionModel::create($opts);
}

/**
* Checks the config to see if you can collect certain information
* @param string $type the config value you want to check
* @return boolean
*/
private function canCollect($type) {
if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
return $this->config['collect'][$type] === true;
}
return false;
}

/**
* @param string $key
*/
protected function collect($key,Exception $e = null){
switch ($key) {
case 'user_id':
return $this->getUserId();
case 'method':
return $this->getMethod();
case 'status_code':
return $this->getStatusCode($e);
case 'url':
return $this->getUrl();
case 'data':
return $this->getData();
default:
throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
}
}

/**
* Gets the ID of the User that is logged in
* @return integer|null The ID of the User or Null if not logged in
*/
protected function getUserId() {
$user = Auth::user();
if (is_object($user)) {
return $user->id;
} else {
return null;
}
}

/**
* Gets the Method of the Request
* @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
*/
protected function getMethod() {
$method = Request::method();
if (!empty($method)) {
return $method;
} else {
return null;
}
}

/**
* Gets the input data of the Request
* @return array|null The Input data or null
*/
protected function getData() {
$data = Input::all();
if (is_array($data)) {
return $data;
} else {
return null;
}
}

/**
* Gets the URL of the Request
* @return string|null Returns a URL string or null
*/
protected function getUrl() {
$url = Request::url();
if (is_string($url)) {
return $url;
} else {
return null;
}
}

/**
* Gets the status code of the Exception
* @param Exception $e The Exception to check
* @return string|integer The status code value
*/
protected function getStatusCode(Exception $e) {
if ($e instanceof HttpExceptionInterface) {
return $e->getStatusCode();
} else {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tylercd100\LERN\Notifications;
namespace Tylercd100\LERN\Factories;

use Exception;
use Monolog\Logger;
Expand Down
Loading

0 comments on commit 1df4394

Please sign in to comment.