I am trying to implement the python logging handler 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 named 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?
Defines a handler which writes to a file, rotating the log after a time period derived from the given suffix string or after the size of the file grows beyond a certain point and keeping a fixed number of backups.
The StreamHandler class, located in the core logging package, sends logging output to streams such as sys. stdout, sys. stderr or any file-like object (or, more precisely, any object which supports write() and flush() methods).
There are two relevant parameters to TimedRotatingFileHandler : when , and interval . Most of the possible values for when , such as D for days, or H for hours, work in conjunction with interval —for example, if when='D', interval=7 were specified, then the log would be rotated every seven days.
"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
handler = logging.handlers.TimedRotatingFileHandler("C:\\isis_ops\\logs\\Rotate_Test",'midnight',1) handler.suffix = "%Y-%m-%d" # or anything else that strftime will allow root_logger.addHandler(handler)
The suffix is the formatting string.
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:
handler.suffix = "%Y%m%d"
handler.extMatch = re.compile(r"^\d{8}$")
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!
try:
import codecs
except ImportError:
codecs = None
import logging.handlers
import time
import os
class MyTimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self,dir_log):
self.dir_log = dir_log
filename = self.dir_log+time.strftime("%m%d%Y")+".txt" #dir_log here MUST be with os.sep on the end
logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='midnight', interval=1, backupCount=0, encoding=None)
def doRollover(self):
"""
TimedRotatingFileHandler remix - rotates logs on daily basis, and filename of current logfile is time.strftime("%m%d%Y")+".txt" always
"""
self.stream.close()
# get the time that this sequence started at and make it a TimeTuple
t = self.rolloverAt - self.interval
timeTuple = time.localtime(t)
self.baseFilename = self.dir_log+time.strftime("%m%d%Y")+".txt"
if self.encoding:
self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
else:
self.stream = open(self.baseFilename, 'w')
self.rolloverAt = self.rolloverAt + self.interval
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With