Skip to content

Commit 32dda6e

Browse files
committed
WIP: Replace more ILogger method calls
This commit replaces more ILogger method calls with `Psr\Log\LoggerInterface` as we gradually move away from the custom ILogger. Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
1 parent 8ee52d3 commit 32dda6e

File tree

7 files changed

+37
-97
lines changed

7 files changed

+37
-97
lines changed

core/Command/Log/Manage.php

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
class Manage extends Command implements CompletionAwareInterface {
3838
public const DEFAULT_BACKEND = 'file';
39-
public const DEFAULT_LOG_LEVEL = 2;
39+
public const DEFAULT_LOG_LEVEL = 'warning';
4040
public const DEFAULT_TIMEZONE = 'UTC';
4141

4242
protected IConfig $config;
@@ -82,14 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8282

8383
$level = $input->getOption('level');
8484
if ($level !== null) {
85-
if (is_numeric($level)) {
86-
$levelNum = $level;
87-
// sanity check
88-
$this->convertLevelNumber($levelNum);
89-
} else {
90-
$levelNum = $this->convertLevelString($level);
91-
}
92-
$toBeSet['loglevel'] = $levelNum;
85+
$toBeSet['loglevel'] = $level;
9386
}
9487

9588
if ($timezone = $input->getOption('timezone')) {
@@ -106,9 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10699
$backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND);
107100
$output->writeln('Enabled logging backend: '.$backend);
108101

109-
$levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
110-
$level = $this->convertLevelNumber($levelNum);
111-
$output->writeln('Log level: '.$level.' ('.$levelNum.')');
102+
$level = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
103+
$output->writeln('Log level: '.$level.' ('.$level.')');
112104

113105
$timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE);
114106
$output->writeln('Log timezone: '.$timezone);
@@ -133,51 +125,6 @@ protected function validateTimezone($timezone) {
133125
new \DateTimeZone($timezone);
134126
}
135127

136-
/**
137-
* @param string $level
138-
* @return int
139-
* @throws \InvalidArgumentException
140-
*/
141-
protected function convertLevelString($level) {
142-
$level = strtolower($level);
143-
switch ($level) {
144-
case 'debug':
145-
return 0;
146-
case 'info':
147-
return 1;
148-
case 'warning':
149-
case 'warn':
150-
return 2;
151-
case 'error':
152-
case 'err':
153-
return 3;
154-
case 'fatal':
155-
return 4;
156-
}
157-
throw new \InvalidArgumentException('Invalid log level string');
158-
}
159-
160-
/**
161-
* @param int $levelNum
162-
* @return string
163-
* @throws \InvalidArgumentException
164-
*/
165-
protected function convertLevelNumber($levelNum) {
166-
switch ($levelNum) {
167-
case 0:
168-
return 'Debug';
169-
case 1:
170-
return 'Info';
171-
case 2:
172-
return 'Warning';
173-
case 3:
174-
return 'Error';
175-
case 4:
176-
return 'Fatal';
177-
}
178-
throw new \InvalidArgumentException('Invalid log level number');
179-
}
180-
181128
/**
182129
* @param string $optionName
183130
* @param CompletionContext $context

lib/base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@
7272
use OC\Share20\Hooks;
7373
use OCP\EventDispatcher\IEventDispatcher;
7474
use OCP\Group\Events\UserRemovedEvent;
75-
use OCP\ILogger;
7675
use OCP\IRequest;
7776
use OCP\IURLGenerator;
7877
use OCP\IUserSession;
7978
use OCP\Server;
8079
use OCP\Share;
8180
use OCP\User\Events\UserChangedEvent;
8281
use Psr\Log\LoggerInterface;
82+
use Psr\Log\LogLevel;
8383
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
8484
use function OCP\Log\logger;
8585

@@ -628,7 +628,7 @@ public static function init(): void {
628628
$eventLogger->start('boot', 'Initialize');
629629

630630
// Override php.ini and log everything if we're troubleshooting
631-
if (self::$config->getValue('loglevel') === ILogger::DEBUG) {
631+
if (self::$config->getValue('loglevel') === LogLevel::DEBUG) {
632632
error_reporting(E_ALL);
633633
}
634634

lib/private/Log.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
use OCP\Log\IFileBased;
4848
use OCP\Log\IWriter;
4949
use OCP\Support\CrashReport\IRegistry;
50+
use Psr\Log\LoggerInterface;
51+
use Psr\Log\LogLevel;
52+
5053
use function strtr;
5154

5255
/**
@@ -58,7 +61,7 @@
5861
*
5962
* MonoLog is an example implementing this interface.
6063
*/
61-
class Log implements ILogger, IDataLogger {
64+
class Log implements LoggerInterface, IDataLogger {
6265
private IWriter $logger;
6366
private ?SystemConfig $config;
6467
private ?bool $logConditionSatisfied = null;
@@ -94,8 +97,8 @@ public function __construct(IWriter $logger, SystemConfig $config = null, Normal
9497
* @param array $context
9598
* @return void
9699
*/
97-
public function emergency(string $message, array $context = []) {
98-
$this->log(ILogger::FATAL, $message, $context);
100+
public function emergency($message, array $context = []) {
101+
$this->log(LogLevel::ERROR, $message, $context);
99102
}
100103

101104
/**
@@ -108,8 +111,8 @@ public function emergency(string $message, array $context = []) {
108111
* @param array $context
109112
* @return void
110113
*/
111-
public function alert(string $message, array $context = []) {
112-
$this->log(ILogger::ERROR, $message, $context);
114+
public function alert($message, array $context = []) {
115+
$this->log(LogLevel::ERROR, $message, $context);
113116
}
114117

115118
/**
@@ -121,8 +124,8 @@ public function alert(string $message, array $context = []) {
121124
* @param array $context
122125
* @return void
123126
*/
124-
public function critical(string $message, array $context = []) {
125-
$this->log(ILogger::ERROR, $message, $context);
127+
public function critical($message, array $context = []) {
128+
$this->log(LogLevel::ERROR, $message, $context);
126129
}
127130

128131
/**
@@ -133,8 +136,8 @@ public function critical(string $message, array $context = []) {
133136
* @param array $context
134137
* @return void
135138
*/
136-
public function error(string $message, array $context = []) {
137-
$this->log(ILogger::ERROR, $message, $context);
139+
public function error($message, array $context = []) {
140+
$this->log(LogLevel::ERROR, $message, $context);
138141
}
139142

140143
/**
@@ -147,8 +150,8 @@ public function error(string $message, array $context = []) {
147150
* @param array $context
148151
* @return void
149152
*/
150-
public function warning(string $message, array $context = []) {
151-
$this->log(ILogger::WARN, $message, $context);
153+
public function warning($message, array $context = []) {
154+
$this->log(LogLevel::WARN, $message, $context);
152155
}
153156

154157
/**
@@ -158,8 +161,8 @@ public function warning(string $message, array $context = []) {
158161
* @param array $context
159162
* @return void
160163
*/
161-
public function notice(string $message, array $context = []) {
162-
$this->log(ILogger::INFO, $message, $context);
164+
public function notice($message, array $context = []) {
165+
$this->log(LogLevel::INFO, $message, $context);
163166
}
164167

165168
/**
@@ -171,8 +174,8 @@ public function notice(string $message, array $context = []) {
171174
* @param array $context
172175
* @return void
173176
*/
174-
public function info(string $message, array $context = []) {
175-
$this->log(ILogger::INFO, $message, $context);
177+
public function info($message, array $context = []) {
178+
$this->log(LogLevel::INFO, $message, $context);
176179
}
177180

178181
/**
@@ -182,8 +185,8 @@ public function info(string $message, array $context = []) {
182185
* @param array $context
183186
* @return void
184187
*/
185-
public function debug(string $message, array $context = []) {
186-
$this->log(ILogger::DEBUG, $message, $context);
188+
public function debug($message, array $context = []) {
189+
$this->log(LogLevel::DEBUG, $message, $context);
187190
}
188191

189192

@@ -195,7 +198,7 @@ public function debug(string $message, array $context = []) {
195198
* @param array $context
196199
* @return void
197200
*/
198-
public function log(int $level, string $message, array $context = []) {
201+
public function log($level, $message, array $context = []) {
199202
$minLevel = $this->getLogLevel($context);
200203

201204
array_walk($context, [$this->normalizer, 'format']);
@@ -269,7 +272,7 @@ public function getLogLevel($context) {
269272

270273
// if log condition is satisfied change the required log level to DEBUG
271274
if ($this->logConditionSatisfied) {
272-
return ILogger::DEBUG;
275+
return LogLevel::DEBUG;
273276
}
274277

275278
if (isset($context['app'])) {
@@ -282,11 +285,11 @@ public function getLogLevel($context) {
282285
if (!empty($logCondition)
283286
&& isset($logCondition['apps'])
284287
&& in_array($app, $logCondition['apps'], true)) {
285-
return ILogger::DEBUG;
288+
return LogLevel::DEBUG;
286289
}
287290
}
288291

289-
return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL);
292+
return min($this->config->getValue('loglevel', LogLevel::WARNING), LogLevel::ERROR);
290293
}
291294

292295
/**
@@ -299,7 +302,7 @@ public function getLogLevel($context) {
299302
*/
300303
public function logException(Throwable $exception, array $context = []) {
301304
$app = $context['app'] ?? 'no app in context';
302-
$level = $context['level'] ?? ILogger::ERROR;
305+
$level = $context['level'] ?? LogLevel::ERROR;
303306

304307
$minLevel = $this->getLogLevel($context);
305308
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) {

lib/private/Template/JSConfigHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
use OCP\IL10N;
4848
use OCP\ISession;
4949
use OCP\IURLGenerator;
50-
use OCP\ILogger;
50+
use Psr\Log\LogLevel;
5151
use OCP\IUser;
5252
use OCP\User\Backend\IPasswordConfirmationBackend;
5353
use OCP\Util;
@@ -169,7 +169,7 @@ public function getConfig(): string {
169169
'auto_logout' => $this->config->getSystemValue('auto_logout', false),
170170
'blacklist_files_regex' => FileInfo::BLACKLIST_FILES_REGEX,
171171
'loglevel' => $this->config->getSystemValue('loglevel_frontend',
172-
$this->config->getSystemValue('loglevel', ILogger::WARN)
172+
$this->config->getSystemValue('loglevel', LogLevel::WARNING)
173173
),
174174
'lost_password_link' => $this->config->getSystemValue('lost_password_link', null),
175175
'modRewriteWorking' => $this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true',

lib/private/Updater.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use OCP\EventDispatcher\IEventDispatcher;
4646
use OCP\HintException;
4747
use OCP\IConfig;
48-
use OCP\ILogger;
4948
use OCP\Util;
5049
use OC\App\AppManager;
5150
use OC\DB\Connection;
@@ -62,6 +61,7 @@
6261
use OC\Repair\Events\RepairWarningEvent;
6362
use OC_App;
6463
use Psr\Log\LoggerInterface;
64+
use Psr\Log\LogLevel;
6565

6666
/**
6767
* Class that handles autoupdating of ownCloud
@@ -112,9 +112,9 @@ public function __construct(IConfig $config,
112112
public function upgrade(): bool {
113113
$this->logAllEvents();
114114

115-
$logLevel = $this->config->getSystemValue('loglevel', ILogger::WARN);
115+
$logLevel = $this->config->getSystemValue('loglevel', LogLevel::WARNING);
116116
$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
117-
$this->config->setSystemValue('loglevel', ILogger::DEBUG);
117+
$this->config->setSystemValue('loglevel', LogLevel::DEBUG);
118118

119119
$wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
120120

lib/public/Log/ILogFactory.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
namespace OCP\Log;
2626

27-
use OCP\ILogger;
2827
use Psr\Log\LoggerInterface;
2928

3029
/**
@@ -40,15 +39,6 @@ interface ILogFactory {
4039
*/
4140
public function get(string $type): IWriter;
4241

43-
/**
44-
* @param string $path
45-
* @return ILogger
46-
* @since 14.0.0
47-
* @deprecated use \OCP\Log\ILogFactory::getCustomPsrLogger
48-
* @see \OCP\Log\ILogFactory::getCustomPsrLogger
49-
*/
50-
public function getCustomLogger(string $path): ILogger;
51-
5242
/**
5343
* @param string $path
5444
* @param string $type

tests/travis/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ cat > ./tests/autoconfig-oracle.php <<DELIM
9898
'dbname' => 'XE',
9999
'dbhost' => 'localhost',
100100
'dbpass' => 'owncloud',
101-
'loglevel' => 0,
101+
'loglevel' => 'warning',
102102
);
103103
DELIM
104104

0 commit comments

Comments
 (0)