From 77f5804d37080e249a0ee1300d8ca5ec10ab4cba Mon Sep 17 00:00:00 2001 From: Andrew Summers <18727110+summersab@users.noreply.github.com> Date: Tue, 29 Aug 2023 19:16:03 -0500 Subject: [PATCH] Refactor `OC\Server::getLogger` Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com> --- core/ajax/update.php | 8 +++++--- core/register_command.php | 2 +- cron.php | 18 +++++++++++++----- lib/private/BackgroundJob/Job.php | 3 ++- lib/private/Cache/File.php | 6 +++--- .../Collaboration/AutoComplete/Manager.php | 7 ++++--- lib/private/Files/Filesystem.php | 3 ++- .../Files/ObjectStore/ObjectStoreStorage.php | 3 ++- lib/private/L10N/L10N.php | 3 ++- lib/private/Log/Rotate.php | 3 ++- lib/private/Search.php | 3 ++- lib/private/Share20/DefaultShareProvider.php | 5 ++++- lib/private/Share20/ProviderFactory.php | 4 ++-- lib/private/Tags.php | 8 +++++--- lib/private/User/Manager.php | 3 ++- lib/private/legacy/OC_App.php | 16 ++++++++++------ lib/private/legacy/OC_Files.php | 15 +++++++++++---- lib/private/legacy/OC_Hook.php | 7 ++++++- lib/private/legacy/OC_Image.php | 3 ++- lib/private/legacy/OC_Template.php | 5 +++-- lib/private/legacy/OC_Util.php | 4 ++-- lib/public/AppFramework/App.php | 4 +++- lib/public/Util.php | 3 ++- ocs/v1.php | 6 +++++- public.php | 12 ++++++++++-- status.php | 7 ++++++- 26 files changed, 111 insertions(+), 50 deletions(-) diff --git a/core/ajax/update.php b/core/ajax/update.php index bc7d8b67fc6da..e8780833fcc66 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -35,7 +35,6 @@ use OCP\IEventSource; use OCP\IEventSourceFactory; use OCP\IL10N; -use OCP\ILogger; use OC\DB\MigratorExecuteSqlEvent; use OC\Repair\Events\RepairAdvanceEvent; use OC\Repair\Events\RepairErrorEvent; @@ -45,6 +44,8 @@ use OC\Repair\Events\RepairStepEvent; use OC\Repair\Events\RepairWarningEvent; use OCP\L10N\IFactory; +use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) { @set_time_limit(0); @@ -186,9 +187,10 @@ function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void { try { $updater->upgrade(); } catch (\Exception $e) { - \OC::$server->getLogger()->logException($e, [ - 'level' => ILogger::ERROR, + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ + 'level' => LogLevel::ERROR, 'app' => 'update', + 'exception' => $e ]); $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage()); $eventSource->close(); diff --git a/core/register_command.php b/core/register_command.php index c9b6cc999017b..ef4221691b2dd 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -89,7 +89,7 @@ $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); - $application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->getLogger())); + $application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->get(LoggerInterface::class))); $application->add(new OC\Core\Command\Background\ListCommand(\OC::$server->getJobList())); $application->add(\OC::$server->query(\OC\Core\Command\Broadcast\Test::class)); diff --git a/cron.php b/cron.php index 7d661621ed090..a1ea9e1e19a85 100644 --- a/cron.php +++ b/cron.php @@ -39,15 +39,17 @@ */ require_once __DIR__ . '/lib/versioncheck.php'; +use Psr\Log\LoggerInterface; + try { require_once __DIR__ . '/lib/base.php'; if (\OCP\Util::needUpgrade()) { - \OC::$server->getLogger()->debug('Update required, skipping cron', ['app' => 'cron']); + \OC::$server->get(LoggerInterface::class)->debug('Update required, skipping cron', ['app' => 'cron']); exit; } if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) { - \OC::$server->getLogger()->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); + \OC::$server->get(LoggerInterface::class)->debug('We are in maintenance mode, skipping cron', ['app' => 'cron']); exit; } @@ -62,7 +64,7 @@ $session = $cryptoWrapper->wrapSession($session); \OC::$server->setSession($session); - $logger = \OC::$server->getLogger(); + $logger = \OC::$server->get(LoggerInterface::class); $config = \OC::$server->getConfig(); $tempManager = \OC::$server->getTempManager(); @@ -185,11 +187,17 @@ $config->setAppValue('core', 'lastcron', time()); exit(); } catch (Exception $ex) { - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'app' => 'cron', + 'exception' => $ex + ]); echo $ex . PHP_EOL; exit(1); } catch (Error $ex) { - \OC::$server->getLogger()->logException($ex, ['app' => 'cron']); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'app' => 'cron', + 'exception' => $ex + ]); echo $ex . PHP_EOL; exit(1); } diff --git a/lib/private/BackgroundJob/Job.php b/lib/private/BackgroundJob/Job.php index ffcaaf8c36d76..33d64225cbb5a 100644 --- a/lib/private/BackgroundJob/Job.php +++ b/lib/private/BackgroundJob/Job.php @@ -28,6 +28,7 @@ use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\IJobList; use OCP\ILogger; +use Psr\Log\LoggerInterface; /** * @deprecated internal class, use \OCP\BackgroundJob\Job @@ -45,7 +46,7 @@ abstract class Job implements IJob { public function execute(IJobList $jobList, ILogger $logger = null) { $jobList->setLastRun($this); if ($logger === null) { - $logger = \OC::$server->getLogger(); + $logger = \OC::$server->get(LoggerInterface::class); } try { diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php index 72fc95a802b19..4f074e15f7660 100644 --- a/lib/private/Cache/File.php +++ b/lib/private/Cache/File.php @@ -192,11 +192,11 @@ public function gc() { } } catch (\OCP\Lock\LockedException $e) { // ignore locked chunks - \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); + \OC::$server->get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); } catch (\OCP\Files\ForbiddenException $e) { - \OC::$server->getLogger()->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); + \OC::$server->get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); } catch (\OCP\Files\LockNotAcquiredException $e) { - \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); + \OC::$server->get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); } } } diff --git a/lib/private/Collaboration/AutoComplete/Manager.php b/lib/private/Collaboration/AutoComplete/Manager.php index cab15baf535ad..47c63e0385e59 100644 --- a/lib/private/Collaboration/AutoComplete/Manager.php +++ b/lib/private/Collaboration/AutoComplete/Manager.php @@ -26,6 +26,7 @@ use OCP\Collaboration\AutoComplete\IManager; use OCP\Collaboration\AutoComplete\ISorter; use OCP\IServerContainer; +use Psr\Log\LoggerInterface; class Manager implements IManager { /** @var string[] */ @@ -46,7 +47,7 @@ public function runSorters(array $sorters, array &$sortArray, array $context) { if (isset($sorterInstances[$sorter])) { $sorterInstances[$sorter]->sort($sortArray, $context); } else { - $this->c->getLogger()->warning('No sorter for ID "{id}", skipping', [ + $this->c->get(LoggerInterface::class)->warning('No sorter for ID "{id}", skipping', [ 'app' => 'core', 'id' => $sorter ]); } @@ -63,13 +64,13 @@ protected function getSorters() { /** @var ISorter $instance */ $instance = $this->c->resolve($sorter); if (!$instance instanceof ISorter) { - $this->c->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}', + $this->c->get(LoggerInterface::class)->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}', ['app' => 'core', 'class' => $sorter]); continue; } $sorterId = trim($instance->getId()); if (trim($sorterId) === '') { - $this->c->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}', + $this->c->get(LoggerInterface::class)->notice('Skipping sorter with empty ID. Class name: {class}', ['app' => 'core', 'class' => $sorter]); continue; } diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index 5f7c0c403db6f..d6fe208ef280c 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -48,6 +48,7 @@ use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use Psr\Log\LoggerInterface; class Filesystem { private static ?Mount\Manager $mounts = null; @@ -200,7 +201,7 @@ public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool */ public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { if (self::$logWarningWhenAddingStorageWrapper) { - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ + \OC::$server->get(LoggerInterface::class)->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ 'wrapper' => $wrapperName, 'app' => 'filesystem', ]); diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index d918bd9872971..2260353c97e61 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -47,6 +47,7 @@ use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload; use OCP\Files\Storage\IChunkedFileWrite; use OCP\Files\Storage\IStorage; +use Psr\Log\LoggerInterface; class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFileWrite { use CopyDirectory; @@ -89,7 +90,7 @@ public function __construct($params) { $this->validateWrites = (bool)$params['validateWrites']; } - $this->logger = \OC::$server->getLogger(); + $this->logger = \OC::$server->get(LoggerInterface::class); } public function mkdir($path, bool $force = false) { diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php index ea4aa0527bba9..386e2690c13fd 100644 --- a/lib/private/L10N/L10N.php +++ b/lib/private/L10N/L10N.php @@ -30,6 +30,7 @@ use OCP\IL10N; use OCP\L10N\IFactory; +use Psr\Log\LoggerInterface; use Punic\Calendar; use Symfony\Component\Translation\IdentityTranslator; @@ -234,7 +235,7 @@ protected function load(string $translationFile): bool { $json = json_decode(file_get_contents($translationFile), true); if (!\is_array($json)) { $jsonError = json_last_error(); - \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); + \OC::$server->get(LoggerInterface::class)->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); return false; } diff --git a/lib/private/Log/Rotate.php b/lib/private/Log/Rotate.php index dfb588837f33b..d0f1c03c3b862 100644 --- a/lib/private/Log/Rotate.php +++ b/lib/private/Log/Rotate.php @@ -25,6 +25,7 @@ namespace OC\Log; use OCP\Log\RotationTrait; +use Psr\Log\LoggerInterface; /** * This rotates the current logfile to a new name, this way the total log usage @@ -43,7 +44,7 @@ public function run($dummy) { if ($this->shouldRotateBySize()) { $rotatedFile = $this->rotate(); $msg = 'Log file "'.$this->filePath.'" was over '.$this->maxSize.' bytes, moved to "'.$rotatedFile.'"'; - \OC::$server->getLogger()->warning($msg, ['app' => Rotate::class]); + \OC::$server->get(LoggerInterface::class)->warning($msg, ['app' => Rotate::class]); } } } diff --git a/lib/private/Search.php b/lib/private/Search.php index b1e39843e494a..100f71c8a5513 100644 --- a/lib/private/Search.php +++ b/lib/private/Search.php @@ -30,6 +30,7 @@ use OCP\ISearch; use OCP\Search\PagedProvider; use OCP\Search\Provider; +use Psr\Log\LoggerInterface; /** * Provide an interface to all search providers @@ -65,7 +66,7 @@ public function searchPaged($query, array $inApps = [], $page = 1, $size = 30) { $results = array_merge($results, $providerResults); } } else { - \OC::$server->getLogger()->warning('Ignoring Unknown search provider', ['provider' => $provider]); + \OC::$server->get(LoggerInterface::class)->warning('Ignoring Unknown search provider', ['provider' => $provider]); } } return $results; diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php index 5201cf074b13b..b9bf42ef9443f 100644 --- a/lib/private/Share20/DefaultShareProvider.php +++ b/lib/private/Share20/DefaultShareProvider.php @@ -55,6 +55,7 @@ use OCP\Share\IAttributes; use OCP\Share\IShare; use OCP\Share\IShareProvider; +use Psr\Log\LoggerInterface; use function str_starts_with; /** @@ -1247,7 +1248,9 @@ public function userDeleted($uid, $shareType) { ) ); } else { - \OC::$server->getLogger()->logException(new \InvalidArgumentException('Default share provider tried to delete all shares for type: ' . $shareType)); + \OC::$server->get(LoggerInterface::class)->error('Default share provider tried to delete all shares for type: ' . $shareType, [ + 'exception' => new \InvalidArgumentException('Default share provider tried to delete all shares for type: ' . $shareType) + ]); return; } diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php index 8c01d6609155c..6db565ae328b2 100644 --- a/lib/private/Share20/ProviderFactory.php +++ b/lib/private/Share20/ProviderFactory.php @@ -192,7 +192,7 @@ protected function getShareByMailProvider() { $this->serverContainer->getUserManager(), $this->serverContainer->getLazyRootFolder(), $this->serverContainer->getL10N('sharebymail'), - $this->serverContainer->getLogger(), + $this->serverContainer->get(LoggerInterface::class), $this->serverContainer->getMailer(), $this->serverContainer->getURLGenerator(), $this->serverContainer->getActivityManager(), @@ -234,7 +234,7 @@ protected function getShareByCircleProvider() { $this->serverContainer->getUserManager(), $this->serverContainer->getLazyRootFolder(), $this->serverContainer->getL10N('circles'), - $this->serverContainer->getLogger(), + $this->serverContainer->get(LoggerInterface::class), $this->serverContainer->getURLGenerator() ); } diff --git a/lib/private/Tags.php b/lib/private/Tags.php index 8da1e7edc3b8a..ab44178719d65 100644 --- a/lib/private/Tags.php +++ b/lib/private/Tags.php @@ -39,6 +39,7 @@ use OCP\ITags; use OCP\Share_Backend; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; class Tags implements ITags { /** @@ -486,10 +487,11 @@ public function getFavorites() { try { return $this->getIdsForTag(ITags::TAG_FAVORITE); } catch (\Exception $e) { - \OC::$server->getLogger()->logException($e, [ + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'message' => __METHOD__, - 'level' => ILogger::ERROR, + 'level' => LogLevel::ERROR, 'app' => 'core', + 'exception' => $e ]); return []; } @@ -549,7 +551,7 @@ public function tagAs($objid, $tag) { try { $qb->executeStatement(); } catch (\Exception $e) { - \OC::$server->getLogger()->error($e->getMessage(), [ + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'app' => 'core', 'exception' => $e, ]); diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index fb1afb6582564..5fd7918369ac6 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -55,6 +55,7 @@ use OCP\User\Events\BeforeUserCreatedEvent; use OCP\User\Events\UserCreatedEvent; use OCP\UserInterface; +use Psr\Log\LoggerInterface; /** * Class Manager @@ -234,7 +235,7 @@ public function checkPassword($loginName, $password) { $result = $this->checkPasswordNoLogging($loginName, $password); if ($result === false) { - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); + \OC::$server->get(LoggerInterface::class)->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); } return $result; diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index ac449a62a4ffb..46b0a48f8fe86 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -454,7 +454,7 @@ public static function getForms(string $type): array { * @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface */ public static function registerLogIn(array $entry) { - \OC::$server->getLogger()->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface'); + \OC::$server->get(LoggerInterface::class)->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface'); self::$altLogin[] = $entry; } @@ -467,7 +467,7 @@ public static function getAlternativeLogIns(): array { foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) { if (!in_array(IAlternativeLogin::class, class_implements($registration->getService()), true)) { - \OC::$server->getLogger()->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [ + \OC::$server->get(LoggerInterface::class)->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [ 'option' => $registration->getService(), 'interface' => IAlternativeLogin::class, 'app' => $registration->getAppId(), @@ -479,10 +479,11 @@ public static function getAlternativeLogIns(): array { /** @var IAlternativeLogin $provider */ $provider = \OCP\Server::get($registration->getService()); } catch (ContainerExceptionInterface $e) { - \OC::$server->getLogger()->logException($e, [ + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'message' => 'Alternative login option {option} can not be initialised.', 'option' => $registration->getService(), 'app' => $registration->getAppId(), + 'exception' => $e ]); } @@ -495,10 +496,11 @@ public static function getAlternativeLogIns(): array { 'class' => $provider->getClass(), ]; } catch (Throwable $e) { - \OC::$server->getLogger()->logException($e, [ + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'message' => 'Alternative login option {option} had an error while loading.', 'option' => $registration->getService(), 'app' => $registration->getAppId(), + 'exception' => $e ]); } } @@ -752,7 +754,7 @@ public static function updateApp(string $appId): bool { } if (is_file($appPath . '/appinfo/database.xml')) { - \OC::$server->getLogger()->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId); + \OC::$server->get(LoggerInterface::class)->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId); return false; } @@ -830,7 +832,9 @@ public static function executeRepairSteps(string $appId, array $steps) { $r->addStep($step); } catch (Exception $ex) { $dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage())); - \OC::$server->getLogger()->logException($ex); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'exception' => $ex + ]); } } // run the steps diff --git a/lib/private/legacy/OC_Files.php b/lib/private/legacy/OC_Files.php index 7bc1fab94b6b4..b6971e3210b11 100644 --- a/lib/private/legacy/OC_Files.php +++ b/lib/private/legacy/OC_Files.php @@ -47,6 +47,7 @@ use OCP\Files\Events\BeforeZipCreatedEvent; use OCP\Files\Events\BeforeDirectFileDownloadEvent; use OCP\EventDispatcher\IEventDispatcher; +use Psr\Log\LoggerInterface; /** * Class for file server access @@ -200,7 +201,7 @@ public static function get($dir, $files, $params = null) { $fileTime = $file->getMTime(); } else { // File is not a file? … - \OC::$server->getLogger()->debug( + \OC::$server->get(LoggerInterface::class)->debug( 'File given, but no Node available. Name {file}', [ 'app' => 'files', 'file' => $file ] ); @@ -221,18 +222,24 @@ public static function get($dir, $files, $params = null) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); } catch (\OCP\Lock\LockedException $ex) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); - OC::$server->getLogger()->logException($ex); + OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'exception' => $ex + ]); $l = \OC::$server->getL10N('lib'); $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; \OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint, 200); } catch (\OCP\Files\ForbiddenException $ex) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); - OC::$server->getLogger()->logException($ex); + OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'exception' => $ex + ]); $l = \OC::$server->getL10N('lib'); \OC_Template::printErrorPage($l->t('Cannot download file'), $ex->getMessage(), 200); } catch (\Exception $ex) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); - OC::$server->getLogger()->logException($ex); + OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'exception' => $ex + ]); $l = \OC::$server->getL10N('lib'); $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; if ($event && $event->getErrorMessage() !== null) { diff --git a/lib/private/legacy/OC_Hook.php b/lib/private/legacy/OC_Hook.php index a7a0f755673c8..ef8f2068a166e 100644 --- a/lib/private/legacy/OC_Hook.php +++ b/lib/private/legacy/OC_Hook.php @@ -28,6 +28,9 @@ * along with this program. If not, see * */ + +use Psr\Log\LoggerInterface; + class OC_Hook { public static $thrownExceptions = []; @@ -105,7 +108,9 @@ public static function emit($signalClass, $signalName, $params = []) { call_user_func([ $i["class"], $i["name"] ], $params); } catch (Exception $e) { self::$thrownExceptions[] = $e; - \OC::$server->getLogger()->logException($e); + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ + 'exception' => $e + ]); if ($e instanceof \OCP\HintException) { throw $e; } diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index aac0c49673400..4e796880f2ad4 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -44,6 +44,7 @@ * */ use OCP\IImage; +use Psr\Log\LoggerInterface; /** * Class for basic image manipulation @@ -84,7 +85,7 @@ class OC_Image implements \OCP\IImage { public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { $this->logger = $logger; if ($logger === null) { - $this->logger = \OC::$server->getLogger(); + $this->logger = \OC::$server->get(LoggerInterface::class); } $this->config = $config; if ($config === null) { diff --git a/lib/private/legacy/OC_Template.php b/lib/private/legacy/OC_Template.php index 974f26fa38122..e69ec6db92e62 100644 --- a/lib/private/legacy/OC_Template.php +++ b/lib/private/legacy/OC_Template.php @@ -39,6 +39,7 @@ */ use OC\TemplateLayout; use OCP\AppFramework\Http\TemplateResponse; +use Psr\Log\LoggerInterface; require_once __DIR__.'/template/functions.php'; @@ -257,7 +258,7 @@ public static function printErrorPage($error_msg, $hint = '', $statusCode = 500) $content->assign('errors', $errors); $content->printPage(); } catch (\Exception $e) { - $logger = \OC::$server->getLogger(); + $logger = \OC::$server->get(LoggerInterface::class); $logger->error("$error_msg $hint", ['app' => 'core']); $logger->logException($e, ['app' => 'core']); @@ -291,7 +292,7 @@ public static function printExceptionErrorPage($exception, $statusCode = 503) { $content->printPage(); } catch (\Exception $e) { try { - $logger = \OC::$server->getLogger(); + $logger = \OC::$server->get(LoggerInterface::class); $logger->logException($exception, ['app' => 'core']); $logger->logException($e, ['app' => 'core']); } catch (Throwable $e) { diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php index 9d62c46137e61..89f635aee07a0 100644 --- a/lib/private/legacy/OC_Util.php +++ b/lib/private/legacy/OC_Util.php @@ -230,7 +230,7 @@ public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { * @return void */ public static function copyr($source, \OCP\Files\Folder $target) { - $logger = \OC::$server->getLogger(); + $logger = \OC::$server->get(LoggerInterface::class); // Verify if folder exists $dir = opendir($source); @@ -1077,7 +1077,7 @@ public static function normalizeUnicode($value) { $normalizedValue = Normalizer::normalize($value); if ($normalizedValue === null || $normalizedValue === false) { - \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); + \OC::$server->get(LoggerInterface::class)->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); return $value; } diff --git a/lib/public/AppFramework/App.php b/lib/public/AppFramework/App.php index 4d6e9177b787d..e65ed3a6dbc46 100644 --- a/lib/public/AppFramework/App.php +++ b/lib/public/AppFramework/App.php @@ -38,6 +38,7 @@ use OC\Route\Router; use OC\ServerContainer; use OCP\Route\IRouter; +use Psr\Log\LoggerInterface; /** * Class App @@ -98,8 +99,9 @@ public function __construct(string $appName, array $urlParams = []) { } if (!$setUpViaQuery && $applicationClassName !== \OCP\AppFramework\App::class) { - \OC::$server->getLogger()->logException($e, [ + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ 'app' => $appName, + 'exception' => $e ]); } } diff --git a/lib/public/Util.php b/lib/public/Util.php index bff8038b3dda5..9d5f92f894a62 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -50,6 +50,7 @@ use OC\AppScriptSort; use bantu\IniGetWrapper\IniGetWrapper; use Psr\Container\ContainerExceptionInterface; +use Psr\Log\LoggerInterface; /** * This class provides different helper functions to make the life of a developer easier @@ -119,7 +120,7 @@ public static function getChannel() { */ public static function writeLog($app, $message, $level) { $context = ['app' => $app]; - \OC::$server->getLogger()->log($level, $message, $context); + \OC::$server->get(LoggerInterface::class)->log($level, $message, $context); } /** diff --git a/ocs/v1.php b/ocs/v1.php index 055398993729a..626f33d66b7c8 100644 --- a/ocs/v1.php +++ b/ocs/v1.php @@ -30,6 +30,8 @@ require_once __DIR__ . '/../lib/versioncheck.php'; require_once __DIR__ . '/../lib/base.php'; +use Psr\Log\LoggerInterface; + if (\OCP\Util::needUpgrade() || \OC::$server->getConfig()->getSystemValueBool('maintenance')) { // since the behavior of apps or remotes are unpredictable during @@ -77,7 +79,9 @@ } catch (\OC\User\LoginException $e) { OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED, 'Unauthorised')); } catch (\Exception $e) { - \OC::$server->getLogger()->logException($e); + \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [ + 'exception' => $e + ]); OC_API::setContentType(); $format = \OC::$server->getRequest()->getParam('format', 'xml'); diff --git a/public.php b/public.php index 2956d7f79dd81..52a91eadc1be2 100644 --- a/public.php +++ b/public.php @@ -32,6 +32,8 @@ */ require_once __DIR__ . '/lib/versioncheck.php'; +use Psr\Log\LoggerInterface; + try { require_once __DIR__ . '/lib/base.php'; if (\OCP\Util::needUpgrade()) { @@ -85,10 +87,16 @@ $status = 503; } //show the user a detailed error page - \OC::$server->getLogger()->logException($ex, ['app' => 'public']); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'app' => 'public', + 'exception' => $ex + ]); OC_Template::printExceptionErrorPage($ex, $status); } catch (Error $ex) { //show the user a detailed error page - \OC::$server->getLogger()->logException($ex, ['app' => 'public']); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'app' => 'public', + 'exception' => $ex + ]); OC_Template::printExceptionErrorPage($ex, 500); } diff --git a/status.php b/status.php index 62eb978781cc1..9150caa05fb79 100644 --- a/status.php +++ b/status.php @@ -33,6 +33,8 @@ */ require_once __DIR__ . '/lib/versioncheck.php'; +use Psr\Log\LoggerInterface; + try { require_once __DIR__ . '/lib/base.php'; @@ -62,5 +64,8 @@ } } catch (Exception $ex) { http_response_code(500); - \OC::$server->getLogger()->logException($ex, ['app' => 'remote']); + \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [ + 'app' => 'remote', + 'exception' => $ex + ]); }