programming practices – Python flask app creating log files from pods

I have a flask app which reads logs from my kubernetes based on the environment product name and product type.

I m creating the log files dynamically when a request arrives. If i choose a static folder for creating the log files, when there are simultaneous requests, how do I save the log files per request.

I also have an option to download the log file, so it is also that the file path should be known.

I took the approach of having a global variable SESSION which is a random string but I see that the issue persists when I have multiple requests and the global variable is manipulated multiple times.

I am also using a local session variable, but this doesnt work when i want to download the log files individually. The value of the var SESSION is lost after the first download of the file.

What is a best practice in this scenario?

Workflow:

  1. HTTP request : localhost:5000/env/product or localhost:5000/env
  2. Based on the Request URL, the log files are retrieved and saved as a file in the server.
  3. This files are being returned as a list with the possibility to be downloaded.
  4. ISSUE: WHen there are multiple requests, the same folder cannot be used.

I can add my code snippet if it is necessary.