def create_loghandler(self): return SummarisingLogger(self.section.fromaddr, self.section.toaddrs, self.mailhost(), self.section.subject, self.section.send_empty_entries, self.section.atexit, self.section.username, self.section.password, self.section.ignore, self.headers(), self.section.send_level, self.section.template, self.section.charset, self.section.content_type)
def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument('-C', default='/config', dest='config_folder') parser.add_argument('--no-email', action='store_true') args = parser.parse_args(argv) args.config_checker = 'svn' args.config_folder = os.path.abspath(args.config_folder) checkers = [] for line in open(os.path.join(args.config_folder, 'checker.txt'), 'rU'): line = line.strip() if not line: continue if line.startswith('#'): continue c = line.split(':', 1) if len(c) != 2: raise ValueError('No colon found on a line in checker.txt') if c[0] in special: setattr(args, c[0], c[1]) else: checkers.append(c) logger.setLevel(INFO) if getattr(args, 'email_to', None) and not args.no_email: to_ = [e.strip() for e in args.email_to.split(',')] # pragma: no branch from_ = getattr(args, 'email_from', to_[0]).strip() handler = SummarisingLogger( from_, to_, subject=getattr(args, 'email_subject', 'Checker output from %(hostname)s').strip(), mailhost=getattr(args, 'email_smtphost', 'localhost').strip(), send_empty_entries=False, ) else: handler = StreamHandler(sys.stdout) logger.addHandler(handler) for c in checkers: check(args.config_folder, *c) for checker in args.config_checker.split(','): check(args.config_folder, checker, args.config_folder)
def create(self, *args, **kw): kw['atexit'] = False self.handler = SummarisingLogger(*args, **kw) self.logger = logging.getLogger('') self.logger.addHandler(self.handler)
class TestSummarisingLogger(TestCase): def setUp(self): removeHandlers() DummySMTP.install(stdout=False) def tearDown(self): DummySMTP.remove() def test_imports(self): from mailinglogger.SummarisingLogger import SummarisingLogger from mailinglogger import SummarisingLogger def create(self, *args, **kw): kw['atexit'] = False self.handler = SummarisingLogger(*args, **kw) self.logger = logging.getLogger('') self.logger.addHandler(self.handler) def test_do_send_empty(self): self.create('*****@*****.**', ('*****@*****.**',)) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) def test_dont_send_empty(self): self.create('*****@*****.**', ('*****@*****.**',), send_empty_entries=False) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 0) def test_send_level_filters(self): self.create('*****@*****.**', ('*****@*****.**',), send_level=logging.CRITICAL) self.logger.warning('This line will not be sent') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 0) def test_send_level_includes_lower_level(self): self.create('*****@*****.**', ('*****@*****.**',), send_level=logging.CRITICAL) self.logger.warning('a warning') self.logger.critical('something critical') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) message_text = DummySMTP.sent[0][3] self.assertTrue('a warning' in message_text) self.assertTrue('something critical' in message_text) def test_tmpfile_goes_away(self): self.create('*****@*****.**', ('*****@*****.**',)) os.remove(self.handler.filename) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) def test_default_charset(self): self.create('*****@*****.**', ('*****@*****.**',), ) self.logger.critical(u"accentu\u00E9") logging.shutdown() m = DummySMTP.sent[0][3] # lovely, utf-8 encoded goodness self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) self.assertTrue('Content-Type: text/plain; charset="utf-8"' in m, m) self.assertTrue('\nYWNjZW50dcOp' in m, m) def test_specified_charset(self): self.create('*****@*****.**', ('*****@*****.**',), charset='iso-8859-1') self.logger.critical(u"accentu\u00E9") logging.shutdown() m = DummySMTP.sent[0][3] # lovely, latin-1 encoded goodness self.assertTrue('\naccentu=E9' in m, m) self.assertTrue( 'Content-Type: text/plain; charset="iso-8859-1"' in m, m) self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) def test_template(self): self.create('*****@*****.**', ('*****@*****.**',), template='<before>%s<after>') logging.critical('message') logging.shutdown() m = DummySMTP.sent[0][3] self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) self.assertTrue('<before>message\n<after>' in m, repr(m)) def test_specified_content_type(self): self.create('*****@*****.**', ('*****@*****.**',), content_type='foo/bar') self.logger.critical(u"message") logging.shutdown() m = DummySMTP.sent[0][3] # NB: we drop the 'foo' self.assertTrue('Content-Type: text/bar' in m, m) def test_flood_level_exceeded(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=3) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s') ) for i in range(10): logging.warning('message %s', i) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0][3] self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) self.assertTrue('\n'.join([ 'WARNING - message 0', 'WARNING - message 1', 'WARNING - message 2', 'CRITICAL - 2 messages not included as flood limit of 3 exceeded', 'WARNING - message 5', 'WARNING - message 6', 'WARNING - message 7', 'WARNING - message 8', ]) in m, repr(m)) def test_flood_highest_level_still_recorded(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=1) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s') ) logging.info('included') logging.warning('filtered') for i in range(5): logging.info('after %i', i) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0][3] self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) self.assertTrue('\n'.join([ 'INFO - included', 'CRITICAL - 1 messages not included as flood limit of 1 exceeded', 'INFO - after 0', ]) in m, repr(m)) def test_flood_except_for_tail(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=1) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s') ) logging.warning('message 1') logging.warning('message 2') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0][3] self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) self.assertTrue('\n'.join([ 'WARNING - message 1', 'WARNING - message 2', ]) in m, repr(m))
class TestSummarisingLogger(TestCase): def setUp(self): removeHandlers() DummySMTP.install() def tearDown(self): DummySMTP.remove() exit_handler_manager.clear_at_exit_handlers() def test_imports(self): pass def create(self, *args, **kw): kw['atexit'] = False self.handler = SummarisingLogger(*args, **kw) self.logger = logging.getLogger('') self.logger.addHandler(self.handler) def test_do_send_empty(self): self.create('*****@*****.**', ('*****@*****.**', )) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) def test_dont_send_empty(self): self.create('*****@*****.**', ('*****@*****.**', ), send_empty_entries=False) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 0) def test_send_level_filters(self): self.create('*****@*****.**', ('*****@*****.**', ), send_level=logging.CRITICAL) self.logger.warning('This line will not be sent') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 0) def test_send_level_includes_lower_level(self): self.create('*****@*****.**', ('*****@*****.**', ), send_level=logging.CRITICAL) self.logger.warning('a warning') self.logger.critical('something critical') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) message_text = DummySMTP.sent[0].msg _check_sent_message('a warning', message_text) _check_sent_message('something critical', message_text) def test_tmpfile_goes_away(self): self.create('*****@*****.**', ('*****@*****.**', )) os.remove(self.handler.filename) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) def test_default_charset(self): self.create( '*****@*****.**', ('*****@*****.**', ), ) self.logger.critical(u"accentu\u00E9") logging.shutdown() m = DummySMTP.sent[0].msg # lovely, utf-8 encoded goodness self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) self.assertTrue('Content-Type: text/plain; charset="utf-8"' in m, m) self.assertTrue('\nYWNjZW50dcOp' in m, m) def test_specified_charset(self): self.create('*****@*****.**', ('*****@*****.**', ), charset='iso-8859-1') self.logger.critical(u"accentu\u00E9") logging.shutdown() m = DummySMTP.sent[0].msg # lovely, latin-1 encoded goodness self.assertTrue('\naccentu=E9' in m, m) self.assertTrue('Content-Type: text/plain; charset="iso-8859-1"' in m, m) self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) def test_template(self): self.create('*****@*****.**', ('*****@*****.**', ), template='<before>%s<after>') logging.critical('message') logging.shutdown() m = DummySMTP.sent[0].msg self.assertTrue('Subject: Summary of Log Messages (CRITICAL)' in m, m) _check_sent_message('<before>message\n<after>', m) def test_specified_content_type(self): self.create('*****@*****.**', ('*****@*****.**', ), content_type='foo/bar') self.logger.critical(u"message") logging.shutdown() m = DummySMTP.sent[0].msg # NB: we drop the 'foo' self.assertTrue('Content-Type: text/bar' in m, m) def test_flood_level_exceeded(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=3) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s')) for i in range(10): logging.warning('message %s', i) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0].msg self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) _check_sent_message( '\n'.join([ 'WARNING - message 0', 'WARNING - message 1', 'WARNING - message 2', 'CRITICAL - 2 messages not included as flood limit of 3 exceeded', 'WARNING - message 5', 'WARNING - message 6', 'WARNING - message 7', 'WARNING - message 8', ]), m) def test_flood_highest_level_still_recorded(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=1) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s')) logging.info('included') logging.warning('filtered') for i in range(5): logging.info('after %i', i) logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0].msg self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) _check_sent_message( '\n'.join([ 'INFO - included', 'CRITICAL - 1 messages not included as flood limit of 1 exceeded', 'INFO - after 0', ]), m) def test_flood_except_for_tail(self): self.create('*****@*****.**', ('*****@*****.**', ), flood_level=1) self.handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s')) logging.warning('message 1') logging.warning('message 2') logging.shutdown() self.assertEqual(len(DummySMTP.sent), 1) m = DummySMTP.sent[0].msg self.assertTrue('Subject: Summary of Log Messages (WARNING)' in m, m) _check_sent_message( '\n'.join([ 'WARNING - message 1', 'WARNING - message 2', ]), m) def test_reopen(self): self.create('*****@*****.**', ('*****@*****.**', )) self.handler.reopen() logging.shutdown() self.assertEqual(len(DummySMTP.sent), 2) def test_safe_close(self): self.create('*****@*****.**', ('*****@*****.**', )) threads = [] for i in range(2): t = threading.Thread(target=self.handler.close) threads.append(t) t.start() [t.join() for t in threads] self.assertEqual(len(DummySMTP.sent), 1)