From 4726fe8b6f7ca3f7d563c0206099c6011fdbf23e Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:32:58 -0800 Subject: [PATCH] Only return logs that exist --- .../app/components/manage/logs/logs.component.ts | 10 +++++----- src/documents/tests/test_api.py | 13 +++++++++++++ src/documents/views.py | 10 ++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src-ui/src/app/components/manage/logs/logs.component.ts b/src-ui/src/app/components/manage/logs/logs.component.ts index b2e6af80d..2da52e950 100644 --- a/src-ui/src/app/components/manage/logs/logs.component.ts +++ b/src-ui/src/app/components/manage/logs/logs.component.ts @@ -38,14 +38,14 @@ export class LogsComponent implements OnInit, AfterViewChecked { } reloadLogs() { - this.logService.get(this.activeLog).subscribe( - (result) => { + this.logService.get(this.activeLog).subscribe({ + next: (result) => { this.logs = result }, - (error) => { + error: () => { this.logs = [] - } - ) + }, + }) } getLogLevel(log: string) { diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 5ad1252e4..a38651984 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -1590,10 +1590,23 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertEqual(v1.filter_rules.count(), 0) def test_get_logs(self): + log_data = "test\ntest2\n" + with open(os.path.join(settings.LOGGING_DIR, "mail.log"), "w") as f: + f.write(log_data) + with open(os.path.join(settings.LOGGING_DIR, "paperless.log"), "w") as f: + f.write(log_data) response = self.client.get("/api/logs/") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data, ["mail", "paperless"]) + def test_get_logs_only_when_exist(self): + log_data = "test\ntest2\n" + with open(os.path.join(settings.LOGGING_DIR, "paperless.log"), "w") as f: + f.write(log_data) + response = self.client.get("/api/logs/") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertCountEqual(response.data, ["paperless"]) + def test_get_invalid_log(self): response = self.client.get("/api/logs/bogus_log/") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) diff --git a/src/documents/views.py b/src/documents/views.py index 7305073f5..c14cb70e6 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -593,11 +593,14 @@ class LogViewSet(ViewSet): log_files = ["paperless", "mail"] + def get_log_filename(self, log): + return os.path.join(settings.LOGGING_DIR, f"{log}.log") + def retrieve(self, request, pk=None, *args, **kwargs): if pk not in self.log_files: raise Http404() - filename = os.path.join(settings.LOGGING_DIR, f"{pk}.log") + filename = self.get_log_filename(pk) if not os.path.isfile(filename): raise Http404() @@ -608,7 +611,10 @@ class LogViewSet(ViewSet): return Response(lines) def list(self, request, *args, **kwargs): - return Response(self.log_files) + exist = [ + log for log in self.log_files if os.path.isfile(self.get_log_filename(log)) + ] + return Response(exist) class SavedViewViewSet(ModelViewSet, PassUserMixin):