I am using a Flask app to solve vehicle routing problems. It receives large requests (around 5MB each) that I would like to log outside of the usual logging messages (e.g. app.logger.info(..)
) because they just render the logs unreadable.
My idea is to save the last n requests as json files with timestamps in a separate folder. I am about to write my own function to do this. I am wondering if you have used this type of setup before and how you implemented it. I couldn't find anything in Python's module logging
to do this for me.
I think you should ignore the logging module in this case.
Take a directory and create the files in this directory.
You can use the current datetime for the file_name.
It has a paranoid way of creating a unique name:
The part "deleting old entries in the directory" is left undone. This is not difficult since the file names can be sorted.
I wouldn't recommend using the logger module to save large json files, especially if you also want them formatted for human consumption.
I actually had the same problem as you in the past, and solved it writing the following function:
You could format the file_to_write variable to be a unix or utc timestamp - that's what I do anyway.
Edit: added the "max n files" part. Just make sure you're using the right pattern with glob!
If you want to use python logger, you can specify where it writes using
FileHandler
like this: