I am trying to implement the python logging handler called TimedRotatingFileHandler.
When it rolls over to midnight it appends the current day in the form: "YYYY-MM-DD".
LOGGING_MSG_FORMAT = '%(name)-14s > [%(levelname)s] [%(asctime)s] : %(message)s'
LOGGING_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(
level=logging.DEBUG,
format=LOGGING_MSG_FORMAT,
datefmt=LOGGING_DATE_FORMAT
)
root_logger = logging.getLogger('')
logger = logging.handlers.TimedRotatingFileHandler("C:\\logs\\Rotate_Test",'midnight',1)
root_logger.addHandler(logger)
while True:
daemon_logger = logging.getLogger('TEST')
daemon_logger.info("SDFKLDSKLFFJKLSDD")
time.sleep(60)
The first log file created is called just "Rotate_Test" then once it rolls over to the next day it changes the file name to: "Rotate_Test.YYYY-MM-DD" Where YYYY-MM-DD is the current day.
How can i change how it alters the filename? I googled and looked at the API and found pretty much nothing.
You can do this by changing the log suffix as suggested above but you will also need to change the extMatch variable to match the suffix for it to find rotated files:
Thanks.
I looked at the source.
There isn't really a way to change its form. Since manipulating suffix, only appends to the end of the file name. Ether way, there is no way real way to manipulate the full file name, what i was hoping for was where you can declare a file mask, and when it does the "RollOver" it will create a new file name based on the file mask. I am just going to go back to my original idea, was to just kill the whole logging subsystem and reinitialize it with the new file name when it RollsOver.
Thanks Tho.
There is another approach to this problem: for example, I need to rotate logs on a daily basis but they must be named with a suffix in the %m%d%Y format...
So I wrote a TimedRotatingFileHandler remix!
"How can i change how it alters the filename?"
Since it isn't documented, I elected to read the source. This is what I concluded from reading the source of
logging/handlers.py
The suffix is the formatting string.
Just an update, i ended up going a different approach.
The easiest way i found to modify the file output, was to simply use a FileHandler, then when it is time to do a roll over.
I do this:
Thats the gist of it. It took alot of poking and looking around but modifying the stream is the easiest way to do so.
:)