Python/Logging: Logging module not picking up the right file path to write log files

Viewed 24

I am using the logging module in python to generate logs files in a folder. The log files are all perfectly generated. The problem is inspite of specifying the location, they write to the Desktop folder and not to the specified location. I have tried printing the file path and it all looks good. Please see the snippet of code.

file_folder = "C:\\Users\\xyzzz\\Desktop\\logs"
log_filename = f"log_{datetime.now().strftime('%d%m%y-%H%M%S')}"
log_filepath = os.path.join(file_folder, log_filename)
print(log_filepath)

logging.basicConfig(level=logging.INFO, filename=log_filename, filemode="w", format="%(asctime)s - %(levelname)s - %(message)s", force=True)

It always writes to the desktop and not to the folder I have specified. Can someone suggest what is the mistake here.

Thanks

1 Answers

You're never using your variable log_filepath that you're printing, instead you are using log_filename

In the last line, replace log_filename by log_filepath and it will probably work

Related