1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Abacaphiliac\Doctrine; |
4
|
|
|
|
5
|
|
|
class LogLevelConfiguration |
6
|
|
|
{ |
7
|
|
|
/** @var array */ |
8
|
|
|
private $logLevelMapping = []; |
9
|
|
|
|
10
|
|
|
public function __construct(array $logLevelMapping) |
11
|
|
|
{ |
12
|
|
|
foreach ($logLevelMapping as $logLevelPriority => $durationThresholdInMilliseconds) { |
13
|
|
|
$this->addLogLevelThreshold($logLevelPriority, $durationThresholdInMilliseconds); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
private function addLogLevelThreshold(string $logLevelPriority, int $durationThresholdInMilliseconds) : void |
18
|
|
|
{ |
19
|
|
|
$this->logLevelMapping[$logLevelPriority] = $durationThresholdInMilliseconds; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getApplicableLogLevel(float $durationInSeconds): ?string |
23
|
|
|
{ |
24
|
|
|
return count($this->logLevelMapping) > 0 ? $this->determineApplicableLogLevel($durationInSeconds) : null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function determineApplicableLogLevel(float $durationInSeconds) : string |
28
|
|
|
{ |
29
|
|
|
$durationInMilliseconds = $durationInSeconds * 1000; |
30
|
|
|
|
31
|
|
|
//Acquire a common / non-associative array with all the thresholds |
32
|
|
|
$durationThresholds = array_values($this->logLevelMapping); |
33
|
|
|
|
34
|
|
|
//Append the incoming query duration in milliseconds to the array of duration thresholds |
35
|
|
|
$durationThresholds[] = $durationInMilliseconds; |
36
|
|
|
|
37
|
|
|
//Sort the array from low to high: the provided duration will end up somewhere between the thresholds |
38
|
|
|
asort($durationThresholds, SORT_NUMERIC); |
39
|
|
|
|
40
|
|
|
//A re-index is required after sorting |
41
|
|
|
$durationThresholds = array_values($durationThresholds); |
42
|
|
|
|
43
|
|
|
//Determine at which position the duration ended up after sorting |
44
|
|
|
$key = array_search($durationInMilliseconds, $durationThresholds, true); |
45
|
|
|
|
46
|
|
|
$logLevels = array_keys($this->logLevelMapping); |
47
|
|
|
|
48
|
|
|
return $logLevels[$key - 1]; //Now take the "previous" key |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|