Skip to content

Commit ada71d9

Browse files
committed
Fix issue where context array was only json encoded in cases where it contained an exception.
Moved json encoding to the model layer and should now run on any user or system supplied array as should be expected behavior.
1 parent 77ba74a commit ada71d9

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/LogToDB.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function getConfig(string $config)
197197
* Parse the exception class
198198
*
199199
* @param mixed $context
200-
* @return mixed
200+
* @return array
201201
*/
202202
private function parseIfException($context)
203203
{
@@ -237,11 +237,7 @@ private function parseIfException($context)
237237
}
238238
}
239239

240-
if (!empty($context)) {
241-
return json_encode($context);
242-
}
243-
244-
return null;
240+
return $context;
245241
}
246242

247243
}

src/Models/LogToDbCreateObject.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public function setDatetimeAttribute(object $value)
7676
$this->attributes['datetime'] = $value->format(config('logtodb.datetime_format'));
7777
}
7878

79+
/**
80+
* Context Mutator
81+
*
82+
* @param array $value
83+
*/
84+
public function setContextAttribute($value)
85+
{
86+
$this->attributes['context'] = $this->jsonEncodeIfNotEmpty($value);
87+
}
88+
7989
/**
8090
* Extra Mutator
8191
*

tests/LogToDbTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ public function testMessageLoggedEvent()
182182
Log::debug("This is to trigger a log event.");
183183
}
184184

185+
/**
186+
* @group context
187+
*/
188+
public function testContext()
189+
{
190+
Log::error("Im trying to add some context", ['whatDis?' => 'dis some context, should always be array']);
191+
$log = LogToDB::model()->where('message', '=' , 'Im trying to add some context')->first();
192+
$this->assertNotEmpty($log);
193+
$this->assertStringContainsString("Im trying to add some context", $log->message);
194+
$this->assertIsArray($log->context);
195+
}
196+
185197
/**
186198
* Check to see if processors are adding extra content.
187199
*

0 commit comments

Comments
 (0)