My python code works fine to create a log file with name and date on which code was executed -
For example-
I run code today it will create log file - logfile_2020-01-15.log
I run code tomorrow it will create log file - logfile_2020-01-16.log
and so on.
Now the concern is, if my code execution is started today and it keeps on running for 8 days. It should create 8 log files - 1 file each day: logfile_2020-01-15.log to logfile_2020-01-23.log
But this is not happening. It keeps on logging in same file: logfile_2020-01-15.log
when the code was initiated.
Please can anyone help me modify code-
import datetime
import logging
import schedule
class Workflow:
def setupLoggingToFile():
logFilePath = "C:\ExceptionLogFiles\"
logdate = datetime.datetime.now().strftime('%Y-%m-%d')
logging.basicConfig(
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d-%y %H:%M:%S',
level=logging.DEBUG,
handlers=[RotatingFileHandler(logFilePath + "logfile_"+logdate+".log",maxBytes=10485760, backupCount=100)])
def StartWorkflow(self):
try:
print("New Cron Cycle Started..")
except Exception:
logging.exception("Something went wrong.", exc_info=True)
def StartCron(self):
try:
schedule.every(5).seconds.do(self.StartWorkflow)
while 1:
schedule.run_pending()
time.sleep(1)
except Exception:
logging.debug("CRON was unable to start. Something Wrong in StartCron function.")
logging.exception("CRON was unable to start. Something Wrong in StartCron function.", exc_info=True)
A = Workflow()
A.StartCron()