Package zope :: Package testing :: Module loggingsupport
[show private | hide private]
[frames | no frames]

Module zope.testing.loggingsupport

Support for testing logging code

If you want to test that your code generates proper log output, you can create and install a handler that collects output:
>>> handler = InstalledHandler('foo.bar')

The handler is installed into loggers for all of the names passed. In addition, the logger level is set to 1, which means, log everything. If you want to log less than everything, you can provide a level keyword argument. The level setting effects only the named loggers.

Then, any log output is collected in the handler:
>>> logging.getLogger('foo.bar').exception('eek')
>>> logging.getLogger('foo.bar').info('blah blah')
>>> for record in handler.records:
...     print record.name, record.levelname
...     print ' ', record.getMessage()
foo.bar ERROR
  eek
foo.bar INFO
  blah blah
A similar effect can be gotten by just printing the handler:
>>> print handler
foo.bar ERROR
  eek
foo.bar INFO
  blah blah
After checking the log output, you need to uninstall the handler:
>>> handler.uninstall()
At which point, the handler won't get any more log output. Let's clear the handler:
>>> handler.clear()
>>> handler.records
[]
And then log something:
>>> logging.getLogger('foo.bar').info('blah')
and, sure enough, we still have no output:
>>> handler.records
[]
$Id: loggingsupport.py,v 1.2 2005/06/24 16:53:04 nyergler Exp $
Classes
Handler  
InstalledHandler  

Generated by Epydoc 2.1 on Fri Jun 24 12:01:23 2005 http://epydoc.sf.net