Skip to content

Commit

Permalink
Merge pull request #43461 from nextcloud/fix/get-rid-of-getlogger
Browse files Browse the repository at this point in the history
chore: Migrate away from OC::$server->getLogger
  • Loading branch information
nfebe authored Feb 14, 2024
2 parents 0369e4c + c0ce272 commit 3fb1674
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 76 deletions.
4 changes: 2 additions & 2 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,11 @@ public function createShare(
try {
$share = $this->shareManager->createShare($share);
} catch (GenericShareException $e) {
\OC::$server->getLogger()->logException($e);
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
$code = $e->getCode() === 0 ? 403 : $e->getCode();
throw new OCSException($e->getHint(), $code);
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e);
\OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
throw new OCSForbiddenException($e->getMessage(), $e);
}

Expand Down
13 changes: 7 additions & 6 deletions apps/files_sharing/lib/DeleteOrphanedSharesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IDBConnection;
use OCP\Server;
use Psr\Log\LoggerInterface;

/**
* Delete all share entries that have no matching entries in the file cache table.
*/
class DeleteOrphanedSharesJob extends TimedJob {
/**
* Default interval in minutes
*
* @var int $defaultIntervalMin
**/
protected $defaultIntervalMin = 15;
protected int $defaultIntervalMin = 15;

/**
* sets the correct interval for this timed job
Expand All @@ -53,15 +54,15 @@ public function __construct(ITimeFactory $time) {
* @param array $argument unused argument
*/
public function run($argument) {
$connection = \OC::$server->getDatabaseConnection();
$logger = \OC::$server->getLogger();
$connection = Server::get(IDBConnection::class);
$logger = Server::get(LoggerInterface::class);

$sql =
'DELETE FROM `*PREFIX*share` ' .
'WHERE `item_type` in (\'file\', \'folder\') ' .
'AND NOT EXISTS (SELECT `fileid` FROM `*PREFIX*filecache` WHERE `file_source` = `fileid`)';

$deletedEntries = $connection->executeUpdate($sql);
$deletedEntries = $connection->executeStatement($sql);
$logger->debug("$deletedEntries orphaned share(s) deleted", ['app' => 'DeleteOrphanedSharesJob']);
}
}
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/ShareBackend/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use OCA\FederatedFileSharing\FederatedShareProvider;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

class File implements \OCP\Share_Backend_File_Dependent {
public const FORMAT_SHARED_STORAGE = 0;
Expand Down Expand Up @@ -233,7 +234,7 @@ protected static function resolveReshares($source) {
if (isset($fileOwner)) {
$source['fileOwner'] = $fileOwner;
} else {
\OC::$server->getLogger()->error('No owner found for reshare', ['app' => 'files_sharing']);
\OCP\Server::get(LoggerInterface::class)->error('No owner found for reshare', ['app' => 'files_sharing']);
}

return $source;
Expand Down
11 changes: 9 additions & 2 deletions apps/files_sharing/lib/SharedMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\ICache;
use OCP\IUser;
use OCP\Share\Events\VerifyMountPointEvent;
use Psr\Log\LoggerInterface;

/**
* Shared mount points can be moved by the user
Expand Down Expand Up @@ -198,7 +199,7 @@ protected function stripUserFilesPath($path) {

// it is not a file relative to data/user/files
if (count($split) < 3 || $split[1] !== 'files') {
\OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
\OCP\Server::get(LoggerInterface::class)->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
}

Expand Down Expand Up @@ -226,7 +227,13 @@ public function moveMount($target) {
$this->setMountPoint($target);
$this->storage->setMountPoint($relTargetPath);
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']);
\OCP\Server::get(LoggerInterface::class)->error(
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
[
'app' => 'files_sharing',
'exception' => $e,
]
);
}

return $result;
Expand Down
4 changes: 3 additions & 1 deletion apps/user_ldap/lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
namespace OCA\User_LDAP;

use Psr\Log\LoggerInterface;

/**
* @property int ldapPagingSize holds an integer
* @property string ldapUserAvatarRule
Expand Down Expand Up @@ -598,7 +600,7 @@ public function getAvatarAttributes(): array {
return [strtolower($attribute)];
}
if ($value !== self::AVATAR_PREFIX_DEFAULT) {
\OC::$server->getLogger()->warning('Invalid config value to ldapUserAvatarRule; falling back to default.');
\OCP\Server::get(LoggerInterface::class)->warning('Invalid config value to ldapUserAvatarRule; falling back to default.');
}
return $defaultAttributes;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/user_ldap/lib/GroupPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\User_LDAP;

use OCP\GroupInterface;
use Psr\Log\LoggerInterface;

class GroupPluginManager {
private int $respondToActions = 0;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function register(ILDAPGroupPlugin $plugin) {
foreach ($this->which as $action => $v) {
if ((bool)($respondToActions & $action)) {
$this->which[$action] = $plugin;
\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
\OCP\Server::get(LoggerInterface::class)->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Doctrine\DBAL\Platforms\SqlitePlatform;
use OCP\DB\IPreparedStatement;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Psr\Log\LoggerInterface;

/**
* Class AbstractMapping
Expand Down Expand Up @@ -351,7 +352,7 @@ public function getList(int $offset = 0, int $limit = null, bool $invalidatedOnl
*/
public function map($fdn, $name, $uuid) {
if (mb_strlen($fdn) > 4000) {
\OC::$server->getLogger()->error(
\OCP\Server::get(LoggerInterface::class)->error(
'Cannot map, because the DN exceeds 4000 characters: {dn}',
[
'app' => 'user_ldap',
Expand Down
5 changes: 3 additions & 2 deletions apps/user_ldap/lib/UserPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\User_LDAP;

use OC\User\Backend;
use Psr\Log\LoggerInterface;

class UserPluginManager {
private int $respondToActions = 0;
Expand Down Expand Up @@ -62,12 +63,12 @@ public function register(ILDAPUserPlugin $plugin) {
foreach ($this->which as $action => $v) {
if (is_int($action) && (bool)($respondToActions & $action)) {
$this->which[$action] = $plugin;
\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
\OCP\Server::get(LoggerInterface::class)->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
}
}
if (method_exists($plugin, 'deleteUser')) {
$this->which['deleteUser'] = $plugin;
\OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
\OCP\Server::get(LoggerInterface::class)->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected function initHelper() {
* initializes the Access test instance
*/
protected function initAccess() {
$this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server->getConfig(), \OC::$server->getLogger());
$this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server->getConfig(), \OCP\Server::get(LoggerInterface::class));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/private/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function getStorage() {
$this->storage = new View('/' . $user->getUID() . '/cache');
return $this->storage;
} else {
\OC::$server->get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']);
\OCP\Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']);
throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
}
}
Expand Down 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']);
\OCP\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']);
\OCP\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']);
\OCP\Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
}
}
}
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.", [
\OCP\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
87 changes: 54 additions & 33 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
use OCP\Files\Storage\IChunkedFileWrite;
use OCP\Files\Storage\IStorage;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFileWrite {
use CopyDirectory;
Expand All @@ -56,7 +56,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil
protected string $id;
private string $objectPrefix = 'urn:oid:';

private ILogger $logger;
private LoggerInterface $logger;

private bool $handleCopiesAsOwned;
protected bool $validateWrites = true;
Expand Down Expand Up @@ -84,7 +84,7 @@ public function __construct($params) {
}
$this->handleCopiesAsOwned = (bool)($params['handleCopiesAsOwned'] ?? false);

$this->logger = \OC::$server->getLogger();
$this->logger = \OCP\Server::get(LoggerInterface::class);
}

public function mkdir($path, bool $force = false) {
Expand Down Expand Up @@ -220,10 +220,13 @@ public function rmObject(ICacheEntry $entry): bool {
$this->objectStore->deleteObject($this->getURN($entry->getId()));
} catch (\Exception $ex) {
if ($ex->getCode() !== 404) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not delete object ' . $this->getURN($entry->getId()) . ' for ' . $entry->getPath(),
]);
$this->logger->error(
'Could not delete object ' . $this->getURN($entry->getId()) . ' for ' . $entry->getPath(),
[
'app' => 'objectstore',
'exception' => $ex,
]
);
return false;
}
//removing from cache is ok as it does not exist in the objectstore anyway
Expand Down Expand Up @@ -286,7 +289,7 @@ public function opendir($path) {

return IteratorDirectory::wrap($files);
} catch (\Exception $e) {
$this->logger->logException($e);
$this->logger->error($e->getMessage(), ['exception' => $e]);
return false;
}
}
Expand Down Expand Up @@ -336,16 +339,22 @@ public function fopen($path, $mode) {
}
return $handle;
} catch (NotFoundException $e) {
$this->logger->logException($e, [
'app' => 'objectstore',
'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
]);
$this->logger->error(
'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
[
'app' => 'objectstore',
'exception' => $e,
]
);
throw $e;
} catch (\Exception $ex) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
]);
} catch (\Exception $e) {
$this->logger->error(
'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
[
'app' => 'objectstore',
'exception' => $e,
]
);
return false;
}
} else {
Expand Down Expand Up @@ -442,10 +451,13 @@ public function touch($path, $mtime = null) {
];
$this->getCache()->put($path, $stat);
} catch (\Exception $ex) {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not create object for ' . $path,
]);
$this->logger->error(
'Could not create object for ' . $path,
[
'app' => 'objectstore',
'exception' => $ex,
]
);
throw $ex;
}
}
Expand Down Expand Up @@ -540,15 +552,21 @@ public function writeStream(string $path, $stream, int $size = null): int {
* Else people lose access to existing files
*/
$this->getCache()->remove($uploadPath);
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not create object ' . $urn . ' for ' . $path,
]);
$this->logger->error(
'Could not create object ' . $urn . ' for ' . $path,
[
'app' => 'objectstore',
'exception' => $ex,
]
);
} else {
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not update object ' . $urn . ' for ' . $path,
]);
$this->logger->error(
'Could not update object ' . $urn . ' for ' . $path,
[
'app' => 'objectstore',
'exception' => $ex,
]
);
}
throw $ex; // make this bubble up
}
Expand Down Expand Up @@ -713,10 +731,13 @@ public function completeChunkedWrite(string $targetPath, string $writeToken): in
}
} catch (S3MultipartUploadException|S3Exception $e) {
$this->objectStore->abortMultipartUpload($urn, $writeToken);
$this->logger->logException($e, [
'app' => 'objectstore',
'message' => 'Could not compete multipart upload ' . $urn . ' with uploadId ' . $writeToken,
]);
$this->logger->error(
'Could not compete multipart upload ' . $urn . ' with uploadId ' . $writeToken,
[
'app' => 'objectstore',
'exception' => $e,
]
);
throw new GenericFileException('Could not write chunked file');
}
return $size;
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']);
\OCP\Server::get(LoggerInterface::class)->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
return false;
}

Expand Down
Loading

0 comments on commit 3fb1674

Please sign in to comment.