1

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

3 Answers3

1

You can add timestamps directly to the logger:

#Create format for logs
FORMAT = "%(asctime)s.%(msecs)03d', %(x)s, %(y)s"
#Log looks like: "m-d-yyyy h:m:s:ms {x}, {y}"

logger.setLevel(logging.INFO)

#Create console handler and set level
handler = logging.FileHandler("path/to/logfile")
handler.setLevel(logging.INFO)

#Create and set formatter
formatter = logging.Formatter(fmt=FORMAT, datefmt="%m-%d-%Y %H:%M:%S", defaults={"x": "0", "y": "1"})
handler.setFormatter(formatter)

#Add handler to logger
logger.addHandler(handler)

If you wish to keep your code the same way it is now: You could read the whole log file, strip the whitespace/newline chars from ends of lines, then write the whole text back to the file.

import re

#use r file mode to read file
with open('loghelperDEBUG.txt', 'r') as wf:
    lines= wf.readlines()

#Clean data to remove \n
new_data = []
for line in lines:
    #Checks if the line being examined ends with a date and then newline
    if(re.search("\d{2}:\d{2}:\d{2}\n$", line):
        line.replace("\n", "") #repalce newline with nothing
    new_data.append(line)

#w:Writes to a file and creates the file if it does not exist or overwrites an #existing file.
#Open the txt file, clears all contents and writes new contents (with removed #newlines)
with open('loghelperDEBUG.txt', 'w') as wf:
    wf.writelines(new_data)
    wf.write(f' -- {t}\n') #need to deal with newlines
mrblue6
  • 587
  • 2
  • 19
0

I wrote this in an online editior, but you could try doing some string manipulation on the phrase and only print / write when you have the full line that you want

list_of_words = ["one", "two", "three", "four"]

count = 1
curr_phrase = ""
for word in list_of_words:
    curr_phrase += word
    if count % 2 == 0:
        print(curr_phrase)
        curr_phrase = ""
    count += 1

This outputs

onetwo
threefour
ab319
  • 11
  • 2
0

Just use the basicConfig on the logger to write out directly to a filename with the specified format

import logging

# specify the filename to write to, the log level, 
# and the format of the log line.
logging.basicConfig(filename='loghelperDEBUG.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# write some logs!
logging.info("hello")
logging.debug("hello")

This creates the file loghelperDEBUG.txt with the contents

2023-03-21 12:22:08,205 - INFO - hello
2023-03-21 12:22:11,621 - DEBUG - hello

Taken from the python documentation on logging

https://docs.python.org/3/howto/logging.html

Nathan McCoy
  • 3,092
  • 1
  • 24
  • 46