Skip to content

Commit 447ea85

Browse files
authored
Merge pull request #4 from danielme85/dev
Merge dev into master
2 parents 0626375 + 966c773 commit 447ea85

File tree

4 files changed

+214
-33
lines changed

4 files changed

+214
-33
lines changed

readme.md

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@ php artisan migrate
2323
Starting with Laravel 5.6 you will have a new settings file: "config/logging.php".
2424
You will need to add an array under 'channels' for Log-to-DB here like so:
2525
```php
26-
'database' => [
27-
'driver' => 'custom',
28-
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
29-
'level' => env('APP_LOG_LEVEL', 'debug'),
30-
'name' => 'My DB Log',
31-
'connection' => 'default',
32-
'collection' => 'log',
33-
'detailed' => true,
34-
'queue' => false,
35-
'queue_name' => '',
36-
'queue_connection' => ''
26+
'channels' => [
27+
'stack' => [
28+
'driver' => 'stack',
29+
'channels' => ['database', 'mongodb'],
30+
],
31+
32+
'database' => [
33+
'driver' => 'custom',
34+
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
35+
'level' => env('APP_LOG_LEVEL', 'debug'),
36+
'name' => 'My DB Log',
37+
'connection' => 'default',
38+
'collection' => 'log',
39+
'detailed' => true,
40+
'queue' => false,
41+
'queue_name' => '',
42+
'queue_connection' => ''
43+
],
44+
...
3745
]
3846
```
3947
* driver = Required to trigger the log driver.
@@ -94,14 +102,27 @@ The queue can be enabled/disabled in any of the following places:
94102
## Usage
95103
Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with
96104
the Laravel Log Facade. See https://laravel.com/docs/5.6/logging for more information.
105+
```php
106+
Log::debug("This is an test DEBUG log event");
107+
Log::info("This is an test INFO log event");
108+
Log::notice("This is an test NOTICE log event");
109+
Log::warning("This is an test WARNING log event");
110+
Log::error("This is an test ERROR log event");
111+
Log::critical("This is an test CRITICAL log event");
112+
Log::alert("This is an test ALERT log event");
113+
Log::emergency("This is an test EMERGENCY log event");
114+
```
115+
You can also log to specific log channels:
116+
Log::channel('database')debug("This is an test DEBUG log event");
117+
97118

98-
#### Fetching Logs
119+
### Fetching Logs
99120
The logging by this channel is done trough the Eloquent Model builder.
100121
LogToDB::model($channel, $connection, $collection);
101122
You can skip all function variables and the default settings from the config/logtodb.php will be used.
102123
```php
103124
$model = LogToDB::model();
104-
$model->get(); //All logs for defualt channel/connection
125+
$model->get(); //All logs for default channel/connection
105126
```
106127

107128
Some more examples of getting logs
@@ -114,34 +135,47 @@ When getting logs for specific channel or DB connection and collection you can e
114135
config/logging.php or connection name from config/databases.php. You can also specify collection/table name if needed as
115136
the third function variable when fetching the model.
116137
```php
117-
$logsFromDefault = LogDB::model()->get();
118-
$logsFromChannel = LogDB::model('database')->get();
119-
$logsFromMysql = LogToDB::model(null, 'mysql')->get();
120-
$logsFromMongoDB = LogToDB::model(null, 'mongodb', 'log')->get();
138+
$logsFromDefault = LogDB::model()->get(); //Get the logs from the default log channel and default connection.
139+
$logsFromChannel = LogDB::model('database')->get(); //Get logs from the 'database' log channel.
140+
$logsFromChannel = LogDB::model('customname')->get(); //Get logs from the 'customname' log channel.
141+
$logsFromMysql = LogToDB::model(null, 'mysql')->get(); //Get all logs from the mysql connection (from Laravel database config)
142+
$logsFromMongoDB = LogToDB::model(null, 'mongodb')->get(); //Get all logs from the mongodb connection (from Laravel database config)
121143
```
122144

123-
##### Custom Model
145+
#### Add your own Model in your app
124146
Since Laravel is supposed to use static defined collection/table names,
125-
it might be best to use your own model in your app for a more solid approach.
147+
it might be better to use your own model in your app for a more solid approach.
126148
<br>
127149
https://laravel.com/docs/5.7/eloquent#eloquent-model-conventions
128150

151+
##### SQL
129152
```php
130153
namespace App;
131154

132155
use Illuminate\Database\Eloquent\Model;
133156

134157
class Log extends Model
135158
{
136-
/**
137-
* The table associated with the model.
138-
*
139-
* @var string
140-
*/
141159
protected $table = 'log';
142-
protected $connection = 'mysq;'
160+
protected $connection = 'mysql'
161+
143162
}
144163
```
164+
165+
##### MongoDB
166+
```php
167+
namespace App;
168+
169+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
170+
171+
class LogMongo extends Eloquent
172+
{
173+
protected $collection = 'log';
174+
protected $connection = 'mongodb';
175+
176+
}
177+
```
178+
145179
Fetching the model trough the LogToDB class (like the examples above) might have some side-effects as tables and connections are
146180
declared dynamically... aka made by Hackerman!
147181
<br>
@@ -182,7 +216,7 @@ LogToDB::model()->removeOlderThen('2019-01-01');
182216
LogToDB::model()->removeOlderThen('2019-01-01 23:00:00');
183217
```
184218

185-
##### Advanced /config/logging.php example
219+
#### Advanced /config/logging.php example
186220
```php
187221
'default' => env('LOG_CHANNEL', 'stack'),
188222

@@ -207,11 +241,24 @@ LogToDB::model()->removeOlderThen('2019-01-01 23:00:00');
207241
'mongodb' => [
208242
'driver' => 'custom',
209243
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
210-
'level' => env('APP_LOG_LEVEL', 'debug'),
244+
'level' => 'debug',
211245
'connection' => 'mongodb',
212-
'collection' => 'log'
246+
'collection' => 'log',
247+
'detailed' => true,
248+
'queue' => true
249+
'queue_name' => 'logQueue'
250+
'queue_connection' => 'redis'
213251
],
214252

253+
'limited' => [
254+
'driver' => 'custom',
255+
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
256+
'level' => 'warning',
257+
'detailed' => false,
258+
'max_rows' => 10,
259+
'name' => 'limited',
260+
]
261+
215262
'single' => [
216263
'driver' => 'single',
217264
'path' => storage_path('logs/laravel.log'),

tests/LogMongo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dmellum
5+
* Date: 4/11/19
6+
* Time: 2:35 PM
7+
*/
8+
9+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
10+
11+
class LogMongo extends Eloquent
12+
{
13+
protected $collection = 'log';
14+
protected $connection = 'mongodb';
15+
16+
}

tests/LogSql.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dmellum
5+
* Date: 4/11/19
6+
* Time: 2:35 PM
7+
*/
8+
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
class LogSql extends Model
12+
{
13+
protected $table = 'log';
14+
15+
}

tests/LogToDbTest.php

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected function getEnvironmentSetUp($app)
4848
'options' => [
4949
//'database' => 'admin' // sets the authentication database required by mongo 3
5050
]
51-
],]
52-
);
51+
],
52+
]);
5353

5454
$app['config']->set('logging.default', 'stack');
5555
$app['config']->set('logging.channels', [
@@ -100,11 +100,21 @@ protected function getPackageProviders($app)
100100
];
101101
}
102102

103+
/**
104+
* Basic test to see if class can be instanced.
105+
*
106+
* @group basic
107+
*/
103108
public function testClassInit() {
104109
$test = new LogToDB();
105110
$this->assertInstanceOf('danielme85\LaravelLogToDB\LogToDB', $test);
106111
}
107112

113+
/**
114+
* Run basic log levels
115+
*
116+
* @group basic
117+
*/
108118
public function testLogLevels() {
109119
Log::debug("This is an test DEBUG log event");
110120
Log::info("This is an test INFO log event");
@@ -118,12 +128,17 @@ public function testLogLevels() {
118128
//Check mysql
119129
$logReader = LogToDB::model()->get()->toArray();
120130
$logReaderMongoDB = LogToDB::model('mongodb')->get()->toArray();
121-
$logReaderSpecific = LogToDB::model('database', 'mysql', 'log')->get()->toArray();
131+
$logReaderSpecific = LogToDB::model('database', 'mysql', 'LogSql')->get()->toArray();
122132
$this->assertCount(8, $logReader);
123133
$this->assertCount(8, $logReaderMongoDB);
124134
$this->assertCount(8, $logReaderSpecific);
125135
}
126136

137+
/**
138+
* Test logging to specific channels
139+
*
140+
* @group advanced
141+
*/
127142
public function testLoggingToChannels() {
128143
//Test limited config, with limited rows and level
129144
Log::channel('limited')->debug("This message should not be stored because DEBUG is LOWER then WARNING");
@@ -134,6 +149,11 @@ public function testLoggingToChannels() {
134149
$this->assertNotEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'WARNING')->get()->toArray());
135150
}
136151

152+
/**
153+
* Test an exception error.
154+
*
155+
* @group advanced
156+
*/
137157
public function testException() {
138158
$e = new Symfony\Component\HttpKernel\Exception\BadRequestHttpException("This is a fake 500 error", null, 500, ['fake-header' => 'value']);
139159
Log::warning("Error", ['exception' => $e, 'more' => 'infohere']);
@@ -142,8 +162,9 @@ public function testException() {
142162
}
143163

144164
/**
145-
* @group queue
165+
* Test queuing the log events.
146166
*
167+
* @group queue
147168
*/
148169
public function testQueue() {
149170
Queue::fake();
@@ -167,8 +188,87 @@ public function testQueue() {
167188
}
168189

169190
/**
170-
* @group cleanup
171191
*
192+
* @group model
193+
*/
194+
public function testModelInteraction() {
195+
$model = LogToDB::model();
196+
//Get all
197+
$all = $model->get();
198+
$this->assertNotEmpty($all->toArray());
199+
//Get Debug
200+
$logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray();
201+
$this->assertNotEmpty($logs);
202+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
203+
204+
$model = LogToDB::model('database');
205+
//Get all
206+
$all = $model->get();
207+
$this->assertNotEmpty($all->toArray());
208+
//Get Debug
209+
$logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray();
210+
$this->assertNotEmpty($logs);
211+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
212+
213+
$model = LogToDB::model(null, 'mysql');
214+
//Get all
215+
$all = $model->get();
216+
$this->assertNotEmpty($all->toArray());
217+
//Get Debug
218+
$logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray();
219+
$this->assertNotEmpty($logs);
220+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
221+
222+
$model = LogToDB::model('database', 'mysql', 'log');
223+
//Get all
224+
$all = $model->get();
225+
$this->assertNotEmpty($all->toArray());
226+
//Get Debug
227+
$logs = $model->where('level_name', '=', 'DEBUG')->get()->toArray();
228+
$this->assertNotEmpty($logs);
229+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
230+
231+
//Same tests for mongoDB
232+
$modelMongo = LogToDB::model('mongodb');
233+
//Get all
234+
$all = $modelMongo->get();
235+
$this->assertNotEmpty($all->toArray());
236+
//Get Debug
237+
$logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray();
238+
$this->assertNotEmpty($logs);
239+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
240+
241+
//Same tests for mongoDB
242+
$modelMongo = LogToDB::model('mongodb', 'mongodb', 'log');
243+
//Get all
244+
$all = $modelMongo->get();
245+
$this->assertNotEmpty($all->toArray());
246+
//Get Debug
247+
$logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray();
248+
$this->assertNotEmpty($logs);
249+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
250+
251+
//Same tests for mongoDB
252+
$modelMongo = LogToDB::model(null, 'mongodb');
253+
//Get all
254+
$all = $modelMongo->get();
255+
$this->assertNotEmpty($all->toArray());
256+
//Get Debug
257+
$logs = $modelMongo->where('level_name', '=', 'DEBUG')->get()->toArray();
258+
$this->assertNotEmpty($logs);
259+
$this->assertEquals('DEBUG', $logs[0]['level_name']);
260+
261+
}
262+
263+
public function testStandAloneModels() {
264+
$this->assertNotEmpty(LogSql::get()->toArray());
265+
$this->assertNotEmpty(LogMongo::get()->toArray());
266+
}
267+
268+
/**
269+
* Test the cleanup functions.
270+
*
271+
* @group cleanup
172272
*/
173273
public function testRemoves() {
174274
$this->assertTrue(LogToDB::model()->removeOldestIfMoreThen(1));
@@ -178,6 +278,9 @@ public function testRemoves() {
178278
$this->assertFalse(LogToDB::model('mongodb')->removeOlderThen(date('Y-m-d')));
179279
}
180280

281+
/**
282+
* Clear all data from the test.
283+
*/
181284
public function testCleanup() {
182285
LogToDB::model()->truncate();
183286
LogToDB::model('mongodb')->truncate();

0 commit comments

Comments
 (0)