From 4f930ac72a1e6b79a81081d573eac5c456dddb07 Mon Sep 17 00:00:00 2001 From: Daniel Mellum Date: Thu, 11 Apr 2019 14:41:50 -0400 Subject: [PATCH 1/4] improve testing and documentation. --- readme.md | 2 +- src/config/logtodb.php | 2 +- .../2018_08_11_003343_create_log_table.php | 4 +- tests/LogMongo.php | 21 ++++++ tests/LogSql.php | 21 ++++++ tests/LogToDbTest.php | 65 +++++++++++++++++-- 6 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 tests/LogMongo.php create mode 100644 tests/LogSql.php diff --git a/readme.md b/readme.md index e861e54..0358334 100644 --- a/readme.md +++ b/readme.md @@ -101,7 +101,7 @@ LogToDB::model($channel, $connection, $collection); You can skip all function variables and the default settings from the config/logtodb.php will be used. ```php $model = LogToDB::model(); -$model->get(); //All logs for defualt channel/connection +$model->get(); //All logs for default channel/connection ``` Some more examples of getting logs diff --git a/src/config/logtodb.php b/src/config/logtodb.php index 54aadcf..71b98e3 100644 --- a/src/config/logtodb.php +++ b/src/config/logtodb.php @@ -38,7 +38,7 @@ | | Set the default database collection/table to use. */ - 'collection' => env('LOG_DB_COLLECTION', 'log'), + 'collection' => env('LOG_DB_COLLECTION', 'LogSql'), /* |-------------------------------------------------------------------------- diff --git a/src/migrations/2018_08_11_003343_create_log_table.php b/src/migrations/2018_08_11_003343_create_log_table.php index c3f11c9..26716a6 100644 --- a/src/migrations/2018_08_11_003343_create_log_table.php +++ b/src/migrations/2018_08_11_003343_create_log_table.php @@ -13,7 +13,7 @@ class CreateLogTable extends Migration */ public function up() { - Schema::create('log', function (Blueprint $table) { + Schema::create('LogSql', function (Blueprint $table) { $table->increments('id'); $table->text('message')->nullable(); $table->string('channel')->nullable(); @@ -34,6 +34,6 @@ public function up() */ public function down() { - Schema::dropIfExists('log'); + Schema::dropIfExists('LogSql'); } } diff --git a/tests/LogMongo.php b/tests/LogMongo.php new file mode 100644 index 0000000..fa2e89a --- /dev/null +++ b/tests/LogMongo.php @@ -0,0 +1,21 @@ + danielme85\LaravelLogToDB\LogToDbHandler::class, 'level' => 'debug', 'connection' => 'default', - 'collection' => 'log' + 'collection' => 'LogSql' ], 'mongodb' => [ 'driver' => 'custom', 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, 'level' => 'debug', 'connection' => 'mongodb', - 'collection' => 'log' + 'collection' => 'LogSql' ], 'limited' => [ 'driver' => 'custom', @@ -100,11 +100,21 @@ protected function getPackageProviders($app) ]; } + /** + * Basic test to see if class can be instanced. + * + * @group basic + */ public function testClassInit() { $test = new LogToDB(); $this->assertInstanceOf('danielme85\LaravelLogToDB\LogToDB', $test); } + /** + * Run basic log levels + * + * @group basic + */ public function testLogLevels() { Log::debug("This is an test DEBUG log event"); Log::info("This is an test INFO log event"); @@ -118,12 +128,17 @@ public function testLogLevels() { //Check mysql $logReader = LogToDB::model()->get()->toArray(); $logReaderMongoDB = LogToDB::model('mongodb')->get()->toArray(); - $logReaderSpecific = LogToDB::model('database', 'mysql', 'log')->get()->toArray(); + $logReaderSpecific = LogToDB::model('database', 'mysql', 'LogSql')->get()->toArray(); $this->assertCount(8, $logReader); $this->assertCount(8, $logReaderMongoDB); $this->assertCount(8, $logReaderSpecific); } + /** + * Test logging to specific channels + * + * @group advanced + */ public function testLoggingToChannels() { //Test limited config, with limited rows and level Log::channel('limited')->debug("This message should not be stored because DEBUG is LOWER then WARNING"); @@ -134,6 +149,11 @@ public function testLoggingToChannels() { $this->assertNotEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'WARNING')->get()->toArray()); } + /** + * Test an exception error. + * + * @group advanced + */ public function testException() { $e = new Symfony\Component\HttpKernel\Exception\BadRequestHttpException("This is a fake 500 error", null, 500, ['fake-header' => 'value']); Log::warning("Error", ['exception' => $e, 'more' => 'infohere']); @@ -142,8 +162,9 @@ public function testException() { } /** - * @group queue + * Test queuing the log events. * + * @group queue */ public function testQueue() { Queue::fake(); @@ -167,8 +188,39 @@ public function testQueue() { } /** - * @group cleanup * + * @group model + */ + public function testModelInteraction() { + $model = LogToDB::model(); + + //Get all + $all = $model->get(); + $this->assertNotEmpty($all->toArray()); + + //Get Debug + $logs = $model->where('level_name', '=', 'DEBUG')->get(); + $this->assertNotEmpty($logs->toArray()); + + $modelMongo = LogToDB::model(null, 'mongodb', 'LogSql'); + //Get all + $all = $modelMongo->get(); + $this->assertNotEmpty($all->toArray()); + + //Get Debug + $logs = $modelMongo->where('level_name', '=', 'DEBUG')->get(); + $this->assertNotEmpty($logs->toArray()); + } + + public function testStandAloneModels() { + $this->assertNotEmpty(LogSql::get()->toArray()); + $this->assertNotEmpty(LogMongo::get()->toArray()); + } + + /** + * Test the cleanup functions. + * + * @group cleanup */ public function testRemoves() { $this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(1)); @@ -178,6 +230,9 @@ public function testRemoves() { $this->assertFalse(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d'))); } + /** + * Clear all data from the test. + */ public function testCleanup() { LogToDB::model()->truncate(); LogToDB::model('mongodb')->truncate(); From 8acb69397a3dd2f76f370a75bcfdb6304cf691a3 Mon Sep 17 00:00:00 2001 From: Daniel Mellum Date: Thu, 11 Apr 2019 14:46:52 -0400 Subject: [PATCH 2/4] thanks for your help phpstorm. --- src/config/logtodb.php | 2 +- src/migrations/2018_08_11_003343_create_log_table.php | 4 ++-- tests/LogMongo.php | 2 +- tests/LogSql.php | 2 +- tests/LogToDbTest.php | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/config/logtodb.php b/src/config/logtodb.php index 71b98e3..54aadcf 100644 --- a/src/config/logtodb.php +++ b/src/config/logtodb.php @@ -38,7 +38,7 @@ | | Set the default database collection/table to use. */ - 'collection' => env('LOG_DB_COLLECTION', 'LogSql'), + 'collection' => env('LOG_DB_COLLECTION', 'log'), /* |-------------------------------------------------------------------------- diff --git a/src/migrations/2018_08_11_003343_create_log_table.php b/src/migrations/2018_08_11_003343_create_log_table.php index 26716a6..c3f11c9 100644 --- a/src/migrations/2018_08_11_003343_create_log_table.php +++ b/src/migrations/2018_08_11_003343_create_log_table.php @@ -13,7 +13,7 @@ class CreateLogTable extends Migration */ public function up() { - Schema::create('LogSql', function (Blueprint $table) { + Schema::create('log', function (Blueprint $table) { $table->increments('id'); $table->text('message')->nullable(); $table->string('channel')->nullable(); @@ -34,6 +34,6 @@ public function up() */ public function down() { - Schema::dropIfExists('LogSql'); + Schema::dropIfExists('log'); } } diff --git a/tests/LogMongo.php b/tests/LogMongo.php index fa2e89a..0105b22 100644 --- a/tests/LogMongo.php +++ b/tests/LogMongo.php @@ -16,6 +16,6 @@ class LogMongo extends Eloquent { - protected $collection = 'logs'; + protected $collection = 'log'; } \ No newline at end of file diff --git a/tests/LogSql.php b/tests/LogSql.php index 38347ce..a28888e 100644 --- a/tests/LogSql.php +++ b/tests/LogSql.php @@ -16,6 +16,6 @@ class LogSql extends Model { - protected $table = 'logs'; + protected $table = 'log'; } \ No newline at end of file diff --git a/tests/LogToDbTest.php b/tests/LogToDbTest.php index 1bb0634..d3d51e8 100644 --- a/tests/LogToDbTest.php +++ b/tests/LogToDbTest.php @@ -62,14 +62,14 @@ protected function getEnvironmentSetUp($app) 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, 'level' => 'debug', 'connection' => 'default', - 'collection' => 'LogSql' + 'collection' => 'log' ], 'mongodb' => [ 'driver' => 'custom', 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, 'level' => 'debug', 'connection' => 'mongodb', - 'collection' => 'LogSql' + 'collection' => 'log' ], 'limited' => [ 'driver' => 'custom', From 07f9630bc0e473836858d7afd4a2f39e2581c144 Mon Sep 17 00:00:00 2001 From: Daniel Mellum Date: Fri, 12 Apr 2019 11:36:09 -0400 Subject: [PATCH 3/4] Update readme. Add more testing around fetching logs. --- readme.md | 101 +++++++++++++++++++++++++++++++----------- tests/LogMongo.php | 1 + tests/LogToDbTest.php | 62 +++++++++++++++++++++++--- 3 files changed, 130 insertions(+), 34 deletions(-) diff --git a/readme.md b/readme.md index 0358334..5aab59c 100644 --- a/readme.md +++ b/readme.md @@ -23,17 +23,25 @@ php artisan migrate Starting with Laravel 5.6 you will have a new settings file: "config/logging.php". You will need to add an array under 'channels' for Log-to-DB here like so: ```php -'database' => [ - 'driver' => 'custom', - 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, - 'level' => env('APP_LOG_LEVEL', 'debug'), - 'name' => 'My DB Log', - 'connection' => 'default', - 'collection' => 'log', - 'detailed' => true, - 'queue' => false, - 'queue_name' => '', - 'queue_connection' => '' +'channels' => [ + 'stack' => [ + 'driver' => 'stack', + 'channels' => ['database', 'mongodb'], + ], + + 'database' => [ + 'driver' => 'custom', + 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, + 'level' => env('APP_LOG_LEVEL', 'debug'), + 'name' => 'My DB Log', + 'connection' => 'default', + 'collection' => 'log', + 'detailed' => true, + 'queue' => false, + 'queue_name' => '', + 'queue_connection' => '' + ], + ... ] ``` * driver = Required to trigger the log driver. @@ -94,8 +102,21 @@ The queue can be enabled/disabled in any of the following places: ## Usage Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with the Laravel Log Facade. See https://laravel.com/docs/5.6/logging for more information. +```php +Log::debug("This is an test DEBUG log event"); +Log::info("This is an test INFO log event"); +Log::notice("This is an test NOTICE log event"); +Log::warning("This is an test WARNING log event"); +Log::error("This is an test ERROR log event"); +Log::critical("This is an test CRITICAL log event"); +Log::alert("This is an test ALERT log event"); +Log::emergency("This is an test EMERGENCY log event"); +``` +You can also log to specific log channels: +Log::channel('database')debug("This is an test DEBUG log event"); + -#### Fetching Logs +### Fetching Logs The logging by this channel is done trough the Eloquent Model builder. LogToDB::model($channel, $connection, $collection); You can skip all function variables and the default settings from the config/logtodb.php will be used. @@ -114,18 +135,20 @@ When getting logs for specific channel or DB connection and collection you can e config/logging.php or connection name from config/databases.php. You can also specify collection/table name if needed as the third function variable when fetching the model. ```php -$logsFromDefault = LogDB::model()->get(); -$logsFromChannel = LogDB::model('database')->get(); -$logsFromMysql = LogToDB::model(null, 'mysql')->get(); -$logsFromMongoDB = LogToDB::model(null, 'mongodb', 'log')->get(); +$logsFromDefault = LogDB::model()->get(); //Get the logs from the default log channel and default connection. +$logsFromChannel = LogDB::model('database')->get(); //Get logs from the 'database' log channel. +$logsFromChannel = LogDB::model('customname')->get(); //Get logs from the 'customname' log channel. +$logsFromMysql = LogToDB::model(null, 'mysql')->get(); //Get all logs from the mysql connection (from Laravel database config) +$logsFromMongoDB = LogToDB::model(null, 'mongodb')->get(); //Get all logs from the mongodb connection (from Laravel database config) ``` -##### Custom Model +#### Add your own Model in your app Since Laravel is supposed to use static defined collection/table names, -it might be best to use your own model in your app for a more solid approach. +it might be better to use your own model in your app for a more solid approach.
https://laravel.com/docs/5.7/eloquent#eloquent-model-conventions +##### SQL ```php namespace App; @@ -133,15 +156,26 @@ use Illuminate\Database\Eloquent\Model; class Log extends Model { - /** - * The table associated with the model. - * - * @var string - */ protected $table = 'log'; - protected $connection = 'mysq;' + protected $connection = 'mysql' + } ``` + +##### MongoDB +```php +namespace App; + +use Jenssegers\Mongodb\Eloquent\Model as Eloquent; + +class LogMongo extends Eloquent +{ + protected $collection = 'log'; + protected $connection = 'mongodb'; + +} +``` + Fetching the model trough the LogToDB class (like the examples above) might have some side-effects as tables and connections are declared dynamically... aka made by Hackerman!
@@ -182,7 +216,7 @@ LogToDB::model()->removeOlderThen('2019-01-01'); LogToDB::model()->removeOlderThen('2019-01-01 23:00:00'); ``` -##### Advanced /config/logging.php example +#### Advanced /config/logging.php example ```php 'default' => env('LOG_CHANNEL', 'stack'), @@ -207,11 +241,24 @@ LogToDB::model()->removeOlderThen('2019-01-01 23:00:00'); 'mongodb' => [ 'driver' => 'custom', 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, - 'level' => env('APP_LOG_LEVEL', 'debug'), + 'level' => 'debug', 'connection' => 'mongodb', - 'collection' => 'log' + 'collection' => 'log', + 'detailed' => true, + 'queue' => true + 'queue_name' => 'logQueue' + 'queue_connection' => 'redis' ], + 'limited' => [ + 'driver' => 'custom', + 'via' => danielme85\LaravelLogToDB\LogToDbHandler::class, + 'level' => 'warning', + 'detailed' => false, + 'max_rows' => 10, + 'name' => 'limited', + ] + 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), diff --git a/tests/LogMongo.php b/tests/LogMongo.php index 0105b22..92b2954 100644 --- a/tests/LogMongo.php +++ b/tests/LogMongo.php @@ -17,5 +17,6 @@ class LogMongo extends Eloquent { protected $collection = 'log'; + protected $connection = 'mongodb'; } \ No newline at end of file diff --git a/tests/LogToDbTest.php b/tests/LogToDbTest.php index d3d51e8..b1bf27b 100644 --- a/tests/LogToDbTest.php +++ b/tests/LogToDbTest.php @@ -48,8 +48,8 @@ protected function getEnvironmentSetUp($app) 'options' => [ //'database' => 'admin' // sets the authentication database required by mongo 3 ] - ],] - ); + ], + ]); $app['config']->set('logging.default', 'stack'); $app['config']->set('logging.channels', [ @@ -193,23 +193,71 @@ public function testQueue() { */ public function testModelInteraction() { $model = LogToDB::model(); + //Get all + $all = $model->get(); + $this->assertNotEmpty($all->toArray()); + //Get Debug + $logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + $model = LogToDB::model('database'); //Get all $all = $model->get(); $this->assertNotEmpty($all->toArray()); + //Get Debug + $logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + $model = LogToDB::model(null, 'mysql'); + //Get all + $all = $model->get(); + $this->assertNotEmpty($all->toArray()); //Get Debug - $logs = $model->where('level_name', '=', 'DEBUG')->get(); - $this->assertNotEmpty($logs->toArray()); + $logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); - $modelMongo = LogToDB::model(null, 'mongodb', 'LogSql'); + $model = LogToDB::model('database', 'mysql', 'log'); + //Get all + $all = $model->get(); + $this->assertNotEmpty($all->toArray()); + //Get Debug + $logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + + //Same tests for mongoDB + $modelMongo = LogToDB::model('mongodb'); //Get all $all = $modelMongo->get(); $this->assertNotEmpty($all->toArray()); + //Get Debug + $logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + //Same tests for mongoDB + $modelMongo = LogToDB::model('mongodb', 'mongodb', 'log'); + //Get all + $all = $modelMongo->get(); + $this->assertNotEmpty($all->toArray()); //Get Debug - $logs = $modelMongo->where('level_name', '=', 'DEBUG')->get(); - $this->assertNotEmpty($logs->toArray()); + $logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + + //Same tests for mongoDB + $modelMongo = LogToDB::model(null, 'mongodb'); + //Get all + $all = $modelMongo->get(); + $this->assertNotEmpty($all->toArray()); + //Get Debug + $logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + } public function testStandAloneModels() { From 966c7735fbb5ba22051bfdc8d6af85c19e56912a Mon Sep 17 00:00:00 2001 From: Daniel Mellum Date: Fri, 12 Apr 2019 13:14:22 -0400 Subject: [PATCH 4/4] remove wonderbar ascii-art --- tests/LogMongo.php | 6 ------ tests/LogSql.php | 6 ------ 2 files changed, 12 deletions(-) diff --git a/tests/LogMongo.php b/tests/LogMongo.php index 92b2954..5c743fb 100644 --- a/tests/LogMongo.php +++ b/tests/LogMongo.php @@ -4,12 +4,6 @@ * User: dmellum * Date: 4/11/19 * Time: 2:35 PM - * ___ _ _ _ ___ - * |_ _|_ __ | |_ ___ _ __ __| | ___ ___(_) __ _ _ __ |_ _|_ __ ___ - * | || '_ \| __/ _ \ '__/ _` |/ _ \/ __| |/ _` | '_ \ | || '_ \ / __| - * | || | | | || __/ | | (_| | __/\__ \ | (_| | | | | | || | | | (__ - * |___|_| |_|\__\___|_| \__,_|\___||___/_|\__, |_| |_| |___|_| |_|\___| - * |___/ */ use Jenssegers\Mongodb\Eloquent\Model as Eloquent; diff --git a/tests/LogSql.php b/tests/LogSql.php index a28888e..38f9fbf 100644 --- a/tests/LogSql.php +++ b/tests/LogSql.php @@ -4,12 +4,6 @@ * User: dmellum * Date: 4/11/19 * Time: 2:35 PM - * ___ _ _ _ ___ - * |_ _|_ __ | |_ ___ _ __ __| | ___ ___(_) __ _ _ __ |_ _|_ __ ___ - * | || '_ \| __/ _ \ '__/ _` |/ _ \/ __| |/ _` | '_ \ | || '_ \ / __| - * | || | | | || __/ | | (_| | __/\__ \ | (_| | | | | | || | | | (__ - * |___|_| |_|\__\___|_| \__,_|\___||___/_|\__, |_| |_| |___|_| |_|\___| - * |___/ */ use Illuminate\Database\Eloquent\Model;