Skip to content

Commit 8ac46a6

Browse files
authored
Merge pull request #33103 from nextcloud/enhancement/add-logging-lo-federation
[Stable23] Logging, updating status for general error in federation
2 parents 4456576 + 11089f7 commit 8ac46a6

4 files changed

Lines changed: 39 additions & 24 deletions

File tree

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedS
9999
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
100100
// remote server revoked access to the address book, remove it
101101
$this->backend->deleteAddressBook($addressBookId);
102-
$this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']);
103-
throw $ex;
102+
$this->logger->error('Authorization failed, remove address book: ' . $url, ['app' => 'dav']);
103+
} else {
104+
$this->logger->error('Client exception:', ['app' => 'dav', 'exception' => $ex]);
104105
}
106+
throw $ex;
105107
}
106108

107109
// 3. apply changes

apps/federation/lib/SyncFederationAddressBooks.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\DAV\CardDAV\SyncService;
3030
use OCP\AppFramework\Http;
3131
use OCP\OCS\IDiscoveryService;
32+
use Psr\Log\LoggerInterface;
3233

3334
class SyncFederationAddressBooks {
3435

@@ -41,18 +42,18 @@ class SyncFederationAddressBooks {
4142
/** @var DiscoveryService */
4243
private $ocsDiscoveryService;
4344

44-
/**
45-
* @param DbHandler $dbHandler
46-
* @param SyncService $syncService
47-
* @param IDiscoveryService $ocsDiscoveryService
48-
*/
45+
/** @var LoggerInterface */
46+
private $logger;
47+
4948
public function __construct(DbHandler $dbHandler,
5049
SyncService $syncService,
51-
IDiscoveryService $ocsDiscoveryService
50+
IDiscoveryService $ocsDiscoveryService,
51+
LoggerInterface $logger
5252
) {
5353
$this->syncService = $syncService;
5454
$this->dbHandler = $dbHandler;
5555
$this->ocsDiscoveryService = $ocsDiscoveryService;
56+
$this->logger = $logger;
5657
}
5758

5859
/**
@@ -71,6 +72,7 @@ public function syncThemAll(\Closure $callback) {
7172
$addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
7273

7374
if (is_null($sharedSecret)) {
75+
$this->logger->error('Shared secret is null');
7476
continue;
7577
}
7678
$targetBookId = $trustedServer['url_hash'];
@@ -82,10 +84,16 @@ public function syncThemAll(\Closure $callback) {
8284
$newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
8385
if ($newToken !== $syncToken) {
8486
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
87+
} else {
88+
$this->logger->info('Token unchanged from previous sync.');
8589
}
8690
} catch (\Exception $ex) {
8791
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
8892
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
93+
$this->logger->error('Server sync failed because of revoked access.');
94+
} else {
95+
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
96+
$this->logger->error('Server sync failed.');
8997
}
9098
$callback($url, $ex);
9199
}

apps/federation/lib/SyncJob.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@
2626
namespace OCA\Federation;
2727

2828
use OC\BackgroundJob\TimedJob;
29-
use OCP\ILogger;
29+
use Psr\Log\LoggerInterface;
3030

3131
class SyncJob extends TimedJob {
3232

3333
/** @var SyncFederationAddressBooks */
3434
protected $syncService;
3535

36-
/** @var ILogger */
36+
/** @var LoggerInterface */
3737
protected $logger;
3838

3939
/**
4040
* @param SyncFederationAddressBooks $syncService
41-
* @param ILogger $logger
4241
*/
43-
public function __construct(SyncFederationAddressBooks $syncService, ILogger $logger) {
42+
public function __construct(SyncFederationAddressBooks $syncService, LoggerInterface $logger) {
4443
// Run once a day
4544
$this->setInterval(24 * 60 * 60);
4645
$this->syncService = $syncService;
@@ -50,11 +49,11 @@ public function __construct(SyncFederationAddressBooks $syncService, ILogger $lo
5049
protected function run($argument) {
5150
$this->syncService->syncThemAll(function ($url, $ex) {
5251
if ($ex instanceof \Exception) {
53-
$this->logger->logException($ex, [
54-
'message' => "Error while syncing $url.",
55-
'level' => ILogger::INFO,
56-
'app' => 'fed-sync',
57-
]);
52+
$this->logger->error("Error while syncing $url.",
53+
[
54+
'exception' => $ex
55+
]
56+
);
5857
}
5958
});
6059
}

apps/federation/tests/SyncFederationAddressbooksTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,31 @@
3131
use OC\OCS\DiscoveryService;
3232
use OCA\Federation\DbHandler;
3333
use OCA\Federation\SyncFederationAddressBooks;
34+
use PHPUnit\Framework\MockObject\MockObject;
35+
use Psr\Log\LoggerInterface;
3436

3537
class SyncFederationAddressbooksTest extends \Test\TestCase {
3638

3739
/** @var array */
3840
private $callBacks = [];
3941

40-
/** @var \PHPUnit\Framework\MockObject\MockObject | DiscoveryService */
42+
/** @var MockObject | DiscoveryService */
4143
private $discoveryService;
4244

45+
/** @var MockObject|LoggerInterface */
46+
private $logger;
47+
4348
protected function setUp(): void {
4449
parent::setUp();
4550

4651
$this->discoveryService = $this->getMockBuilder(DiscoveryService::class)
4752
->disableOriginalConstructor()->getMock();
4853
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
54+
$this->logger = $this->createMock(LoggerInterface::class);
4955
}
5056

5157
public function testSync() {
52-
/** @var DbHandler | \PHPUnit\Framework\MockObject\MockObject $dbHandler */
58+
/** @var DbHandler | MockObject $dbHandler */
5359
$dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
5460
disableOriginalConstructor()->
5561
getMock();
@@ -62,24 +68,24 @@ public function testSync() {
6268
'sync_token' => '0'
6369
]
6470
]);
65-
$dbHandler->expects($this->once())->method('setServerStatus')->
66-
with('https://cloud.drop.box', 1, '1');
71+
$dbHandler->expects($this->once())->method('setServerStatus')
72+
->with('https://cloud.drop.box', 1, '1');
6773
$syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
6874
->disableOriginalConstructor()
6975
->getMock();
7076
$syncService->expects($this->once())->method('syncRemoteAddressBook')
7177
->willReturn(1);
7278

7379
/** @var \OCA\DAV\CardDAV\SyncService $syncService */
74-
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService);
80+
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
7581
$s->syncThemAll(function ($url, $ex) {
7682
$this->callBacks[] = [$url, $ex];
7783
});
7884
$this->assertEquals(1, count($this->callBacks));
7985
}
8086

8187
public function testException() {
82-
/** @var DbHandler | \PHPUnit\Framework\MockObject\MockObject $dbHandler */
88+
/** @var DbHandler | MockObject $dbHandler */
8389
$dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
8490
disableOriginalConstructor()->
8591
getMock();
@@ -99,7 +105,7 @@ public function testException() {
99105
->willThrowException(new \Exception('something did not work out'));
100106

101107
/** @var \OCA\DAV\CardDAV\SyncService $syncService */
102-
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService);
108+
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
103109
$s->syncThemAll(function ($url, $ex) {
104110
$this->callBacks[] = [$url, $ex];
105111
});

0 commit comments

Comments
 (0)