12 Logging and exception handling

 

This chapter covers

  • Logging to files
  • Formatting logs properly
  • Handling exceptions
  • Raising exceptions

When we move our application into production, we temporarily “lose” control of our product; we must rely on the product itself to behave. If we’ve been extremely careful during the development phase, we may be lucky enough to have a perfect product that has no bugs. This almost never happens, however. Thus, we should know that a variety of problems, such as an unusual amount of traffic to our web app, can occur. Should any problem arise, we don’t panic; we start the problem-solving process.

12.1 How do I monitor my program with logging?

12.1.1 Creating the Logger object to log application events

12.1.2 Using files to store application events

12.1.3 Adding multiple handlers to the logger

12.1.4 Discussion

12.1.5 Challenge

12.2 How do I save log records properly?

12.2.1 Categorizing application events with levels

12.2.2 Setting a handler’s level

12.2.3 Setting formats to the handler

12.2.4 Discussion

12.2.5 Challenge

12.3 How do I handle exceptions?

12.3.1 Handling exceptions with try. . .except. . .

12.3.2 Specifying the exception in the except clause

12.3.3 Handling multiple exceptions

12.3.4 Showing more information about an exception

12.3.5 Discussion

12.3.6 Challenge