Skip to content

Commit

Permalink
Fix issue where context array was only json encoded in cases where it…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
danielme85 committed Jun 5, 2020
1 parent 77ba74a commit ada71d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/LogToDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function getConfig(string $config)
* Parse the exception class
*
* @param mixed $context
* @return mixed
* @return array
*/
private function parseIfException($context)
{
Expand Down Expand Up @@ -237,11 +237,7 @@ private function parseIfException($context)
}
}

if (!empty($context)) {
return json_encode($context);
}

return null;
return $context;
}

}
10 changes: 10 additions & 0 deletions src/Models/LogToDbCreateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public function setDatetimeAttribute(object $value)
$this->attributes['datetime'] = $value->format(config('logtodb.datetime_format'));
}

/**
* Context Mutator
*
* @param array $value
*/
public function setContextAttribute($value)
{
$this->attributes['context'] = $this->jsonEncodeIfNotEmpty($value);
}

/**
* Extra Mutator
*
Expand Down
12 changes: 12 additions & 0 deletions tests/LogToDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ public function testMessageLoggedEvent()
Log::debug("This is to trigger a log event.");
}

/**
* @group context
*/
public function testContext()
{
Log::error("Im trying to add some context", ['whatDis?' => 'dis some context, should always be array']);
$log = LogToDB::model()->where('message', '=' , 'Im trying to add some context')->first();
$this->assertNotEmpty($log);
$this->assertStringContainsString("Im trying to add some context", $log->message);
$this->assertIsArray($log->context);
}

/**
* Check to see if processors are adding extra content.
*
Expand Down

0 comments on commit ada71d9

Please sign in to comment.