I'm new to logging module of python. I want to create a new log file everyday while my application is in running condition.
log file name - my_app_20170622.log
log file entries within time - 00:00:01 to 23:59:59
On next day I want to create a new log file with next day's date -
log file name - my_app_20170623.log
log file entries within time - 00:00:01 to 23:59:59
I'm using logging module of python.
I'm using like below -
log_level = int(log_level)
logger = logging.getLogger('simple')
logger.setLevel(log_level)
fh = logging.FileHandler(log_file_name)
fh.setLevel(log_level)
formatter = logging.Formatter(log_format)
fh.setFormatter(formatter)
logger.addHandler(fh)
Is their any configurations in logging module of python to create a log on daily basis?
I suggest you take a look at
logging.handlers.TimedRotatingFileHandler
. I think it's what you're looking for.You have to create a
TimedRotatingFileHandler
:This piece of code will create a
my_app.log
but the log will be moved to a new log file namedmy_app.log.20170623
when the current day ends at midnight.I hope this helps.
Finally, I got the correct answer and I want to share this answer.
Basically, need to create a TimedRotatingFileHandler like below -
This above code will generate file like my_app.log for current day and my_app.log.20170704 for previous day.
Hope it helps.