0

I'm trying to create logfiles for my script. It is important that a new file get's created by every run.

My script looks like this

from datetime import datetime as dt
import logging
import os

file_name = os.path.basename(__file__).split('.')[0]
file_ts = dt.now().strftime('%Y_%m_%d__%H_%M_%S')
log_file_name = f"C:/Program Files/WinPython3.9.5.0/Logfiles/{file_name}/{file_name}_{file_ts}.log"
err_log_file_name = f"C:/Program Files/WinPython3.9.5.0/Logfiles/{file_name}/error_{file_name}_{file_ts}.log"

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')

file_handler = logging.FileHandler(log_file_name)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)

file_handler_err = logging.FileHandler(err_log_file_name)
file_handler_err.setLevel(logging.ERROR)
file_handler_err.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(file_handler_err)

try:
    my code
    logger.debug(f"Dataframe for area xyz in year 123 has 567 values.")
except:
    logger.exception("Error message")

file_handler_err.close()
file_handler.close()
if os.path.getsize(err_log_file_name) == 0:
    os.remove(err_log_file_name)

The code works fine. Empty error logs get deleted, too.

My problem: even though a new logfile get's created every run, python writes the same logs into all existing logfiles. Which means that after a while, the first logfiles contains all logs from the following runs.

Any idea what I am doing wrong? How do I tell Python to write the logs only into the new logfile?

cvluepke
  • 85
  • 2
  • 9
  • I've never seen the behavior you describe, and I can't reproduce it with the code you've shown. This question needs a [mcve]. – 0x5453 May 25 '22 at 14:16
  • Are you perhaps running this code repeatedly in the same python interpreter? Or running the script multiple times in a single second? – 0x5453 May 25 '22 at 14:20
  • I added `logger.removeHandler(file_handler)` and `logger.removeHandler(file_handler_err)` just before the .close()-lines and now it seems to work. – cvluepke May 25 '22 at 14:27
  • @0x5453 it's possible that running the code repeatedly in the same python interpreter caused the problem – cvluepke May 25 '22 at 14:29

0 Answers0