-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathErrorGovernor.php
More file actions
47 lines (38 loc) · 1.12 KB
/
ErrorGovernor.php
File metadata and controls
47 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Stackify\Log\Filters;
use Stackify\Log\Entities\Api\LogMsg;
use Stackify\Log\Entities\Api\ErrorItem;
use Stackify\Log\Entities\Api\StackifyError;
class ErrorGovernor
{
const MAX_DUPLICATES = 100;
private $counter = array();
public function shouldBeSent(LogMsg $logMsg = null)
{
if (!isset($logMsg->Ex)) {
return true;
}
$errorItem = $this->getBaseError($logMsg->Ex);
$key = $this->getUniqueKey($errorItem);
if (!isset($this->counter[$key])) {
$this->counter[$key] = 0;
}
return (++$this->counter[$key] <= self::MAX_DUPLICATES);
}
/**
* @return \Stackify\Log\Entities\Api\ErrorItem
*/
private function getBaseError(StackifyError $error)
{
$errorItem = $error->Error;
while (null !== $errorItem->InnerError) {
$errorItem = $errorItem->InnerError;
}
return $errorItem;
}
private function getUniqueKey(ErrorItem $item)
{
$key = sprintf('%s-%s-%s', $item->ErrorType, $item->ErrorTypeCode, $item->SourceMethod);
return md5($key);
}
}