Calmcode - logging: error

Logging for errors in Python.

1 2 3 4 5 6 7

In this video we show that you can also properly log errors.

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.