I've scheduled my py file and wanted to write today's datetime into seperate log files after the code ran.
Test1:
from datetime import datetime, timedelta
import logging
sheet_list=['A','B','C','D']
def log(sheet):
today=datetime.today()
file_name='C:\\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\{}.log'.format(sheet)
print(file_name)
logging.basicConfig(filename = file_name,filemode = "w",level = logging.INFO)
logger = logging.getLogger()
logging.info(today)
return ("done!")
for sheet in sheet_list:
log=log(sheet)
Test1 output:
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\A.log
TypeError Traceback (most recent call last)
Cell In[2], line 2
----> 2 log=log(sheet)
TypeError: 'str' object is not callable
It only printed first sheet's file_name, but failed afterwards. Then I did Test2 to see if the error happened in Test1 was because I didn't do the function right. Although Test2 printed all the file_name, I only found 1 log file in my logs folder, which is A.log. And it records 4 logging info that was supposed to write into different log files.
Test2:
from datetime import datetime, timedelta
import logging
sheet_list=['A','B','C','D']
for sheet in sheet_list:
today=datetime.today()
file_name='C:\\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\{}.log'.format(sheet)
print(file_name)
logging.basicConfig(filename = file_name,filemode = "w",level = logging.INFO)
logger = logging.getLogger()
logging.info(today)
Test2 output:
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\A.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\B.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\C.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\D.log
A.log:
INFO:root:2023-06-30 11:04:49.034897
INFO:root:2023-06-30 11:04:49.036891
INFO:root:2023-06-30 11:04:49.036891
INFO:root:2023-06-30 11:04:49.036891
I need to know how to modify my code in Test1 to let it write datetime into four seperate log files. Thanks