I have an application that makes use of multi-threading and is run in the background on a server. In order to monitor the application without having to log on to the server, I decided to include Bottle in order to respond to a few HTTP endpoints and report status, perform remote shutdown, etc.
I also wanted to add a way to consult the logfile. I could log using the FileHandler
and send the destination file when the URL is requested (e.g. /log
).
However, I was wondering if it'd be possible to implement something like a RotatingFileHandler
, but instead of logging to a file, logging to a variable (e.g. BytesIO
). This way, I could limit the log to the most recent information, while at the same time being able to return it to the browser as text instead of as a separate file download.
The RotatingFileHandler
requires a filename, so it's not an option to pass it a BytesIO
stream. Logging to a variable itself is perfectly doable (e.g. Capturing Python Log Output In A Variable), but I'm a bit stumped on how to do the rolling part.
Any thoughts, hints, suggestions would be greatly appreciated.
collections.deque
object to capture the log output? You would set a maximum length for the deque, and once it reaches this length old log items pop off the start as you append new log items. – Trample