Skip to content

Commit 77ba74a

Browse files
committed
Better exception handling, hopefully less exception loops when there is an exception logging the exception 😅
Reverting context->exception->trace back ot getTrace() vs getTraceAsString().
1 parent af17616 commit 77ba74a

File tree

5 files changed

+72
-73
lines changed

5 files changed

+72
-73
lines changed

src/Jobs/SaveNewLogEvent.php

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

55
use danielme85\LaravelLogToDB\Models\CreateLogFromRecord;
6+
use danielme85\LaravelLogToDB\Models\DBLogException;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Queue\InteractsWithQueue;
89
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -26,7 +27,7 @@ class SaveNewLogEvent implements ShouldQueue
2627
* Create a new job instance.
2728
*
2829
* @param object $logToDb
29-
* @param array $record
30+
* @param array $record
3031
* @return void
3132
*/
3233
public function __construct($logToDb, $record)
@@ -42,12 +43,15 @@ public function __construct($logToDb, $record)
4243
*/
4344
public function handle()
4445
{
45-
$model = $this->logToDb->getModel();
46-
$log = $model->generate(
47-
$this->record,
48-
$this->logToDb->getConfig('detailed')
49-
);
50-
51-
$log->save();
46+
try {
47+
$model = $this->logToDb->getModel();
48+
$log = $model->generate(
49+
$this->record,
50+
$this->logToDb->getConfig('detailed')
51+
);
52+
$log->save();
53+
} catch (\Exception $e) {
54+
throw new DBLogException($e->getMessage());
55+
}
5256
}
5357
}

src/LogToDB.php

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ public function getModel()
146146
*/
147147
public function newFromMonolog(array $record)
148148
{
149+
$detailed = $this->getConfig('detailed');
150+
149151
if (!empty($this->connection)) {
150-
if (!empty($record['context']) && !empty($record['context']['exception'])) {
151-
$record['context']['exception'] = $this->parseIfException($record['context']['exception']);
152+
if ($detailed && !empty($record['context']) && !empty($record['context']['exception'])) {
153+
$record['context'] = $this->parseIfException($record['context']);
154+
} else if (!$detailed){
155+
$record['context'] = null;
152156
}
153-
154157
if ($this->config['queue']) {
155158
if (empty($this->config['queue_name']) && empty($this->config['queue_connection'])) {
156159
dispatch(new SaveNewLogEvent($this, $record));
@@ -168,8 +171,7 @@ public function newFromMonolog(array $record)
168171
} else {
169172
$model = $this->getModel();
170173
$log = $model->generate(
171-
$record,
172-
$this->getConfig('detailed')
174+
$record
173175
);
174176
if ($log->save()) {
175177
return true;
@@ -194,45 +196,52 @@ public function getConfig(string $config)
194196
/**
195197
* Parse the exception class
196198
*
197-
* @param mixed $exception
199+
* @param mixed $context
198200
* @return mixed
199201
*/
200-
private function parseIfException($exception)
202+
private function parseIfException($context)
201203
{
202-
if (is_object($exception)) {
203-
if (get_class($exception) === \Exception::class
204-
|| is_subclass_of($exception, \Exception::class)
205-
|| strpos(get_class($exception), "Exception") !== false) {
206-
207-
$newexception = [];
208-
209-
if (method_exists($exception, 'getMessage')) {
210-
$newexception['message'] = $exception->getMessage();
211-
}
212-
if (method_exists($exception, 'getCode')) {
213-
$newexception['code'] = $exception->getCode();
214-
}
215-
if (method_exists($exception, 'getFile')) {
216-
$newexception['file'] = $exception->getFile();
217-
}
218-
if (method_exists($exception, 'getLine')) {
219-
$newexception['line'] = $exception->getLine();
204+
if (!empty($context['exception'])) {
205+
$exception = $context['exception'];
206+
if (is_object($exception)) {
207+
if (get_class($exception) === \Exception::class
208+
|| is_subclass_of($exception, \Exception::class)
209+
|| strpos(get_class($exception), "Exception") !== false) {
210+
211+
$newexception = [];
212+
213+
if (method_exists($exception, 'getMessage')) {
214+
$newexception['message'] = $exception->getMessage();
215+
}
216+
if (method_exists($exception, 'getCode')) {
217+
$newexception['code'] = $exception->getCode();
218+
}
219+
if (method_exists($exception, 'getFile')) {
220+
$newexception['file'] = $exception->getFile();
221+
}
222+
if (method_exists($exception, 'getLine')) {
223+
$newexception['line'] = $exception->getLine();
224+
}
225+
if (method_exists($exception, 'getTrace')) {
226+
$newexception['trace'] = $exception->getTrace();
227+
}
228+
if (method_exists($exception, 'getPrevious')) {
229+
$newexception['previous'] = $exception->getPrevious();
230+
}
231+
if (method_exists($exception, 'getSeverity')) {
232+
$newexception['severity'] = $exception->getSeverity();
233+
}
234+
235+
$context['exception'] = $newexception;
220236
}
221-
if (method_exists($exception, 'getTrace')) {
222-
$newexception['trace'] = $exception->getTraceAsString();
223-
}
224-
if (method_exists($exception, 'getPrevious')) {
225-
$newexception['previous'] = $exception->getPrevious();
226-
}
227-
if (method_exists($exception, 'getSeverity')) {
228-
$newexception['severity'] = $exception->getSeverity();
229-
}
230-
231-
$exception = $newexception;
232237
}
233238
}
234239

235-
return $exception;
240+
if (!empty($context)) {
241+
return json_encode($context);
242+
}
243+
244+
return null;
236245
}
237246

238247
}

src/LogToDbCustomLoggingHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ function __construct(array $config,
5555
protected function write(array $record): void
5656
{
5757
if (!empty($record)) {
58-
if (!empty($record['context']['exception'])
59-
&& get_class($record['context']['exception']) === DBLogException::class) {
58+
if (!empty($record['context']['exception']) &&
59+
get_class($record['context']['exception']) === DBLogException::class) {
6060
//Do nothing if empty log record or an error Exception from itself.
6161
} else {
6262
try {
6363
$log = new LogToDB($this->config);
6464
$log->newFromMonolog($record);
65+
} catch (DBLogException $e) {
66+
//do nothing if exception of self
6567
} catch (\Exception $e) {
6668
//convert any runtime Exception while logging to a special class so we can avoid our own
6769
//exceptions for 99% less infinite loops!

src/Models/LogToDbCreateObject.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@ trait LogToDbCreateObject
1313
* Create a new log object
1414
*
1515
* @param array $record
16-
* @param bool $detailed
1716
*
1817
* @return mixed
1918
*/
20-
public function generate(array $record, bool $detailed = false)
19+
public function generate(array $record)
2120
{
2221
if (isset($record['message'])) {
2322
$this->message = $record['message'];
2423
}
25-
if ($detailed) {
26-
if (isset($record['context'])) {
27-
if (!empty($record['context'])) {
28-
$this->context = $record['context'];
29-
}
30-
}
24+
if (!empty($record['context'])) {
25+
$this->context = $record['context'];
3126
}
3227
if (isset($record['level'])) {
3328
$this->level = $record['level'];
@@ -41,10 +36,8 @@ public function generate(array $record, bool $detailed = false)
4136
if (isset($record['datetime'])) {
4237
$this->datetime = $record['datetime'];
4338
}
44-
if (isset($record['extra'])) {
45-
if (!empty($record['extra'])) {
46-
$this->extra = $record['extra'];
47-
}
39+
if (!empty($record['extra'])) {
40+
$this->extra = $record['extra'];
4841
}
4942
$this->unix_time = time();
5043

@@ -73,16 +66,6 @@ public function getExtraAttribute($value)
7366
return $this->jsonDecodeIfNotEmpty($value);
7467
}
7568

76-
/**
77-
* Context Mutator
78-
*
79-
* @param array $value
80-
*/
81-
public function setContextAttribute(array $value)
82-
{
83-
$this->attributes['context'] = $this->jsonEncodeIfNotEmpty($value);
84-
}
85-
8669
/**
8770
* DateTime Mutator
8871
*
@@ -114,8 +97,6 @@ private function jsonEncodeIfNotEmpty($value)
11497
if (!empty($value)) {
11598
return @json_encode($value) ?? null;
11699
}
117-
118-
return $value;
119100
}
120101

121102
/**

tests/LogToDbTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ public function testLoggingToChannels()
205205
{
206206
//Test limited config, with limited rows and level
207207
Log::channel('limited')->debug("This message should not be stored because DEBUG is LOWER then WARNING");
208-
$this->assertEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'DEBUG')->get()->toArray());
208+
$this->assertEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'DEBUG')->get());
209209

210210
//Test limited config, with limited rows and level
211211
Log::channel('limited')->warning("This message should be stored because WARNING = WARNING");
212-
$this->assertNotEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'WARNING')->get()->toArray());
212+
$this->assertNotEmpty(LogToDB::model('limited')->where('channel', 'limited')->where('level_name', 'WARNING')->get());
213+
213214
}
214215

215216
/**
@@ -427,6 +428,8 @@ public function testCustomModel()
427428
*/
428429
public function testRemoves()
429430
{
431+
$this->assertFalse(LogToDB::model()->removeOldestIfMoreThan(1000));
432+
430433
Log::debug("This is an test DEBUG log event");
431434
Log::info("This is an test INFO log event");
432435
Log::notice("This is an test NOTICE log event");

0 commit comments

Comments
 (0)