How to use the `logging` python module
I had been always
python3 script.py > out.txt 2>err.txt.
It was getting a little difficult to understand when what is happening and this seemed to hacky. So I started looking at alternatives and came across a built-in python library called
After going through a few different resource and StackOverflow answers, I came up with the following short script which works perfectly in my use case. Feel free to adjust the date-format/log-format as per your preference.
import logging logging.basicConfig( filename='script.log', filemode='w', #can do 'a' for append level=logging.DEBUG, format="%(asctime)s | %(levelname)s : %(message)s", datefmt="%d-%m-%Y %H:%M:%S ", ) logging.debug("debug message") logging.info("info message") logging.warning("warn message") logging.error("error message") logging.critical("critical message")