I am not able to get log file rotations to work in Python (v2.6.6). I need the naming control for log file extensions I find with TimedRotatingFileHandler, and the ability to force log file rotations at size thresholds I find with RotatingFileHandler.
If I establish a log handler fh of type TimedRotatingFileHandler I am able to include a DT stamp in the extension of the rotated log files. I do that like so:
fh = logging.handlers.TimedRotatingFileHandler('/mydirpath/logs/logFileBaseName', when='H', interval=1, backupCount=48)
fh.suffix = '%Y%m%d_%H%M%S'
fh.extMatch = re.compile(r"^\d{8}\_\d{6}$")
However, apparently if my python script is not actively writing to the logs when interval occurs, the logs will not rotate. The single log file grows and grows to extreme sizes.
As an alternative I tried a simple RotatingFileHandler. It seems to give me the ability to dictate log rotation because it allows me to dictate a size threshold. Once that threshold is reached or even as it is approached, the logs rotate. This is how fh is established in my case for this handler type:
fh = logging.handlers.RotatingFileHandler('/mydirpath/logs/logFileBaseName', maxBytes=7000000, backupCount=48)
Using RotatingFileHandler the logs are indeed rotated as expected, but it appears that I have no ability to add my DT stamp to the rotated log file extensions. As detailed in the documentation, I get extensions of .1, .2, .3, etc etc. I must have those DTs in the extensions of the rotated log files.
I suspect I need to use RotatingFileHandler but employ an override method to customize the extensions of the rotated log files. I'm not yet proficient at python, and do not know how I would identify the proper method in the logging package to override. I also do not know how I would establish such an override method in my python class to override a method in a package class.
I'm hoping to get some assistance. If I am misunderstanding or misusing the python rotating file handlers, I'd like to fix my error. If I am correct that a method override is required, I hope to get an example from someone in the community who has done something similar. Thanks in advance for any assistance.