CHAPTER 3

Logging

As you are exploring and, later, using bestPy you might want to keep track (in a discrete way) of what happens under the hood. For that purpose, a convenient logging faciĺity is built into bestPy that keeps you up to date.

Preliminaries

We only need this because the examples folder is a subdirectory of the bestPy package.


In [1]:
import sys
sys.path.append('../..')

Import

We are not going to actually recommend anything in the present chapter. We just want to take a closer look at the warnings issued when reading transaction data from CSV file in the last chapter 2. To recreate these warnings, all we need to import is Transactions from bestPy.datastructures.


In [2]:
from bestPy.datastructures import Transactions

Read transaction data


In [3]:
file = 'examples_data.csv'
data = Transactions.from_csv(file)


WARNING:root:Could not interpret transaction on line 1. Skipping.
WARNING:root:Could not interpret transaction on line 2. Skipping.

There they are again! While it is maybe helpful to have the warnings pop up like this in a Jupyter notbook, it is not clear how to benefit from this feature when writing a standalone python program or service. Also, having a lot of them might mess up your tidy notebook layout.

In fact, these messages aren't intended to pop up in the Jupyter notebook at all! Rather, they are intended to be written to a logfile together with other information (as well as some warnings and errors while your are still experimenting with bestPy). We will make it best practice, then, to always enable bestPys logging facilities before doing anything else. The logging function is conveniently accessible through the top-level package.

from bestPy import write_log_to

Tab completion reveals that the write_log_to() function has two arguments. The first is the path to and name of the logfile to be written and the second is the logging level, which can have the following (integer) values:

  • 10 ... debug
  • 20 ... info
  • 30 ... warning
  • 40 ... error
  • 50 ... critical

Any event with a logging level lower than the one specified will not appear in the logfile. You might want to start with 20 for info to learn which events are logged and then swotch to 30 for warning later.

To see how logging works in practice, you will first need to restart the Kernel of this Jupyter notebook (Menu: Kernel --> Restart). Then, we

  • make again sure we have bestPy in our PYTHONPATH
  • do our imports again
  • intialize logging
  • read transaction data again

In [1]:
import sys
sys.path.append('../..')

from bestPy import write_log_to
from bestPy.datastructures import Transactions

logfile = 'logfile.txt'
write_log_to(logfile, log_level=20)

file = 'examples_data.csv'
data = Transactions.from_csv(file)

And, behold, the warning messages are gone. Instead, we now have a file named logfile.txt in the examples directory, which contains the two lines

[WARNING ]: Could not interpret transaction on line 1. Skipping. (from_csv|from_csv)
[WARNING ]: Could not interpret transaction on line 2. Skipping. (from_csv|from_csv)

The first term in brackets indicates the severity of the logged event. This is followed by an explanatory message and, in parentheses, the names of the python module and the function that triggered the logging message. This helps you understand where things went wrong.

The logging facilities of bestPy are quite extensive and informative, so whenever you're working with bestPy, it might be a good idea to have a terminal open on the side with a tail -f on the logfile just to stay ahead of things.


In [ ]: