diff --git a/CHANGELOG.md b/CHANGELOG.md index 6172d30..c9142de 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 314baa5..299dc93 100755 --- a/README.md +++ b/README.md @@ -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, ], ], ``` diff --git a/config/lern.php b/config/lern.php index 5961563..c861302 100755 --- a/config/lern.php +++ b/config/lern.php @@ -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, ], /** @@ -22,6 +29,11 @@ ], 'notify'=>[ + /** + * The Notifier to use + */ + 'class' => \Tylercd100\LERN\Components\Notifier::class, + /** * The view file to use */ diff --git a/migrations/2017_09_23_000000_add_ip_to_lern_tables.php b/migrations/2017_09_23_000000_add_ip_to_lern_tables.php new file mode 100644 index 0000000..8b0097b --- /dev/null +++ b/migrations/2017_09_23_000000_add_ip_to_lern_tables.php @@ -0,0 +1,32 @@ +string('ip')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table(config('lern.record.table'), function(Blueprint $table) { + $table->dropColumn('ip'); + }); + } + +} \ No newline at end of file diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 20f2bc9..105dfa4 100755 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -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) { @@ -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: @@ -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 diff --git a/src/LERN.php b/src/LERN.php index 26c3348..044857e 100755 --- a/src/LERN.php +++ b/src/LERN.php @@ -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 @@ -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); } /** @@ -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); + } + } } \ No newline at end of file diff --git a/src/LERNServiceProvider.php b/src/LERNServiceProvider.php index fc9ff46..c9be5d0 100755 --- a/src/LERNServiceProvider.php +++ b/src/LERNServiceProvider.php @@ -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'), ]); }