catch_logs class

(Shortest import: from brian2.utils.logger import catch_logs)

class brian2.utils.logger.catch_logs(log_level=30)[source]

Bases: object

A context manager for catching log messages. Use this for testing the messages that are logged. Defaults to catching warning/error messages and this is probably the only real use case for testing. Note that while this context manager is active, all log messages are suppressed. Using this context manager returns a list of (log level, name, message) tuples.


log_level : int or str, optional

The log level above which messages are caught.


>>> logger = get_logger('brian2.logtest')
>>> logger.warn('An uncaught warning') 
WARNING  brian2.logtest: An uncaught warning
>>> with catch_logs() as l:
...    logger.warn('a caught warning')
...    print('l contains: %s' % l)
l contains: [('WARNING', 'brian2.logtest', 'a caught warning')]