the singleton pattern now you can see what is happening in the program when things go wrong your log file is like black box on an aircraftit keeps tally of your program' execution when your program crashesyou can pop open the black box and see what happened leading up to the crashas well as where you should start looking for bug one line is better than twoso create function that handles opening and writing to the filedef log_message(msg)with open("filename log"" "as log_filelog_file write("{ }\nformat(msg)you can use this replacement for print statements wherever you want to record some state of your program for later reviewlog_message("save this for later"the log_message function opens file and appends the message passed to it to the file this is the dry principle in action dryyou ask it stands for don' repeat yourself at the most basic levelyou should hear little alarm go off in your mind whenever you feel tempted to copy and paste code whenever possibleyou should repackage the code in such way that you can reuse it without the need to copy and paste the lines somewhere else why should you go through the effort of rethinking code that workswhy not copy it and just change one or two piecesif your code is in one placeand you must change somethingyou only have to change it at that one place iffor instanceyou had the code for the logger scattered throughout your programand you decided to change the name of the file you write the logs toyou would have to change the name in many places you will find that this is recipe for disasterif you miss one line of output codesome of your log messages will go missing in programmingthe number one cause of mistakes is people if you need to work on project that spans thousands or even millions of lines of codeand you need to change something like log file nameyou can easily miss line here or there during your update whenever possibleeliminate the number of human interactions with your code good programmers write solid codegreat programmers write code that makes average programmers produce solid code one way of making the code bit more user-friendly is to have file in your project that contains nothing but the logging function this allows you to import the logging function into any file in your project all changes to the way logging is done in the project need only be made in this one file