3
# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
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.
19
# This file is part of the Python logging distribution. See
20
# http://www.red-dove.com/python_logging.html
23
A test harness for the logging module. Tests BufferingHandler, BufferingFormatter.
25
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
27
import logging, logging.handlers
29
class XMLFormatter(logging.BufferingFormatter):
31
A formatter which formats a set of records using XML, using an example DTD called "logging.dtd".
35
<event name="%(name)s" level="%(levelno)d" filename="%(filename)s" lineno="%(lineno)d">
36
<message>%(message)s</message>
38
logging.BufferingFormatter.__init__(self, logging.Formatter(fmtstr))
40
# def formatHeader(self, records):
42
#<?xml version="1.0" ?><!DOCTYPE eventSet SYSTEM "logging.dtd">
43
#<eventSet xmlns="http://www.red-dove.com/logging">"""
45
# def formatFooter(self, records):
46
# return "</eventSet>"
48
class XMLHandler(logging.handlers.BufferingHandler):
49
def __init__(self, capacity):
50
logging.handlers.BufferingHandler.__init__(self, capacity)
51
self.setFormatter(XMLFormatter())
54
if len(self.buffer) > 0:
55
file = open("events.xml","w")
56
file.write(self.formatter.format(self.buffer))
61
logger = logging.getLogger("")
62
logger.setLevel(logging.DEBUG)
66
logger.info("Info index = %d", i)
68
logger.removeHandler(xh)
70
if __name__ == "__main__":