source: trunk/packages/xen-common/xen-common/tools/python/logging/logging-0.4.9.2/test/log_test11.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

  • Property svn:executable set to *
  • Property svn:mime-type set to text/script
File size: 2.9 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
4#
5# Permission to use, copy, modify, and distribute this software and its
6# documentation for any purpose and without fee is hereby granted,
7# provided that the above copyright notice appear in all copies and that
8# both that copyright notice and this permission notice appear in
9# supporting documentation, and that the name of Vinay Sajip
10# not be used in advertising or publicity pertaining to distribution
11# of the software without specific, written prior permission.
12# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
15# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
16# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19# This file is part of the Python logging distribution. See
20# http://www.red-dove.com/python_logging.html
21#
22"""Test harness for the logging module. Tests BufferingSMTPHandler, an alternative implementation
23of SMTPHandler.
24
25Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
26"""
27import string, logging, logging.handlers
28
29MAILHOST = 'beta'
30FROM     = 'log_test11@red-dove.com'
31TO       = ['arkadi_renko']
32SUBJECT  = 'Test Logging email from Python logging module (buffering)'
33
34class BufferingSMTPHandler(logging.handlers.BufferingHandler):
35    def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
36        logging.handlers.BufferingHandler.__init__(self, capacity)
37        self.mailhost = mailhost
38        self.mailport = None
39        self.fromaddr = fromaddr
40        self.toaddrs = toaddrs
41        self.subject = subject
42        self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s"))
43
44    def flush(self):
45        if len(self.buffer) > 0:
46            try:
47                import smtplib
48                port = self.mailport
49                if not port:
50                    port = smtplib.SMTP_PORT
51                smtp = smtplib.SMTP(self.mailhost, port)
52                msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (self.fromaddr, string.join(self.toaddrs, ","), self.subject)
53                for record in self.buffer:
54                    s = self.format(record)
55                    print s
56                    msg = msg + s + "\r\n"
57                smtp.sendmail(self.fromaddr, self.toaddrs, msg)
58                smtp.quit()
59            except:
60                self.handleError(None)  # no particular record
61            self.buffer = []
62
63def test():
64    logger = logging.getLogger("")
65    logger.setLevel(logging.DEBUG)
66    logger.addHandler(BufferingSMTPHandler(MAILHOST, FROM, TO, SUBJECT, 10))
67    for i in xrange(102):
68        logger.info("Info index = %d", i)
69    logging.shutdown()
70
71if __name__ == "__main__":
72    test()
Note: See TracBrowser for help on using the repository browser.