Skip to content

Commit 77f5804

Browse files
committed
Refactor OC\Server::getLogger
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
1 parent 613cd16 commit 77f5804

File tree

26 files changed

+111
-50
lines changed

26 files changed

+111
-50
lines changed

core/ajax/update.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use OCP\IEventSource;
3636
use OCP\IEventSourceFactory;
3737
use OCP\IL10N;
38-
use OCP\ILogger;
3938
use OC\DB\MigratorExecuteSqlEvent;
4039
use OC\Repair\Events\RepairAdvanceEvent;
4140
use OC\Repair\Events\RepairErrorEvent;
@@ -45,6 +44,8 @@
4544
use OC\Repair\Events\RepairStepEvent;
4645
use OC\Repair\Events\RepairWarningEvent;
4746
use OCP\L10N\IFactory;
47+
use Psr\Log\LoggerInterface;
48+
use Psr\Log\LogLevel;
4849

4950
if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
5051
@set_time_limit(0);
@@ -186,9 +187,10 @@ function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
186187
try {
187188
$updater->upgrade();
188189
} catch (\Exception $e) {
189-
\OC::$server->getLogger()->logException($e, [
190-
'level' => ILogger::ERROR,
190+
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
191+
'level' => LogLevel::ERROR,
191192
'app' => 'update',
193+
'exception' => $e
192194
]);
193195
$eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
194196
$eventSource->close();

core/register_command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
$application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig()));
9090
$application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig()));
9191
$application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig()));
92-
$application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->getLogger()));
92+
$application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->get(LoggerInterface::class)));
9393
$application->add(new OC\Core\Command\Background\ListCommand(\OC::$server->getJobList()));
9494

9595
$application->add(\OC::$server->query(\OC\Core\Command\Broadcast\Test::class));

cron.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@
3939
*/
4040
require_once __DIR__ . '/lib/versioncheck.php';
4141

42+
use Psr\Log\LoggerInterface;
43+
4244
try {
4345
require_once __DIR__ . '/lib/base.php';
4446

4547
if (\OCP\Util::needUpgrade()) {
46-
\OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']);
48+
\OC::$server->get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']);
4749
exit;
4850
}
4951
if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
50-
\OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
52+
\OC::$server->get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']);
5153
exit;
5254
}
5355

@@ -62,7 +64,7 @@
6264
$session = $cryptoWrapper->wrapSession($session);
6365
\OC::$server->setSession($session);
6466

65-
$logger = \OC::$server->getLogger();
67+
$logger = \OC::$server->get(LoggerInterface::class);
6668
$config = \OC::$server->getConfig();
6769
$tempManager = \OC::$server->getTempManager();
6870

@@ -185,11 +187,17 @@
185187
$config->setAppValue('core', 'lastcron', time());
186188
exit();
187189
} catch (Exception $ex) {
188-
\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
190+
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
191+
'app' => 'cron',
192+
'exception' => $ex
193+
]);
189194
echo $ex . PHP_EOL;
190195
exit(1);
191196
} catch (Error $ex) {
192-
\OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
197+
\OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
198+
'app' => 'cron',
199+
'exception' => $ex
200+
]);
193201
echo $ex . PHP_EOL;
194202
exit(1);
195203
}

lib/private/BackgroundJob/Job.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\BackgroundJob\IJob;
2929
use OCP\BackgroundJob\IJobList;
3030
use OCP\ILogger;
31+
use Psr\Log\LoggerInterface;
3132

3233
/**
3334
* @deprecated internal class, use \OCP\BackgroundJob\Job
@@ -45,7 +46,7 @@ abstract class Job implements IJob {
4546
public function execute(IJobList $jobList, ILogger $logger = null) {
4647
$jobList->setLastRun($this);
4748
if ($logger === null) {
48-
$logger = \OC::$server->getLogger();
49+
$logger = \OC::$server->get(LoggerInterface::class);
4950
}
5051

5152
try {

lib/private/Cache/File.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ public function gc() {
192192
}
193193
} catch (\OCP\Lock\LockedException $e) {
194194
// ignore locked chunks
195-
\OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
195+
\OC::$server->get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
196196
} catch (\OCP\Files\ForbiddenException $e) {
197-
\OC::$server->getLogger()->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']);
197+
\OC::$server->get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']);
198198
} catch (\OCP\Files\LockNotAcquiredException $e) {
199-
\OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
199+
\OC::$server->get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
200200
}
201201
}
202202
}

lib/private/Collaboration/AutoComplete/Manager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\Collaboration\AutoComplete\IManager;
2727
use OCP\Collaboration\AutoComplete\ISorter;
2828
use OCP\IServerContainer;
29+
use Psr\Log\LoggerInterface;
2930

3031
class Manager implements IManager {
3132
/** @var string[] */
@@ -46,7 +47,7 @@ public function runSorters(array $sorters, array &$sortArray, array $context) {
4647
if (isset($sorterInstances[$sorter])) {
4748
$sorterInstances[$sorter]->sort($sortArray, $context);
4849
} else {
49-
$this->c->getLogger()->warning('No sorter for ID "{id}", skipping', [
50+
$this->c->get(LoggerInterface::class)->warning('No sorter for ID "{id}", skipping', [
5051
'app' => 'core', 'id' => $sorter
5152
]);
5253
}
@@ -63,13 +64,13 @@ protected function getSorters() {
6364
/** @var ISorter $instance */
6465
$instance = $this->c->resolve($sorter);
6566
if (!$instance instanceof ISorter) {
66-
$this->c->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
67+
$this->c->get(LoggerInterface::class)->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
6768
['app' => 'core', 'class' => $sorter]);
6869
continue;
6970
}
7071
$sorterId = trim($instance->getId());
7172
if (trim($sorterId) === '') {
72-
$this->c->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
73+
$this->c->get(LoggerInterface::class)->notice('Skipping sorter with empty ID. Class name: {class}',
7374
['app' => 'core', 'class' => $sorter]);
7475
continue;
7576
}

lib/private/Files/Filesystem.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use OCP\IUser;
4949
use OCP\IUserManager;
5050
use OCP\IUserSession;
51+
use Psr\Log\LoggerInterface;
5152

5253
class Filesystem {
5354
private static ?Mount\Manager $mounts = null;
@@ -200,7 +201,7 @@ public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool
200201
*/
201202
public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) {
202203
if (self::$logWarningWhenAddingStorageWrapper) {
203-
\OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [
204+
\OC::$server->get(LoggerInterface::class)->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [
204205
'wrapper' => $wrapperName,
205206
'app' => 'filesystem',
206207
]);

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
4848
use OCP\Files\Storage\IChunkedFileWrite;
4949
use OCP\Files\Storage\IStorage;
50+
use Psr\Log\LoggerInterface;
5051

5152
class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFileWrite {
5253
use CopyDirectory;
@@ -89,7 +90,7 @@ public function __construct($params) {
8990
$this->validateWrites = (bool)$params['validateWrites'];
9091
}
9192

92-
$this->logger = \OC::$server->getLogger();
93+
$this->logger = \OC::$server->get(LoggerInterface::class);
9394
}
9495

9596
public function mkdir($path, bool $force = false) {

lib/private/L10N/L10N.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
use OCP\IL10N;
3232
use OCP\L10N\IFactory;
33+
use Psr\Log\LoggerInterface;
3334
use Punic\Calendar;
3435
use Symfony\Component\Translation\IdentityTranslator;
3536

@@ -234,7 +235,7 @@ protected function load(string $translationFile): bool {
234235
$json = json_decode(file_get_contents($translationFile), true);
235236
if (!\is_array($json)) {
236237
$jsonError = json_last_error();
237-
\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
238+
\OC::$server->get(LoggerInterface::class)->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
238239
return false;
239240
}
240241

lib/private/Log/Rotate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace OC\Log;
2626

2727
use OCP\Log\RotationTrait;
28+
use Psr\Log\LoggerInterface;
2829

2930
/**
3031
* This rotates the current logfile to a new name, this way the total log usage
@@ -43,7 +44,7 @@ public function run($dummy) {
4344
if ($this->shouldRotateBySize()) {
4445
$rotatedFile = $this->rotate();
4546
$msg = 'Log file "'.$this->filePath.'" was over '.$this->maxSize.' bytes, moved to "'.$rotatedFile.'"';
46-
\OC::$server->getLogger()->warning($msg, ['app' => Rotate::class]);
47+
\OC::$server->get(LoggerInterface::class)->warning($msg, ['app' => Rotate::class]);
4748
}
4849
}
4950
}

0 commit comments

Comments
 (0)