Skip to content

Commit 17632d9

Browse files
authored
Merge pull request #10 from danielme85/dev
Merge dev -> master, all green! ✅
2 parents 4974e8c + f5a5ed7 commit 17632d9

File tree

7 files changed

+133
-13
lines changed

7 files changed

+133
-13
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: php
22
php:
33
- 7.2
4+
- 7.3
45
services:
56
- mysql
67
- mongodb

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.1",
14+
"php": ">=7.2",
1515
"illuminate/support": ">=5.6"
1616
},
1717
"require-dev": {

readme.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![GitHub tag](https://img.shields.io/github/tag/danielme85/laravel-log-to-db.svg?style=flat-square)](https://github.com/danielme85/laravel-log-to-db)
66
[![Travis (.org)](https://img.shields.io/travis/danielme85/laravel-log-to-db.svg?style=flat-square)](https://travis-ci.org/danielme85/laravel-log-to-db)
77
[![Codecov](https://img.shields.io/codecov/c/github/danielme85/laravel-log-to-db.svg?style=flat-square)](https://codecov.io/gh/danielme85/laravel-log-to-db)
8+
[![CodeFactor](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db/badge)](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db)
89

910
Custom Laravel 6/5.6+ Log channel handler that can store log events to SQL or MongoDB databases.
1011
Uses Laravel native logging functionality.
@@ -50,7 +51,10 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
5051
'detailed' => true,
5152
'queue' => false,
5253
'queue_name' => '',
53-
'queue_connection' => ''
54+
'queue_connection' => '',
55+
'processors' => [
56+
//Monolog\Processor\HostnameProcessor::class
57+
]
5458
],
5559
...
5660
]
@@ -62,6 +66,8 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
6266
* connection = The DB connection from config/database.php to use (default: 'default').
6367
* collection = The DB table or collection name. (Default: log).
6468
* detailed = Store detailed log on Exceptions like stack-trace (default: true).
69+
* processors = Array of additional processors. These will add additional info into the 'extra' field in the logged data.
70+
[More information about processors](#processors)
6571

6672
More info about some of these options: https://laravel.com/docs/5.6/logging#customizing-monolog-for-channels
6773

@@ -227,6 +233,32 @@ LogToDB::model()->removeOlderThen('2019-01-01');
227233
LogToDB::model()->removeOlderThen('2019-01-01 23:00:00');
228234
```
229235

236+
#### Processors
237+
Monolog ships with a set of [processors](https://github.com/Seldaek/monolog/tree/master/src/Monolog/Processor), these will generate additional data and populate the 'extra' field.
238+
239+
You could also create your own custom processor, make sure they implement [Monolog\Processor\ProcessorInterface](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/ProcessorInterface.php).
240+
241+
##### Example of custom processor
242+
```php
243+
<?php
244+
245+
namespace App\CustomProcessors;
246+
247+
use Monolog\Processor\ProcessorInterface;
248+
249+
class PhpVersionProcessor implements ProcessorInterface {
250+
/**
251+
* @return array The processed record
252+
*/
253+
public function __invoke(array $record) {
254+
$record['extra']['php_version'] = phpversion();
255+
256+
return $record;
257+
}
258+
}
259+
260+
```
261+
230262
#### Advanced /config/logging.php example
231263
```php
232264
'default' => env('LOG_CHANNEL', 'stack'),

src/LogToDbCustomLoggingHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace danielme85\LaravelLogToDB;
44

55
use Monolog\Handler\AbstractProcessingHandler;
6-
use Monolog\Logger;
76

87
/**
98
* Class LogToDbHandler
@@ -83,10 +82,8 @@ function __construct(array $config,
8382
}
8483
}
8584

86-
8785
//Set the processors
88-
if (!empty($processors))
89-
{
86+
if (!empty($processors)) {
9087
foreach ($processors as $processor) {
9188
$this->pushProcessor($processor);
9289
}
@@ -112,7 +109,11 @@ protected function write(array $record): void
112109
$this->saveWithQueueConnection);
113110
$log->newFromMonolog($record);
114111
} catch (\Exception $e) {
115-
112+
//We want ignore this exception for (at least) two reasons:
113+
//1. Let's not ruin the whole app/request/job whatever is supposed to happen
114+
// by this log write operation failing.
115+
//2. There is a potential for an infinate loop of this log writer failing,
116+
// then trying to write a log about the log failing :(
116117
}
117118
}
118119
}

src/LogToDbHandler.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace danielme85\LaravelLogToDB;
44

55
use Monolog\Logger;
6-
use Monolog\Processor\IntrospectionProcessor;
76

87
/**
98
* Class LogToDbHandler
@@ -20,9 +19,16 @@ class LogToDbHandler
2019
*/
2120
public function __invoke(array $config)
2221
{
23-
$processors = [
24-
new IntrospectionProcessor()
25-
];
22+
$processors = [];
23+
24+
if (isset($config['processors']) && !empty($config['processors']) && is_array($config['processors'])) {
25+
foreach ($config['processors'] as $processorName) {
26+
if (class_exists($processorName)) {
27+
$processors[] = new $processorName;
28+
}
29+
}
30+
}
31+
2632
return new Logger($config['name'] ?? 'LogToDB',
2733
[
2834
new LogToDbCustomLoggingHandler($config, $processors)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dmellum
5+
* Date: 10/4/19
6+
* Time: 1:53 PM
7+
* ___ _ _ _ ___
8+
* |_ _|_ __ | |_ ___ _ __ __| | ___ ___(_) __ _ _ __ |_ _|_ __ ___
9+
* | || '_ \| __/ _ \ '__/ _` |/ _ \/ __| |/ _` | '_ \ | || '_ \ / __|
10+
* | || | | | || __/ | | (_| | __/\__ \ | (_| | | | | | || | | | (__
11+
* |___|_| |_|\__\___|_| \__,_|\___||___/_|\__, |_| |_| |___|_| |_|\___|
12+
* |___/
13+
*/
14+
15+
namespace danielme85\LaravelLogToDB\Processors;
16+
17+
use Monolog\Processor\ProcessorInterface;
18+
19+
class PhpVersionProcessor implements ProcessorInterface
20+
{
21+
/**
22+
* @return array The processed record
23+
*/
24+
public function __invoke(array $record) {
25+
$record['extra']['php_version'] = phpversion();
26+
27+
return $record;
28+
}
29+
}

tests/LogToDbTest.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,21 @@ protected function getEnvironmentSetUp($app)
6262
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
6363
'level' => 'debug',
6464
'connection' => 'default',
65-
'collection' => 'log'
65+
'collection' => 'log',
66+
'processors' => [
67+
Monolog\Processor\HostnameProcessor::class,
68+
danielme85\LaravelLogToDB\Processors\PhpVersionProcessor::class
69+
]
6670
],
6771
'mongodb' => [
6872
'driver' => 'custom',
6973
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
7074
'level' => 'debug',
7175
'connection' => 'mongodb',
72-
'collection' => 'log'
76+
'collection' => 'log',
77+
'processors' => [
78+
Monolog\Processor\HostnameProcessor::class
79+
]
7380
],
7481
'limited' => [
7582
'driver' => 'custom',
@@ -108,6 +115,10 @@ protected function getPackageProviders($app)
108115
public function testClassInit() {
109116
$test = new LogToDB();
110117
$this->assertInstanceOf('danielme85\LaravelLogToDB\LogToDB', $test);
118+
119+
//Class works, now let's cleanup possible failed test
120+
LogToDB::model()->truncate();
121+
LogToDB::model('mongodb')->truncate();
111122
}
112123

113124
/**
@@ -134,6 +145,19 @@ public function testLogLevels() {
134145
$this->assertCount(8, $logReaderSpecific);
135146
}
136147

148+
/**
149+
* Check to see if processors are adding extra content.
150+
*
151+
* @group basic
152+
*/
153+
public function testProcessors()
154+
{
155+
$log = LogToDB::model()->orderBy('created_at', 'desc')->first()->toArray();
156+
$this->assertNotEmpty($log['extra']);
157+
$this->assertNotEmpty($log['extra']['php_version']);
158+
$this->assertNotEmpty($log['extra']['hostname']);
159+
}
160+
137161
/**
138162
* Test logging to specific channels
139163
*
@@ -188,6 +212,33 @@ public function testQueue() {
188212
}
189213

190214
/**
215+
* Test save new log event job
216+
*
217+
* @group job
218+
*/
219+
public function testSaveNewLogEventJob()
220+
{
221+
$logToDb = new LogToDB();
222+
$record = [
223+
'message' => 'job-test',
224+
'context' => [],
225+
'level' => 200,
226+
'level_name' => 'INFO',
227+
'channel' => 'local',
228+
'datetime' => new Monolog\DateTimeImmutable(true),
229+
'extra' => [],
230+
'formatted' => "[2019-10-04T17:26:38.446827+00:00] local.INFO: test [] []\n"
231+
];
232+
233+
$job = new SaveNewLogEvent($logToDb, $record);
234+
$job->handle();
235+
236+
$this->assertNotEmpty($logToDb->model()->where('message', '=', 'job-test')->get());
237+
}
238+
239+
240+
/**
241+
* Test model interaction
191242
*
192243
* @group model
193244
*/

0 commit comments

Comments
 (0)