import logging from unittest import TestCase from cStringIO import StringIO from mock import patch from rattail import logging as rattail_logging class TestLogging(TestCase): @patch('rattail.logging.os') @patch('rattail.logging.sys') @patch('rattail.logging.socket') @patch('rattail.logging.getpass') def test_adapter_adds_all_context(self, getpass, socket, sys, os): socket.getfqdn.return_value = 'testing.rattailproject.org' socket.gethostbyname.return_value = '127.0.0.1' sys.argv = ['just', 'testing'] os.getuid.return_value = 420 getpass.getuser.return_value = 'joeschmoe' formatter = logging.Formatter(u"%(hostname)s %(hostip)s %(argv)s %(username)s %(uid)s %(levelname)s %(message)s") string = StringIO() handler = logging.StreamHandler(string) handler.setFormatter(formatter) log = logging.getLogger('fake_for_testing') log.addHandler(handler) log.propagate = False log = rattail_logging.RattailAdapter(log) self.assertEqual(string.getvalue(), "") log.debug("some random thing") self.assertEqual(string.getvalue(), u"testing.rattailproject.org 127.0.0.1 ['just', 'testing'] joeschmoe 420 DEBUG some random thing\n") string.close()