Writing log files
The next technique we will look at is having our application output a log file. This allows us to get a better understanding of what was happening at the time an application failed, which can provide key information into finding the cause of the failure, especially when the failure is being reported by a user of your application.
We will add some logging statements to the Calculator.py
and Operation.py
files. To do this, we must first add the import for the logging module (https://docs.python.org/2/library/logging.html) to the start of each python file, which is simply:
import logging
In the Operation.py
file, we will add two logging calls in the evaluate
function, as shown in the following code:
def evaluate(self, a, b): logging.getLogger(__name__).info("Evaluating operation: %s" % (self._operation)) logging.getLogger(__name__).debug("RHS: %f, LHS: %f" % (a, b))
This will output two logging statements: one at the debug level and one at the information...