Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] /api/logs/$type/ returns 404
### Description

I have found that even though the `/api/logs/` endpoint returns `["paperless", "mail"]`, the server still sends a 404 response when calling `/api/logs/mail/`. Is this intentional?

To me it would make sense to not include `"mail"` in the response from `/api/logs/`, as this suggests that the log should exists.

### Steps to reproduce

1. GET `/api/logs/`
2. Ensure that e.g. "mail" is listed.
3. GET `/api/logs/mail/`
4. Observe 404 response

Same goes for the webclient when selecting the mail.log tab:
Browser console output: `Failed to load resource: the server responded with a status of 404 ()`

### Webserver logs

```bash
[2023-02-23 22:30:24,488] [WARNING] [django.request] Not Found: /api/logs/mail/
```


### Browser logs

_No response_

### Paperless-ngx version

Tested on 1.11.3 and 1.13.0

### Host OS

Ubuntu 20.04

### Installation method

Docker - official image

### Browser

Chrome

### Configuration changes

_No response_

### Other

_No response_


Discussion:
I can't reproduce this, perhaps something is up with your install

```
Summary
URL: /api/logs/mail/
Status: 200
```

<img width="461" alt="Screenshot 2023-02-23 at 2 05 15 PM" src="https://user-images.githubusercontent.com/4887959/221041410-ca5d3a18-3e6d-4b64-a617-5b74027121f3.png">

<img width="326" alt="Screenshot 2023-02-23 at 2 05 20 PM" src="https://user-images.githubusercontent.com/4887959/221041405-161949be-e179-4860-b3cf-4291df2ecde6.png">
I've tested with two instances, same result on both. One of them is a fresh dockerized instance. I don't use any mail features. Could this have to do something with this?
Ah, I guess I assumed you were. The log file isn't created until e.g. a mail action runs.

I mean I suppose it could be changed to not return the mail endpoint if there is no log, but that feels a bit pedantic since a 404 at the mail endpoint communicates the same idea? Is there a use case for you where getting a 404 is not enough?
No, the 404 is sufficient, I guess it just didn't make much sense to me why the /logs/ endpoint would return the path to a logfile that doesn't exist, seems a bit inconsistent to me but it's not really that important.

Thanks

Resolving patch:
diff --git a/src/documents/views.py b/src/documents/views.py
--- 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 @@ def retrieve(self, request, pk=None, *args, **kwargs):
         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):


Test patch:
diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py
--- a/src/documents/tests/test_api.py
+++ b/src/documents/tests/test_api.py
@@ -1590,10 +1590,23 @@ def test_create_update_patch(self):
         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)