Home > Articles > Programming > Python

Examining Python 2.3: New Additions

Boudewijn Rempt
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Python 2.3 is around the corner. What will this new release bring? Is it mere nerd-fodder, or is there more to the changes Guido van Rossum and his team are implementing? Boudewijn Rempt examines the most important additions to the language and libraries, and shows how to use them with examples.

Logging

Serious applications not only use a database (see Databases, GUIs, and Python), but they also need to log information. Too often, logging is seen as merely spouting debug information onto the console, with print statements, most likely. There is far more to logging than that: Logging is necessary to keep an audit trail of an application, monitor performance, and calculate usage statistics.

Python's new logging package owes a large debt to log4j, the well-regarded logging package for Java from the Apache project, but it isn't a direct translation from Java to Python. That's good because log4j can be a pain to work with. The logging module is thread-safe and very easy to use.

In fact, starting with the new logging module is simplicity itself: You can use just use it out of the box:

>>> import logging
>>> logging.warn("WARNING")
WARN:root:WARNING

The logging module is quite flexible and offers a large number of handlers—that is, modules that can send you logging information to all sorts of destinations, such as the console, a text file, and also the NT event log or UNIX's SysLog.

A real-world example of logging takes a bit more thought. You must determine your logging needs quite precisely before adding logging to your application. Python's logging module allows enables to create a hierarchy of loggers objects, with every logger having its own level.

For instance, a subsystem of your application can be responsible for handling requests. These requests need to go through two filters before being processed. In this case, you can have the following hierarchy:

requestLogger
requestLogger.filter1
requestLogger.filter2
requestLogger.process

You get a Logger object by asking the logging module for it, using the getLogger(name) function. The same name returns always the same object; if the name is not yet used, a new Logger object is created.

You can choose from many different Handler objects. The following example uses the SMTP handler to mail me a critical error that my script is about to finish:

import logging
from logging import handlers

logger = logging.getLogger("requestLogger")
logger.setLevel(logging.CRITICAL)
logger.addHandler(handlers.SMTPHandler("calcifer", 
                  "logger@calcifer", 
                  ["boud"], 
                  "CRITICAL error"))
                  
logger.critical("Script about to finish")
<logger.png>

The logging module works internally with LogRecord objects. You can create Formatter to create a custom string representation of these LogRecords. You can then set your formatter in the Handler object that you are using with setHandler. Be sure, however, that your formatter is optimized—otherwise, you might get a severe performance penalty from logging.

Filters can be created to limit the output of your logging system. They work on the name of your Logger object and can thus be configured to allow logging from only part of your hierarchy. You can add filters to both handlers and loggers, giving you a lot of flexibility.

Finally, when your logging system is in place, you will often want to allow the users of your application to fine-tune the information they receive. The logging module allows for configuration through a file, using the fileConfig class. The file format is simple enough, but the actual range of options can be bewildering, just as with log4j.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network