Skip to content

Commit

Permalink
Added IP option and custom Recorder/Notifier classes (#51)
Browse files Browse the repository at this point in the history
* custom fields

* added missing use statements
  • Loading branch information
tylercd100 authored Sep 23, 2017
1 parent dd14902 commit f93abbb
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

### 4.2.0
- Added the ability to use Custom Recorder and Notifier classes
- Added IP option to the config to collect IP addresses

### 4.1.1
- Small typo

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ To change what is recorded in to the database take a look at `config/lern.php`
'status_code'=>true,
'user_id'=>false,
'url'=>false,
'ip'=>false,
],
],
```
Expand Down
12 changes: 12 additions & 0 deletions config/lern.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@


'record'=>[
/**
* The Recorder to use
*/
'class' => \Tylercd100\LERN\Components\Recorder::class,

'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,
'ip'=>false,
],

/**
Expand All @@ -22,6 +29,11 @@
],

'notify'=>[
/**
* The Notifier to use
*/
'class' => \Tylercd100\LERN\Components\Notifier::class,

/**
* The view file to use
*/
Expand Down
32 changes: 32 additions & 0 deletions migrations/2017_09_23_000000_add_ip_to_lern_tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

class AddIpToLernTables extends Migration {

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

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

}
15 changes: 13 additions & 2 deletions src/Components/Recorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function record(Exception $e)
'trace' => $e->getTraceAsString(),
];

$configDependant = ['user_id', 'status_code', 'method', 'data', 'url'];
$configDependant = array_keys($this->config['collect']);

try {
foreach ($configDependant as $key) {
Expand Down Expand Up @@ -92,9 +92,11 @@ protected function collect($key, Exception $e = null) {
return $this->getUrl();
case 'data':
return $this->getData();
case 'ip':
return $this->getIp();
case 'status_code':
if ($e === null) {
return 0;
return 0;
}
return $this->getStatusCode($e);
default:
Expand Down Expand Up @@ -154,6 +156,15 @@ protected function getUrl() {
}
}

/**
* Returns the IP from the request
*
* @return string
*/
protected function getIp() {
return Request::ip();
}

/**
* Gets the status code of the Exception
* @param Exception $e The Exception to check
Expand Down
58 changes: 45 additions & 13 deletions src/LERN.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Monolog\Handler\HandlerInterface;
use Tylercd100\LERN\Components\Notifier;
use Tylercd100\LERN\Components\Recorder;
use Tylercd100\LERN\Exceptions\NotifierFailedException;
use Tylercd100\LERN\Exceptions\RecorderFailedException;

/**
* The master class
Expand All @@ -18,30 +20,23 @@ class LERN
private $exception;

/**
* @var \Tylercd100\LERN\Components\Notifier
* @var Notifier
*/
private $notifier;

/**
* @var \Tylercd100\LERN\Components\Recorder
* @var Recorder
*/
private $recorder;

/**
* @param \Tylercd100\LERN\Components\Notifier|null $notifier Notifier instance
* @param \Tylercd100\LERN\Components\Recorder|null $recorder Recorder instance
* @param Notifier|null $notifier Notifier instance
* @param Recorder|null $recorder Recorder instance
*/
public function __construct(Notifier $notifier = null, Recorder $recorder = null)
{
if (empty($notifier)) {
$notifier = new Notifier();
}
$this->notifier = $notifier;

if (empty($recorder)) {
$recorder = new Recorder();
}
$this->recorder = $recorder;
$this->notifier = $this->buildNotifier($notifier);
$this->recorder = $this->buildRecorder($recorder);
}

/**
Expand Down Expand Up @@ -150,4 +145,41 @@ public function setSubject($cb)
return $this;
}

/**
* Constructs a Notifier
*
* @param Notifier $notifier
* @return Notifier
*/
protected function buildNotifier(Notifier $notifier = null)
{
$class = config('lern.notify.class');
if (empty($notifier)) {
$notifier = new $class();
}
if ($notifier instanceof Notifier) {
return $notifier;
} else {
throw new NotifierFailedException("LERN was expecting an instance of ".Notifier::class);
}
}

/**
* Constructs a Recorder
*
* @param Recorder $recorder
* @return Recorder
*/
protected function buildRecorder(Recorder $recorder = null)
{
$class = config('lern.record.class');
if (empty($recorder)) {
$recorder = new $class();
}
if ($recorder instanceof Recorder) {
return $recorder;
} else {
throw new RecorderFailedException("LERN was expecting an instance of ".Recorder::class);
}
}
}
1 change: 1 addition & 0 deletions src/LERNServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function boot()
__DIR__.'/../views/exceptions/default.blade.php' => base_path('resources/views/exceptions/default.blade.php'),
__DIR__.'/../migrations/2016_03_17_000000_create_lern_tables.php' => base_path('database/migrations/2016_03_17_000000_create_lern_tables.php'),
__DIR__.'/../migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php' => base_path('database/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php'),
__DIR__.'/../migrations/2017_09_23_000000_add_ip_to_lern_tables.php' => base_path('database/migrations/2017_09_23_000000_add_ip_to_lern_tables.php'),
__DIR__.'/../config/lern.php' => base_path('config/lern.php'),
]);
}
Expand Down

0 comments on commit f93abbb

Please sign in to comment.