Passed
Push — master ( 18bafe...2ff0c9 )
by Julius
14:52 queued 13s
created

EventLogger::isLoggingActivated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Piotr Mrówczyński <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OC\Diagnostics;
26
27
use OC\Log;
28
use OC\SystemConfig;
29
use OCP\Diagnostics\IEvent;
30
use OCP\Diagnostics\IEventLogger;
31
use Psr\Log\LoggerInterface;
32
33
class EventLogger implements IEventLogger {
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
		if ($this->config->getValue('debug', false)) {
64
			return true;
65
		}
66
67
		$isDebugLevel = $this->internalLogger->getLogLevel([]) === Log::DEBUG;
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
		$isDebugLevel = $this->internalLogger->getLogLevel([]) === /** @scrutinizer ignore-deprecated */ Log::DEBUG;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
68
		$systemValue = (bool)$this->config->getValue('diagnostics.logging', false);
69
		return $systemValue && $isDebugLevel;
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75
	public function start($id, $description = '') {
76
		if ($this->activated) {
77
			$this->events[$id] = new Event($id, $description, microtime(true));
78
			$this->writeLog($this->events[$id]);
79
		}
80
	}
81
82
	/**
83
	 * @inheritdoc
84
	 */
85
	public function end($id) {
86
		if ($this->activated && isset($this->events[$id])) {
87
			$timing = $this->events[$id];
88
			$timing->end(microtime(true));
89
			$this->writeLog($timing);
90
		}
91
	}
92
93
	/**
94
	 * @inheritdoc
95
	 */
96
	public function log($id, $description, $start, $end) {
97
		if ($this->activated) {
98
			$this->events[$id] = new Event($id, $description, $start);
99
			$this->events[$id]->end($end);
100
			$this->writeLog($this->events[$id]);
101
		}
102
	}
103
104
	/**
105
	 * @inheritdoc
106
	 */
107
	public function getEvents() {
108
		return $this->events;
109
	}
110
111
	/**
112
	 * @inheritdoc
113
	 */
114
	public function activate() {
115
		$this->activated = true;
116
	}
117
118
	private function writeLog(IEvent $event) {
119
		if ($this->activated) {
120
			if ($event->getEnd() === null) {
0 ignored issues
show
introduced by
The condition $event->getEnd() === null is always false.
Loading history...
121
				return;
122
			}
123
			$duration = $event->getDuration();
124
			$timeInMs = round($duration * 1000, 4);
125
126
			$loggingMinimum = (int)$this->config->getValue('diagnostics.logging.threshold', 0);
127
			if ($loggingMinimum > 0 && $timeInMs < $loggingMinimum) {
128
				return;
129
			}
130
131
			$message = microtime() . ' - ' . $event->getId() . ': ' . $timeInMs . ' (' . $event->getDescription() . ')';
132
			$this->logger->debug($message, ['app' => 'diagnostics']);
133
		}
134
	}
135
}
136