Skip to content

Commit e2f08d9

Browse files
authored
Merge pull request #43534 from nextcloud/enh/noid/log-mnor-syntax-update
2 parents 836c882 + 618aadd commit e2f08d9

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

lib/private/Log.php

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,22 @@
6262
* MonoLog is an example implementing this interface.
6363
*/
6464
class Log implements ILogger, IDataLogger {
65-
private ?SystemConfig $config;
6665
private ?bool $logConditionSatisfied = null;
67-
private ?Normalizer $normalizer;
68-
private ?IEventDispatcher $eventDispatcher;
66+
private ?IEventDispatcher $eventDispatcher = null;
6967

70-
/**
71-
* @param IWriter $logger The logger that should be used
72-
* @param SystemConfig|null $config the system config object
73-
* @param Normalizer|null $normalizer
74-
* @param IRegistry|null $crashReporters
75-
*/
7668
public function __construct(
7769
private IWriter $logger,
78-
SystemConfig $config = null,
79-
Normalizer $normalizer = null,
80-
private ?IRegistry $crashReporters = null
70+
private SystemConfig $config,
71+
private ?Normalizer $normalizer = null,
72+
private ?IRegistry $crashReporters = null
8173
) {
82-
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
83-
if ($config === null) {
84-
$config = \OC::$server->getSystemConfig();
85-
}
86-
87-
$this->config = $config;
74+
// FIXME: php8.1 allows "private Normalizer $normalizer = new Normalizer()," in initializer
8875
if ($normalizer === null) {
8976
$this->normalizer = new Normalizer();
90-
} else {
91-
$this->normalizer = $normalizer;
9277
}
93-
$this->eventDispatcher = null;
9478
}
9579

96-
public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
80+
public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
9781
$this->eventDispatcher = $eventDispatcher;
9882
}
9983

@@ -102,9 +86,8 @@ public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
10286
*
10387
* @param string $message
10488
* @param array $context
105-
* @return void
10689
*/
107-
public function emergency(string $message, array $context = []) {
90+
public function emergency(string $message, array $context = []): void {
10891
$this->log(ILogger::FATAL, $message, $context);
10992
}
11093

@@ -116,9 +99,8 @@ public function emergency(string $message, array $context = []) {
11699
*
117100
* @param string $message
118101
* @param array $context
119-
* @return void
120102
*/
121-
public function alert(string $message, array $context = []) {
103+
public function alert(string $message, array $context = []): void {
122104
$this->log(ILogger::ERROR, $message, $context);
123105
}
124106

@@ -129,9 +111,8 @@ public function alert(string $message, array $context = []) {
129111
*
130112
* @param string $message
131113
* @param array $context
132-
* @return void
133114
*/
134-
public function critical(string $message, array $context = []) {
115+
public function critical(string $message, array $context = []): void {
135116
$this->log(ILogger::ERROR, $message, $context);
136117
}
137118

@@ -141,9 +122,8 @@ public function critical(string $message, array $context = []) {
141122
*
142123
* @param string $message
143124
* @param array $context
144-
* @return void
145125
*/
146-
public function error(string $message, array $context = []) {
126+
public function error(string $message, array $context = []): void {
147127
$this->log(ILogger::ERROR, $message, $context);
148128
}
149129

@@ -155,9 +135,8 @@ public function error(string $message, array $context = []) {
155135
*
156136
* @param string $message
157137
* @param array $context
158-
* @return void
159138
*/
160-
public function warning(string $message, array $context = []) {
139+
public function warning(string $message, array $context = []): void {
161140
$this->log(ILogger::WARN, $message, $context);
162141
}
163142

@@ -166,9 +145,8 @@ public function warning(string $message, array $context = []) {
166145
*
167146
* @param string $message
168147
* @param array $context
169-
* @return void
170148
*/
171-
public function notice(string $message, array $context = []) {
149+
public function notice(string $message, array $context = []): void {
172150
$this->log(ILogger::INFO, $message, $context);
173151
}
174152

@@ -179,9 +157,8 @@ public function notice(string $message, array $context = []) {
179157
*
180158
* @param string $message
181159
* @param array $context
182-
* @return void
183160
*/
184-
public function info(string $message, array $context = []) {
161+
public function info(string $message, array $context = []): void {
185162
$this->log(ILogger::INFO, $message, $context);
186163
}
187164

@@ -190,9 +167,8 @@ public function info(string $message, array $context = []) {
190167
*
191168
* @param string $message
192169
* @param array $context
193-
* @return void
194170
*/
195-
public function debug(string $message, array $context = []) {
171+
public function debug(string $message, array $context = []): void {
196172
$this->log(ILogger::DEBUG, $message, $context);
197173
}
198174

@@ -203,9 +179,8 @@ public function debug(string $message, array $context = []) {
203179
* @param int $level
204180
* @param string $message
205181
* @param array $context
206-
* @return void
207182
*/
208-
public function log(int $level, string $message, array $context = []) {
183+
public function log(int $level, string $message, array $context = []): void {
209184
$minLevel = $this->getLogLevel($context);
210185
if ($level < $minLevel
211186
&& (($this->crashReporters?->hasReporters() ?? false) === false)
@@ -247,7 +222,7 @@ public function log(int $level, string $message, array $context = []) {
247222
}
248223
}
249224

250-
public function getLogLevel($context) {
225+
public function getLogLevel($context): int {
251226
$logCondition = $this->config->getValue('log.condition', []);
252227

253228
/**
@@ -297,20 +272,16 @@ public function getLogLevel($context) {
297272
}
298273

299274
if (isset($context['app'])) {
300-
$app = $context['app'];
301-
302275
/**
303276
* check log condition based on the context of each log message
304277
* once this is met -> change the required log level to debug
305278
*/
306-
if (!empty($logCondition)
307-
&& isset($logCondition['apps'])
308-
&& in_array($app, $logCondition['apps'], true)) {
279+
if (in_array($context['app'], $logCondition['apps'] ?? [], true)) {
309280
return ILogger::DEBUG;
310281
}
311282
}
312283

313-
return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL);
284+
return min($this->config->getValue('loglevel', ILogger::WARN) ?? ILogger::WARN, ILogger::FATAL);
314285
}
315286

316287
/**
@@ -321,7 +292,7 @@ public function getLogLevel($context) {
321292
* @return void
322293
* @since 8.2.0
323294
*/
324-
public function logException(Throwable $exception, array $context = []) {
295+
public function logException(Throwable $exception, array $context = []): void {
325296
$app = $context['app'] ?? 'no app in context';
326297
$level = $context['level'] ?? ILogger::ERROR;
327298

@@ -395,7 +366,7 @@ public function logData(string $message, array $data, array $context = []): void
395366
* @param string|array $entry
396367
* @param int $level
397368
*/
398-
protected function writeLog(string $app, $entry, int $level) {
369+
protected function writeLog(string $app, $entry, int $level): void {
399370
$this->logger->write($app, $entry, $level);
400371
}
401372

0 commit comments

Comments
 (0)