astropy/astropy — pull request #183 by astrofrog (2012-03-24 15:57:08+00:00)
Title: Implemented messaging system

This is a prototype implementation of a messaging system that improves significantly on #93 and addresses comments on the astropy-dev mailing list. There are no tests and documentation yet (I will do this if we agree on the implementation).

The current implementation does not depend on the logging module, but instead uses a custom Message class to store information about message levels, content, and origin. The use of the name `messaging` is to avoid confusion (`logging`/`logger` made people think of log files).

The main improvement in practice compared to #93 is that messages are stored as classes, and can contain more information than a simple string. At the moment, there is no ``Messenger`` class, in the sense that there is a single messenger, the module itself (unlike the logging module where one can instantiate different loggers for different parts of the code using getLogger). If we feel that there is a need for multiple messengers, we can implement getMessenger which would return a Messenger class, but we should think about whether this is really needed.

One nifty feature is that it is possible to catch messages with the Python ``with`` syntax:

with messaging.catch_messages() as messages:
messaging.debug("Nothing much here")
messaging.info("For information")

`messages` is then a list of `Message` instances that can be examined. I've also implemented the ability to catch only specific messages by level or by origin:

with messaging.catch_messages(filter_level=messaging.INFO) as messages:
messaging.debug("Nothing much here")
messaging.info("For information")

The above will catch only `INFO` messages. There is also a ``filter_origin`` argument, but I don't think it will work because ``find_current_module`` returns an object that I need to figure out how we compare to module names (@eteq - any ideas?).

Anyway, I've posted an example script you can use here to test things out: https://gist.github.com/2184446

As I said, this is just a prototype at this stage, so extensive comments/thoughts are welcome! Once we converge on this, I'll post to the mailing list to ask for further comments.