![]() |
[Intermediate] How to create a log file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Tutorials (https://python-forum.io/forum-4.html) +---- Forum: Tutorial Requests and Submissions (https://python-forum.io/forum-21.html) +---- Thread: [Intermediate] How to create a log file (/thread-10794.html) |
How to create a log file - Panda - Jun-06-2018 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: 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: This is changeable. I have mine set to ('At %(asctime)s, This program returned %(message)s at level (%levelname)s')Which prints: 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 Other levels that work are critical, error, warning, and debug.Enjoy this new knowledge! If you get an error, please tell me. ![]() RE: How to create a log file - Panda - Jun-09-2018 I made a mistake. You must import "datetime" for the handler to be correct |