From bf5cc619dc0bd8143aff24bb7a7caef8e1a9d8f2 Mon Sep 17 00:00:00 2001 From: korelstar Date: Sun, 27 Mar 2022 21:53:17 +0200 Subject: [PATCH] improve logging --- .github/workflows/test.yml | 5 ++++- lib/Service/MetaService.php | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a4f9db9f1..02b1c2512 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -73,10 +73,13 @@ jobs: php server/occ files:scan --all - name: Start Nextcloud server working-directory: ../server/ - run: "php -S localhost:8080 &" + run: "php -S localhost:8080 > data/php.log 2>&1 &" - name: Test API run: make test-api - name: Show nextcloud.log if: always() run: "cat ../server/data/nextcloud.log" + - name: Show php.log + if: always() + run: "cat ../server/data/php.log" diff --git a/lib/Service/MetaService.php b/lib/Service/MetaService.php index 24c03d71c..879d9e30e 100644 --- a/lib/Service/MetaService.php +++ b/lib/Service/MetaService.php @@ -75,11 +75,14 @@ public function getAll(string $userId, array $notes, bool $forceUpdate = false) } } + $insertErrorCount = 0; // insert/update changes foreach ($notes as $id => $note) { if (!array_key_exists($id, $metas)) { // INSERT new notes - $meta = $this->createMeta($userId, $note); + $meta = $this->createMeta($userId, $note, function () use (&$insertErrorCount) { + $insertErrorCount++; + }); } else { // UPDATE changed notes $meta = $metas[$id]; @@ -89,6 +92,18 @@ public function getAll(string $userId, array $notes, bool $forceUpdate = false) } $result[$id] = new MetaNote($note, $meta); } + if ($insertErrorCount) { + if ($insertErrorCount == count($notes)) { + $this->util->logger->warning( + 'Database failed inserting Meta objects for all '.$insertErrorCount.' notes. '. + 'If this happens consistently, there is a problem with your database.', + ); + } else { + $this->util->logger->warning( + 'Database failed inserting Meta objects for '.$insertErrorCount.' times.', + ); + } + } return $result; } @@ -116,7 +131,7 @@ private function getIndexedArray(array $data, string $property) : array { return $result; } - private function createMeta(string $userId, Note $note) : Meta { + private function createMeta(string $userId, Note $note, callable $onError = null) : Meta { $meta = new Meta(); $meta->setUserId($userId); $meta->setFileId($note->getId()); @@ -129,7 +144,12 @@ private function createMeta(string $userId, Note $note) : Meta { // We can ignore this, since the result should be the same. // But we log it for being able to detect other problems. // (If this happens often, this may cause performance problems.) - $this->util->logger->warning( + $loglevel = 'warning'; + if ($onError) { + $onError(); + $loglevel = 'debug'; + } + $this->util->logger->$loglevel( 'Could not insert meta data for note '.$note->getId(), [ 'exception' => $e ] );