im trying to use the logging module to save some logs in a external file, while using the .getLogger() attribute to change the logger name in another log file... still the one problem im having is just when im trying to write the log. It will always create a newline when i use the .write() attribute, and i dont know how to remove the newline(even tried several times using \n).. can someone help me with this?
#filename: loghelper.py
import logging
import datetime
from logging import DEBUG, WARNING, FileHandler, StreamHandler
logger = logging.getLogger(__name__)
logger.level= WARNING
logger.handlers = [FileHandler('loghelperDEBUG.txt', 'a')]
t = datetime.datetime.now(tz= None).strftime('%d-%m-%Y - %H:%M:%S')
with open('loghelperDEBUG.txt', 'r') as f:
fil = f.read()
n= 0
for i in fil:
if i == '\n':
n +=1
n+=1
logger.warning(f'LogHelper being used!, {n} times used')
print("")
print(f'loghelper used a total of {n} times by now.')
with open('loghelperDEBUG.txt', 'a') as wf:
wf.write(f' -- {t}\n') #need to deal with newlines
--this will always print in the loghelperDEBUG.txt file, when runned two times:
LogHelper being used!, 1 times used
21-03-2023 - 14:21:13
LogHelper being used!, 3 times used
21-03-2023 - 14:21:23
--expected:
LogHelper being used!, 1 times used -- 21-03-2023 - 14:21:13
LogHelper being used!, 2 times used -- 21-03-2023 - 14:21:23
you see, i only want the first \n instance for each line to disappear, and leave the other one. that way the program will count the lines in the for loop above