1 '''
2 Defines a function for configuring the standard Python logging module, a class
3 that redirects print statements through the configured logging system, and
4 a class that defines a logging handler using XML-RPC as the transport.
5
6 @var LOG_FORMAT: Default log output format
7 @type LOG_FORMAT: string
8
9 @author: Peter Parente
10 @organization: IBM Corporation
11 @copyright: Copyright (c) 2005, 2007 IBM Corporation
12 @license: The BSD License
13
14 @author: Frank Zenker
15 @organization: IT Science Center Ruegen gGmbH, Germany
16 @copyright: Copyright (c) 2007, 2008 ITSC Ruegen
17 @license: The BSD License
18
19 All rights reserved. This program and the accompanying materials are made
20 available under the terms of the BSD license which accompanies
21 this distribution, and is available at
22 U{http://www.opensource.org/licenses/bsd-license.php}
23 '''
24 import logging, logging.config, sys, socket, xmlrpclib
25
26
27
28 LOG_FORMAT = '[%(name)s %(levelname)s %(asctime)s] %(message)s'
29
31 '''
32 Defines an XML-RPC handler for the standard Python logging system.
33
34 @ivar server: Server proxy mirroring methods on the server side
35 @type server: xmlrpclib.ServerProxy
36 '''
37 - def __init__(self, host='127.0.0.1', port=9000):
38 '''
39 Constructs a proxy to the server.
40
41 @param host: Hostname on which the server is running
42 @type host: string
43 @param port: Port on which the server is listening
44 @type port: integer
45 '''
46 logging.Handler.__init__(self)
47 self.server = xmlrpclib.ServerProxy('http://%s:%d' % (host, port))
48
49 - def emit(self, record):
50 '''
51 Writes interpolated string log messages to the server.
52
53 @param record: Record to log
54 @type record: logging.LogRecord
55 '''
56 if record is not None:
57 msg = self.format(record)
58 if msg is not None:
59 try:
60 self.server.write(msg)
61 except socket.error:
62
63 pass
64
65
66
67 logging.XMLRPCHandler = XMLRPCHandler
68
70 '''
71 Provides a dirt-simple interface compatible with stdout and stderr. When
72 assigned to sys.stdout or sys.stderr, an instance of this class redirects
73 print statements to the logging system. This means the result of the
74 print statements can be silenced, sent to a file, etc. using the command
75 line options to SUE. The log level used is defined by the L{LEVEL} constant
76 in this class.
77
78 @cvar LEVEL: Logging level for writes directed through this class, above
79 DEBUG and below INFO
80 @type LEVEL: integer
81 @ivar log: Reference to the Print log channel
82 @type log: logging.Logger
83 '''
84 LEVEL = 15
85
87 '''
88 Create the logger.
89 '''
90 self.log = logging.getLogger('print')
91 self.chunks = []
92
94 '''
95 Write the given data at the debug level to the logger. Stores chunks of
96 text until a new line is encountered, then sends to the logger.
97
98 @param data: Any object that can be converted to a string
99 @type data: stringable
100 '''
101 s = data.encode('utf-8')
102 if s.endswith('\n'):
103 self.chunks.append(s[:-1])
104 s = ''.join(self.chunks)
105 self.log.log(self.LEVEL, s)
106 self.chunks = []
107 else:
108 self.chunks.append(s)
109
165