I wrote a program that is going to run for one whole day and I would like to capture the entire logging output in a manageable manner. Specifically, I would like to have the program receiving a maximum file size as a configuration parameter, and then have a logging handler in place to capture all logging but that ensures that, after execution halts, the output is NOT one single log file, but a collection of sequentially identified logging files, each with a file size under the specified given input parameter.
Could this be done by specifying a logging handler? I know that "Handler objects are responsible for dispatching the appropriate log messages (based on the log messages’ severity) to the handler’s specified destination".
Current research:
- This URL How to split a log file into several csv files with python is about already having one big file and then splitting it. I would like to generate the collection of files.
- I have checked https://docs.python.org/3/howto/logging.html but I do not see a specific example that allows me to control to maximum size of an item in a set of sequentially identified output files.
- I have checked a few handlers such as https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler . Particularly, this one allows to control the maximum file size per rotation, but this does NOT behave as I need to.
- I have checked https://docs.python.org/3/howto/logging-cookbook.html#using-file-rotation which look very close as to how I would like the output to be, but the file rotation is not what I require.
I believe that deriving the https://docs.python.org/3/library/logging.handlers.html#filehandler class to allow for this custom behavior would be a good idea, since my program is comprised of many modules, the logging of all of which I am interested in capturing.
Pseudo code of desired behavior: Hopefully, this pseudo code helps me convey the kind of behavior I have in mind.
file_name = "output.{id}.log"
max_log_file_size_byte = 26214400 # 25 MB.
file_number = 0
logging.basicConfig(filename=file_name.format(id=str(file_number)),
encoding='utf-8',
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
while True:
Take file 0 size in bytes.
if File 0 size > max_log_file_size_byte:
file_number = file_number + 1
logging.basicConfig(filename=file_name.format(id=str(file_number)),
encoding='utf-8',
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
App.Function1WhichNeedsToLog()
App.Function2WhichNeedsToLog()