Skip to content

Commit

Permalink
Refactor OC\Server::getLogger
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
  • Loading branch information
summersab committed Aug 30, 2023
1 parent 613cd16 commit 77f5804
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 50 deletions.
8 changes: 5 additions & 3 deletions core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
18 changes: 13 additions & 5 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion lib/private/BackgroundJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/private/Collaboration/AutoComplete/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] */
Expand All @@ -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
]);
}
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
]);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/L10N/L10N.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use OCP\IL10N;
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;
use Punic\Calendar;
use Symfony\Component\Translation\IdentityTranslator;

Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Log/Rotate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
}
}
3 changes: 2 additions & 1 deletion lib/private/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/private/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\ITags;
use OCP\Share_Backend;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class Tags implements ITags {
/**
Expand Down Expand Up @@ -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 [];
}
Expand Down Expand Up @@ -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,
]);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use OCP\User\Events\BeforeUserCreatedEvent;
use OCP\User\Events\UserCreatedEvent;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;

/**
* Class Manager
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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(),
Expand All @@ -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
]);
}

Expand All @@ -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
]);
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 77f5804

Please sign in to comment.