Jun-06-2018, 11:38 PM
In python, if your program crashes, you might want to know where it crashed. You can create a log file in order to see exactly where and when your file crashed. If you want to create a log file, you need to import "logging", a library the can allow you to create log files.
Before you run anything inside your python file, you would need to type the following items:
On windows, open Explorer. Go to "This PC", and click on OS (C:). Go to Users/{NAME}/The location of your file.
The location of your file will most likely be on Desktop, but sometimes it is in a special folder. Once you find that file, fill in the information of its location into "Path to the file". Just a warning, you can NOT include C:\
. Just start with what goes past C:\ instead. I recommend creating a "logs" folder, because the program will create a new log file each time the program is turned on, because we added format into the hdlr. Next, add a formatter.
I do not know how to do this on Mac
I am learning how to do this on Linux
Which prints:
Enjoy this new knowledge! If you get an error, please tell me.
Before you run anything inside your python file, you would need to type the following items:
import logging logger = logging.getLogger('myApp')The command
logger = logging.getLogger('myApp')is very important, but the 'myApp' can be set to anything. I myself set it to the python file's name. After this, type the handler.
import logging logger = logging.getLogger('myApp') hdlr = logging.FileHandler('/Path to the file/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f')))Where it says "/Path to the log file/" type the explorer path to the log file.
On windows, open Explorer. Go to "This PC", and click on OS (C:). Go to Users/{NAME}/The location of your file.
The location of your file will most likely be on Desktop, but sometimes it is in a special folder. Once you find that file, fill in the information of its location into "Path to the file". Just a warning, you can NOT include C:\

I do not know how to do this on Mac
I am learning how to do this on Linux
import logging logger = logging.getLogger('myApp') hdlr = logging.FileHandler('/Path to the file/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f'))) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')With what the formatter is set to, this is how to program will print:
Output:YEAR-MONTH-DAY HOUR:MINUTE:SECOND,MILLISECOND (DEBUG, INFO, WARNING, ERROR, or CRITICAL) (MESSAGE)
This is changeable. I have mine set to ('At %(asctime)s, This program returned %(message)s at level (%levelname)s')Which prints:
Output:At YEAR-MONTH-DAY HOUR:MINUTE:SECOND,MILLISECOND, This program returned (MESSAGE) at level (DEBUG, INFO, WARNING, ERROR, or CRITICAL)
Next, we need to set the formatter and the handler to go with logger.import logging logger = logging.getLogger('myApp') hdlr = logging.FileHandler('/Path to the file/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f'))) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr)What you need to add next is the log level.
import logging logger = logging.getLogger('myApp') hdlr = logging.FileHandler('/Path to the file/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f'))) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG)And, congratulations! A log file will be made. To add stuff to the log file, type
logger.info('Hello there!')to print
Output:{THE TIME} INFO Hello there!
Other levels that work are critical, error, warning, and debug.Enjoy this new knowledge! If you get an error, please tell me.
