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. Implements a SOAPHandler class which |
---|
24 | can be used to form the basis of extended SOAP functionality. |
---|
25 | |
---|
26 | Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved. |
---|
27 | """ |
---|
28 | import string, logging, logging.handlers, types |
---|
29 | |
---|
30 | SOAP_MESSAGE = """<SOAP-ENV:Envelope |
---|
31 | xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" |
---|
32 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
---|
33 | xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
---|
34 | xmlns:logging="http://www.red-dove.com/logging" |
---|
35 | SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" |
---|
36 | > |
---|
37 | <SOAP-ENV:Body> |
---|
38 | <logging:log> |
---|
39 | %s |
---|
40 | </logging:log> |
---|
41 | </SOAP-ENV:Body> |
---|
42 | </SOAP-ENV:Envelope> |
---|
43 | """ |
---|
44 | |
---|
45 | class SOAPHandler(logging.Handler): |
---|
46 | """ |
---|
47 | A class which sends records to a SOAP server. |
---|
48 | """ |
---|
49 | def __init__(self, host, url): |
---|
50 | """ |
---|
51 | Initialize the instance with the host and the request URL |
---|
52 | """ |
---|
53 | logging.Handler.__init__(self) |
---|
54 | self.host = host |
---|
55 | self.url = url |
---|
56 | |
---|
57 | def emit(self, record): |
---|
58 | """ |
---|
59 | Send the record to the Web server as a SOAP message |
---|
60 | """ |
---|
61 | try: |
---|
62 | import httplib |
---|
63 | h = httplib.HTTP(self.host) |
---|
64 | h.putrequest("POST", self.url) |
---|
65 | keys = record.__dict__.keys() |
---|
66 | keys.sort() |
---|
67 | args = "" |
---|
68 | for key in keys: |
---|
69 | v = record.__dict__[key] |
---|
70 | if type(v) == types.StringType: |
---|
71 | t = "string" |
---|
72 | elif (type(v) == types.IntType) or (type(v) == types.LongType): |
---|
73 | t = "integer" |
---|
74 | elif type(v) == types.FloatType: |
---|
75 | t = "float" |
---|
76 | else: |
---|
77 | t = "string" |
---|
78 | args = args + "%12s<logging:%s xsi:type=\"xsd:%s\">%s</logging:%s>\n" % ("", |
---|
79 | key, t, str(v), key) |
---|
80 | data = SOAP_MESSAGE % args[:-1] |
---|
81 | h.putheader("Content-type", "text/plain; charset=\"utf-8\"") |
---|
82 | h.putheader("Content-length", str(len(data))) |
---|
83 | h.endheaders() |
---|
84 | #print data |
---|
85 | h.send(data) |
---|
86 | r = h.getreply() #can't do anything with the result |
---|
87 | f = h.getfile() |
---|
88 | if f: |
---|
89 | #print f.read() |
---|
90 | f.close() |
---|
91 | except: |
---|
92 | self.handleError(record) |
---|
93 | |
---|
94 | def main(): |
---|
95 | sh = SOAPHandler('localhost:%d' % logging.handlers.DEFAULT_SOAP_LOGGING_PORT, '/log') |
---|
96 | logger = logging.getLogger("log_test13") |
---|
97 | logging.getLogger("").setLevel(logging.DEBUG) |
---|
98 | logger.propagate = 0 |
---|
99 | logger.addHandler(sh) |
---|
100 | logger.info("Jackdaws love my big %s of %s", "sphinx", "quartz") |
---|
101 | logger.debug("Pack my %s with five dozen %s", "box", "liquor jugs") |
---|
102 | logger.removeHandler(sh) |
---|
103 | |
---|
104 | if __name__ == "__main__": |
---|
105 | main() |
---|
106 | |
---|