~ubuntu-branches/ubuntu/intrepid/xen-3.3/intrepid-proposed

« back to all changes in this revision

Viewing changes to tools/python/logging/logging-0.4.9.2/test/log_test9.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-08-14 10:28:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080814102857-a832fn5gowurz5do
Tags: upstream-3.3.0
ImportĀ upstreamĀ versionĀ 3.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""
 
23
A test harness for the logging module. Tests BufferingHandler, BufferingFormatter.
 
24
 
 
25
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
 
26
"""
 
27
import logging, logging.handlers
 
28
 
 
29
class XMLFormatter(logging.BufferingFormatter):
 
30
        """
 
31
        A formatter which formats a set of records using XML, using an example DTD called "logging.dtd".
 
32
        """
 
33
        def __init__(self):
 
34
            fmtstr = """
 
35
    <event name="%(name)s" level="%(levelno)d" filename="%(filename)s" lineno="%(lineno)d">
 
36
    <message>%(message)s</message>
 
37
    </event>"""
 
38
            logging.BufferingFormatter.__init__(self, logging.Formatter(fmtstr))
 
39
 
 
40
#   def formatHeader(self, records):
 
41
#       return """
 
42
#<?xml version="1.0" ?><!DOCTYPE eventSet SYSTEM "logging.dtd">
 
43
#<eventSet xmlns="http://www.red-dove.com/logging">"""
 
44
#
 
45
#   def formatFooter(self, records):
 
46
#       return "</eventSet>"
 
47
 
 
48
class XMLHandler(logging.handlers.BufferingHandler):
 
49
    def __init__(self, capacity):
 
50
        logging.handlers.BufferingHandler.__init__(self, capacity)
 
51
        self.setFormatter(XMLFormatter())
 
52
 
 
53
    def flush(self):
 
54
        if len(self.buffer) > 0:
 
55
            file = open("events.xml","w")
 
56
            file.write(self.formatter.format(self.buffer))
 
57
            file.close()
 
58
            self.buffer = []
 
59
 
 
60
def main():
 
61
    logger = logging.getLogger("")
 
62
    logger.setLevel(logging.DEBUG)
 
63
    xh = XMLHandler(10)
 
64
    logger.addHandler(xh)
 
65
    for i in xrange(100):
 
66
        logger.info("Info index = %d", i)
 
67
    xh.close()
 
68
    logger.removeHandler(xh)
 
69
 
 
70
if __name__ == "__main__":
 
71
    main()