Calmcode - logging: introduction

Using the python logging framework instead of print.

1 2 3 4 5 6 7

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.

This video has three files. To run it from the command line you need to be sure that you're in a virtualenv that has pandas installed.

job.py

import sys

from summarise import summary

if __name__ == "__main__":
    # this line will grab the ticker argument
    ticker = sys.argv[1]

    # this line will take the ticker and do the analysis
    print(f"The average stock price is {summary(ticker)}")

summarise.py

from fetch import download_data

def summary(ticker):
    dataf = download_data()
    return dataf[ticker].mean()

fetch.py

import pandas as pd

def download_data():
    url = 'https://calmcode.io/datasets/stocks.csv'
    return pd.read_csv(url)

You'll notice that everything runs fine when we run;

python job.py KLM

But things go wrong when we run;

python job.py GOOG

The debugging could be made easier if we had logging around. Sure, we could also use the python debugger but logging is a good habbit either way. In this series of videos we're going to explain how to set it up.