logo

... logging.


Print statements can only do so much. You also need to log to files if you want to debug something that is in production. That is why in this series of videos we'll show you how to set up logging for your project and to also show you how it works.


Notes

The main thing to remember is that you need to catch BaseException is you want to also see the trace.

job.py

import sys 

from logger import logger
from summarise import summary

if __name__ == "__main__":
    logger.debug("Program started.")
    try:
        ticker = sys.argv[1]
        logger.info(f"Will find summary for {ticker}.")
        print(f"The average stock price is {summary(ticker)}")
        logger.debug("Program ended with success.")
    except BaseException:
        logger.error("Error happened!", exc_info=True)

When you run this you should see the traceback appear in your logs.


Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.