diff --git a/readme.md b/readme.md index e861e54..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,14 +102,27 @@ 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. ```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 @@ -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 new file mode 100644 index 0000000..5c743fb --- /dev/null +++ b/tests/LogMongo.php @@ -0,0 +1,16 @@ + [ //'database' => 'admin' // sets the authentication database required by mongo 3 ] - ],] - ); + ], + ]); $app['config']->set('logging.default', 'stack'); $app['config']->set('logging.channels', [ @@ -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,87 @@ 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()->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()->toArray(); + $this->assertNotEmpty($logs); + $this->assertEquals('DEBUG', $logs[0]['level_name']); + + $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()->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() { + $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 +278,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();