Skip to content

Commit 0324a92

Browse files
committed
Add custom LogException that gets ignored by the log itself to avoid log spamming loop.
Some general code cleanup.
1 parent dc24904 commit 0324a92

File tree

8 files changed

+114
-74
lines changed

8 files changed

+114
-74
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ Then add to your AppServiceProvider (or another provider that calls the app boot
237237
LogToDB::model()->observe(LogObserver::class);
238238
}
239239
}
240-
``
241-
240+
```
241+
242242

243243
#### Adding tables/expanding collections
244244
The Log handler for SQL expects the following schema:

src/Jobs/SaveNewLogEvent.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class SaveNewLogEvent implements ShouldQueue
1212
{
1313
use Dispatchable, InteractsWithQueue, Queueable;
1414

15+
/**
16+
* @var object
17+
*/
1518
protected $logToDb;
19+
20+
/**
21+
* @var array
22+
*/
1623
protected $record;
1724

1825
/**
@@ -35,10 +42,10 @@ public function __construct($logToDb, $record)
3542
*/
3643
public function handle()
3744
{
38-
$log = CreateLogFromRecord::generate(
39-
$this->logToDb->getModel(),
45+
$model = $this->logToDb->getModel();
46+
$log = $model->generate(
4047
$this->record,
41-
$this->logToDb->getConfig('detailed') ?? null
48+
$this->logToDb->getConfig('detailed')
4249
);
4350

4451
$log->save();

src/LogToDB.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
66
use danielme85\LaravelLogToDB\Models\DBLog;
77
use danielme85\LaravelLogToDB\Models\DBLogMongoDB;
8-
use danielme85\LaravelLogToDB\Models\CreateLogFromRecord;
98

109
/**
1110
* Class LogToDb
@@ -32,7 +31,7 @@ class LogToDB
3231
public $database;
3332

3433
/**
35-
* @var
34+
* @var string
3635
*/
3736
protected $model;
3837

@@ -175,8 +174,8 @@ public function newFromMonolog(array $record)
175174
->onQueue($this->config['queue_name']);
176175
}
177176
} else {
178-
$log = CreateLogFromRecord::generate(
179-
$this->getModel(),
177+
$model = $this->getModel();
178+
$log = $model->generate(
180179
$record,
181180
$this->getConfig('detailed')
182181
);
@@ -190,6 +189,8 @@ public function newFromMonolog(array $record)
190189
}
191190

192191
/**
192+
* Get config value
193+
*
193194
* @param string $config
194195
* @return mixed|null
195196
*/

src/LogToDbCustomLoggingHandler.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace danielme85\LaravelLogToDB;
44

5+
use danielme85\LaravelLogToDB\Models\DBLogException;
56
use Monolog\Handler\AbstractProcessingHandler;
67

78
/**
@@ -49,12 +50,24 @@ function __construct(array $config,
4950
* Write the Log
5051
*
5152
* @param array $record
53+
* @throws DBLogException
5254
*/
5355
protected function write(array $record): void
5456
{
5557
if (!empty($record)) {
56-
$log = new LogToDB($this->config);
57-
$log->newFromMonolog($record);
58+
if (!empty($record['context']['exception'])
59+
&& strpos(get_class($record['context']['exception']), "DBLogException")) {
60+
//Do nothing if empty log record or an error Exception from itself.
61+
} else {
62+
try {
63+
$log = new LogToDB($this->config);
64+
$log->newFromMonolog($record);
65+
} catch (\Exception $e) {
66+
//convert any runtime Exception while logging to a special class so we can avoid our own
67+
//exceptions for 99% less infinite loops!
68+
throw new DBLogException($e->getMessage());
69+
}
70+
}
5871
}
5972
}
6073
}

src/Models/CreateLogFromRecord.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Models/DBLogException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace danielme85\LaravelLogToDB\Models;
4+
5+
/**
6+
* Class DBLogException
7+
* @package danielme85\LaravelLogToDB\Models
8+
*/
9+
class DBLogException extends \Exception
10+
{
11+
12+
}

src/Models/LogToDbCreateObject.php

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,68 @@
99
*/
1010
trait LogToDbCreateObject
1111
{
12+
/**
13+
* Create a new log object
14+
*
15+
* @param array $record
16+
* @param bool $detailed
17+
*
18+
* @return mixed
19+
*/
20+
public function generate(array $record, bool $detailed = false)
21+
{
22+
if (isset($record['message'])) {
23+
$this->message = $record['message'];
24+
}
25+
if ($detailed) {
26+
if (isset($record['context'])) {
27+
if (!empty($record['context'])) {
28+
$this->context = $record['context'];
29+
}
30+
}
31+
}
32+
if (isset($record['level'])) {
33+
$this->level = $record['level'];
34+
}
35+
if (isset($record['level_name'])) {
36+
$this->level_name = $record['level_name'];
37+
}
38+
if (isset($record['channel'])) {
39+
$this->channel = $record['channel'];
40+
}
41+
if (isset($record['datetime'])) {
42+
$this->datetime = $record['datetime'];
43+
}
44+
if (isset($record['extra'])) {
45+
if (!empty($record['extra'])) {
46+
$this->extra = $record['extra'];
47+
}
48+
}
49+
$this->unix_time = time();
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* @param $value
56+
*/
57+
public function setMessageAttribute($value)
58+
{
59+
if (config('logtodb.encrypt')) {
60+
$this->attributes['message'] = encrypt($value);
61+
} else {
62+
$this->attributes['message'] = $value;
63+
}
64+
}
65+
66+
public function getMessageAttribute($value)
67+
{
68+
if (config('logtodb.encrypt')) {
69+
return decrypt($value) ?? $value;
70+
}
71+
72+
return $value;
73+
}
1274
/**
1375
* Context Accessor
1476
*
@@ -47,37 +109,37 @@ public function setContextAttribute(array $value)
47109
if (method_exists($exception, 'getMessage')) {
48110
$newexception['message'] = $exception->getMessage();
49111
} else {
50-
$newexception['message'] = '';
112+
$newexception['message'] = null;
51113
}
52114
if (method_exists($exception, 'getCode')) {
53115
$newexception['code'] = $exception->getCode();
54116
} else {
55-
$newexception['code'] = '';
117+
$newexception['code'] = null;
56118
}
57119
if (method_exists($exception, 'getFile')) {
58120
$newexception['file'] = $exception->getFile();
59121
} else {
60-
$newexception['file'] = '';
122+
$newexception['file'] = null;
61123
}
62124
if (method_exists($exception, 'getLine')) {
63125
$newexception['line'] = $exception->getLine();
64126
} else {
65-
$newexception['line'] = '';
127+
$newexception['line'] = null;
66128
}
67129
if (method_exists($exception, 'getTrace')) {
68130
$newexception['trace'] = $exception->getTrace();
69131
} else {
70-
$newexception['trace'] = '';
132+
$newexception['trace'] = null;
71133
}
72134
if (method_exists($exception, 'getPrevious')) {
73135
$newexception['previous'] = $exception->getPrevious();
74136
} else {
75-
$newexception['previous'] = '';
137+
$newexception['previous'] = null;
76138
}
77139
if (method_exists($exception, 'getSeverity')) {
78140
$newexception['severity'] = $exception->getSeverity();
79141
} else {
80-
$newexception['severity'] = '';
142+
$newexception['severity'] = null;
81143
}
82144

83145
$value['exception'] = $newexception;

tests/LogToDbTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public function testStandAloneModels()
368368
public function testCustomModel()
369369
{
370370
config()->set('logtodb.model', \danielme85\LaravelLogToDB\Tests\CustomEloquentModel::class);
371-
$this->expectException(ErrorException::class);
371+
$this->expectException(\danielme85\LaravelLogToDB\Models\DBLogException::class);
372372
$this->expectExceptionMessage('This is on a custom model class');
373373
Log::info('This is on a custom model class');
374374
$this->assertStringContainsString('This is on a custom model class', LogToDB::model()->latest('id')->first()->message);

0 commit comments

Comments
 (0)