logging:
format
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
We've introduced a new file.
logger.py
Important: do not name this file logging.py
.
import logging
logger = logging.getLogger(__name__)
# the handler determines where the logs go: stdout/file
handler = logging.StreamHandler()
# the formatter determines what our logs will look like
fmt = '%(levelname)s %(asctime)s %(filename)s %(funcName)s %(lineno)d %(message)s'
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.WARNING)
This file contains a logger that we can re-use. We're using it in all of our other files.
job.py
import sys
from logger import logger
from summarise import summary
if __name__ == "__main__":
ticker = sys.argv[1]
logger.warning(f"Will find summary for {ticker}")
print(f"The average stock price is {summary(ticker)}")
summarise.py
from fetch import download_data
from logger import logger
def summary(ticker):
logger.warning("About to download data.")
dataf = download_data()
logger.warning("Dataset downloaded.")
return dataf[ticker].mean()
fetch.py
import pandas as pd
from logger import logger
def download_data():
url = 'https://calmcode.io/datasets/stocks.csv'
logger.warning(f"Fetching from {url}")
reutrn pd.read_csv(url)
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.