I have a python test in which I want to test if the logging works properly. For example I have a function that creates a user and at the end the logging writes to log file the response.
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
handler = logging.handlers.WatchedFileHandler('mylogfile.log')
formatter = logging.Formatter('%(asctime)s: %(message)s',
'%d/%b/%Y:%H:%M:%S %z')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('Some log text')
In my test case I want to send the log output to the StringIO.
class MyTest(unittest.TestCase):
def setUp(self):
stream = StringIO()
self.handler = logging.StreamHandler(stream)
log = logging.getLogger('mylogger')
log.removeHandler(log.handlers[0])
log.addHandler(self.handler)
def tearDown(self):
log = logging.getLogger('mylogger')
log.removeHandler(self.handler)
self.handler.close()
The problem is that I'm not sure how should I test wheter or not my logger is working.
stream.getvalue()
? – Aegaeon