@@ -32,105 +32,105 @@ |
||
32 | 32 | |
33 | 33 | class EventLogger implements IEventLogger { |
34 | 34 | |
35 | - /** @var Event[] */ |
|
36 | - private $events = []; |
|
37 | - |
|
38 | - /** @var SystemConfig */ |
|
39 | - private $config; |
|
40 | - |
|
41 | - /** @var LoggerInterface */ |
|
42 | - private $logger; |
|
43 | - |
|
44 | - /** @var Log */ |
|
45 | - private $internalLogger; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var bool - Module needs to be activated by some app |
|
49 | - */ |
|
50 | - private $activated = false; |
|
51 | - |
|
52 | - public function __construct(SystemConfig $config, LoggerInterface $logger, Log $internalLogger) { |
|
53 | - $this->config = $config; |
|
54 | - $this->logger = $logger; |
|
55 | - $this->internalLogger = $internalLogger; |
|
56 | - |
|
57 | - if ($this->isLoggingActivated()) { |
|
58 | - $this->activate(); |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - public function isLoggingActivated(): bool { |
|
63 | - $systemValue = (bool)$this->config->getValue('diagnostics.logging', false); |
|
64 | - |
|
65 | - if ($systemValue && $this->config->getValue('debug', false)) { |
|
66 | - return true; |
|
67 | - } |
|
68 | - |
|
69 | - $isDebugLevel = $this->internalLogger->getLogLevel([]) === Log::DEBUG; |
|
70 | - return $systemValue && $isDebugLevel; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @inheritdoc |
|
75 | - */ |
|
76 | - public function start($id, $description = '') { |
|
77 | - if ($this->activated) { |
|
78 | - $this->events[$id] = new Event($id, $description, microtime(true)); |
|
79 | - $this->writeLog($this->events[$id]); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @inheritdoc |
|
85 | - */ |
|
86 | - public function end($id) { |
|
87 | - if ($this->activated && isset($this->events[$id])) { |
|
88 | - $timing = $this->events[$id]; |
|
89 | - $timing->end(microtime(true)); |
|
90 | - $this->writeLog($timing); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @inheritdoc |
|
96 | - */ |
|
97 | - public function log($id, $description, $start, $end) { |
|
98 | - if ($this->activated) { |
|
99 | - $this->events[$id] = new Event($id, $description, $start); |
|
100 | - $this->events[$id]->end($end); |
|
101 | - $this->writeLog($this->events[$id]); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @inheritdoc |
|
107 | - */ |
|
108 | - public function getEvents() { |
|
109 | - return $this->events; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @inheritdoc |
|
114 | - */ |
|
115 | - public function activate() { |
|
116 | - $this->activated = true; |
|
117 | - } |
|
118 | - |
|
119 | - private function writeLog(IEvent $event) { |
|
120 | - if ($this->activated) { |
|
121 | - if ($event->getEnd() === null) { |
|
122 | - return; |
|
123 | - } |
|
124 | - $duration = $event->getDuration(); |
|
125 | - $timeInMs = round($duration * 1000, 4); |
|
126 | - |
|
127 | - $loggingMinimum = (int)$this->config->getValue('diagnostics.logging.threshold', 0); |
|
128 | - if ($loggingMinimum > 0 && $timeInMs < $loggingMinimum) { |
|
129 | - return; |
|
130 | - } |
|
131 | - |
|
132 | - $message = microtime() . ' - ' . $event->getId() . ': ' . $timeInMs . ' (' . $event->getDescription() . ')'; |
|
133 | - $this->logger->debug($message, ['app' => 'diagnostics']); |
|
134 | - } |
|
135 | - } |
|
35 | + /** @var Event[] */ |
|
36 | + private $events = []; |
|
37 | + |
|
38 | + /** @var SystemConfig */ |
|
39 | + private $config; |
|
40 | + |
|
41 | + /** @var LoggerInterface */ |
|
42 | + private $logger; |
|
43 | + |
|
44 | + /** @var Log */ |
|
45 | + private $internalLogger; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var bool - Module needs to be activated by some app |
|
49 | + */ |
|
50 | + private $activated = false; |
|
51 | + |
|
52 | + public function __construct(SystemConfig $config, LoggerInterface $logger, Log $internalLogger) { |
|
53 | + $this->config = $config; |
|
54 | + $this->logger = $logger; |
|
55 | + $this->internalLogger = $internalLogger; |
|
56 | + |
|
57 | + if ($this->isLoggingActivated()) { |
|
58 | + $this->activate(); |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + public function isLoggingActivated(): bool { |
|
63 | + $systemValue = (bool)$this->config->getValue('diagnostics.logging', false); |
|
64 | + |
|
65 | + if ($systemValue && $this->config->getValue('debug', false)) { |
|
66 | + return true; |
|
67 | + } |
|
68 | + |
|
69 | + $isDebugLevel = $this->internalLogger->getLogLevel([]) === Log::DEBUG; |
|
70 | + return $systemValue && $isDebugLevel; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @inheritdoc |
|
75 | + */ |
|
76 | + public function start($id, $description = '') { |
|
77 | + if ($this->activated) { |
|
78 | + $this->events[$id] = new Event($id, $description, microtime(true)); |
|
79 | + $this->writeLog($this->events[$id]); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @inheritdoc |
|
85 | + */ |
|
86 | + public function end($id) { |
|
87 | + if ($this->activated && isset($this->events[$id])) { |
|
88 | + $timing = $this->events[$id]; |
|
89 | + $timing->end(microtime(true)); |
|
90 | + $this->writeLog($timing); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @inheritdoc |
|
96 | + */ |
|
97 | + public function log($id, $description, $start, $end) { |
|
98 | + if ($this->activated) { |
|
99 | + $this->events[$id] = new Event($id, $description, $start); |
|
100 | + $this->events[$id]->end($end); |
|
101 | + $this->writeLog($this->events[$id]); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @inheritdoc |
|
107 | + */ |
|
108 | + public function getEvents() { |
|
109 | + return $this->events; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @inheritdoc |
|
114 | + */ |
|
115 | + public function activate() { |
|
116 | + $this->activated = true; |
|
117 | + } |
|
118 | + |
|
119 | + private function writeLog(IEvent $event) { |
|
120 | + if ($this->activated) { |
|
121 | + if ($event->getEnd() === null) { |
|
122 | + return; |
|
123 | + } |
|
124 | + $duration = $event->getDuration(); |
|
125 | + $timeInMs = round($duration * 1000, 4); |
|
126 | + |
|
127 | + $loggingMinimum = (int)$this->config->getValue('diagnostics.logging.threshold', 0); |
|
128 | + if ($loggingMinimum > 0 && $timeInMs < $loggingMinimum) { |
|
129 | + return; |
|
130 | + } |
|
131 | + |
|
132 | + $message = microtime() . ' - ' . $event->getId() . ': ' . $timeInMs . ' (' . $event->getDescription() . ')'; |
|
133 | + $this->logger->debug($message, ['app' => 'diagnostics']); |
|
134 | + } |
|
135 | + } |
|
136 | 136 | } |