I'am trying to learn about logging. When i am created my first file handler, it has worked correctly. I created a file which named "Guesses_of_PC.log" and the handler has put it to the same path with py file.
But after i want that py project in another path so i created a new folder next to the py file and put that py file to new folder and also "Guesses_of_PC.log". But now, when i run py file, it still creates "Guesses_of_PC.log" in the first directory even i wrote new path to the handler. Why it behaves like that?
Thank you for your support. Here the code:
log_guess.setLevel(logging.INFO)
log_hand_guess=logging.FileHandler(filename="Guesses_of_PC.log")
log_guess.addHandler(log_hand_guess)
log_form_guess=logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
log_hand_guess.setFormatter(log_form_guess) ```
[1]: https://i.sstatic.net/BvUPm.png
Python resolve the relative path base on the working directory, and your working directory is the first directory. So it create the log file using first_folder/Guesses_of_PC.log
. To create the log file in the new folder, you need to use filename="new_folder/Guesses_of_PC.log"
, or change the working directory before execute the code. Sample code:
log_guess.setLevel(logging.INFO)
log_hand_guess=logging.FileHandler(filename="new_folder/Guesses_of_PC.log")
log_guess.addHandler(log_hand_guess)
log_form_guess=logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
log_hand_guess.setFormatter(log_form_guess)