How could you write a unittest in Python that would test that the output from a logger is indeed in the format you expect (i.e. set through a call to logging.basicConfig())? I’m thinking along the lines of a custom StreamHandler and use of the ‘re’ library but it doesn’t look like LogRecord passed to StreamHandler.emit() can give me the string that will be output.
Share
From the documentation (http://packages.python.org/testfixtures/logging.html):
The examples are included in the documentation. The shortened version is below.
The context manager
And after that you can check the logs for equality:
The decorator
Similar to the previous, but applied to specific function:
Manual usage
After which you can also "check" the logs:
EDIT: To access specific logs and analyze them in a custom way, you can just iterate through
l.records(wherelis justLogCapture‘s instance) and access some properties of each of them (eg.msgcontains message sent to logger,levelnamecontains codename of the level, and there are plenty other properties).