Only return logs that exist

This commit is contained in:
shamoon 2023-02-23 14:32:58 -08:00
parent 782db3f324
commit 4726fe8b6f
3 changed files with 26 additions and 7 deletions

View File

@ -38,14 +38,14 @@ export class LogsComponent implements OnInit, AfterViewChecked {
} }
reloadLogs() { reloadLogs() {
this.logService.get(this.activeLog).subscribe( this.logService.get(this.activeLog).subscribe({
(result) => { next: (result) => {
this.logs = result this.logs = result
}, },
(error) => { error: () => {
this.logs = [] this.logs = []
} },
) })
} }
getLogLevel(log: string) { getLogLevel(log: string) {

View File

@ -1590,10 +1590,23 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertEqual(v1.filter_rules.count(), 0) self.assertEqual(v1.filter_rules.count(), 0)
def test_get_logs(self): 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/") response = self.client.get("/api/logs/")
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertCountEqual(response.data, ["mail", "paperless"]) 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): def test_get_invalid_log(self):
response = self.client.get("/api/logs/bogus_log/") response = self.client.get("/api/logs/bogus_log/")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

View File

@ -593,11 +593,14 @@ class LogViewSet(ViewSet):
log_files = ["paperless", "mail"] 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): def retrieve(self, request, pk=None, *args, **kwargs):
if pk not in self.log_files: if pk not in self.log_files:
raise Http404() raise Http404()
filename = os.path.join(settings.LOGGING_DIR, f"{pk}.log") filename = self.get_log_filename(pk)
if not os.path.isfile(filename): if not os.path.isfile(filename):
raise Http404() raise Http404()
@ -608,7 +611,10 @@ class LogViewSet(ViewSet):
return Response(lines) return Response(lines)
def list(self, request, *args, **kwargs): 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): class SavedViewViewSet(ModelViewSet, PassUserMixin):