| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node345.html" /> |
| 13 | <link rel="prev" href="multiple-destinations.html" /> |
| 14 | <link rel="parent" href="module-logging.html" /> |
| 15 | <link rel="next" href="node345.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.29.4 Sending and receiving logging events across a network |
| 18 | </title> |
| 19 | </head> |
| 20 | <body> |
| 21 | <DIV CLASS="navigation"> |
| 22 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 23 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 24 | <tr> |
| 25 | <td class='online-navigation'><a rel="prev" title="6.29.3 Logging to multiple" |
| 26 | href="multiple-destinations.html"><img src='../icons/previous.png' |
| 27 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 28 | <td class='online-navigation'><a rel="parent" title="6.29 logging " |
| 29 | href="module-logging.html"><img src='../icons/up.png' |
| 30 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 31 | <td class='online-navigation'><a rel="next" title="6.29.5 Handler Objects" |
| 32 | href="node345.html"><img src='../icons/next.png' |
| 33 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 34 | <td align="center" width="100%">Python Library Reference</td> |
| 35 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 36 | href="contents.html"><img src='../icons/contents.png' |
| 37 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 38 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 39 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 40 | <td class='online-navigation'><a rel="index" title="Index" |
| 41 | href="genindex.html"><img src='../icons/index.png' |
| 42 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 43 | </tr></table> |
| 44 | <div class='online-navigation'> |
| 45 | <b class="navlabel">Previous:</b> |
| 46 | <a class="sectref" rel="prev" href="multiple-destinations.html">6.29.3 Logging to multiple</A> |
| 47 | <b class="navlabel">Up:</b> |
| 48 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> |
| 49 | <b class="navlabel">Next:</b> |
| 50 | <a class="sectref" rel="next" href="node345.html">6.29.5 Handler Objects</A> |
| 51 | </div> |
| 52 | <hr /></div> |
| 53 | </DIV> |
| 54 | <!--End of Navigation Panel--> |
| 55 | |
| 56 | <H2><A NAME="SECTION0082940000000000000000"></A><A NAME="network-logging"></A> |
| 57 | <BR> |
| 58 | 6.29.4 Sending and receiving logging events across a network |
| 59 | |
| 60 | </H2> |
| 61 | |
| 62 | <P> |
| 63 | Let's say you want to send logging events across a network, and handle them |
| 64 | at the receiving end. A simple way of doing this is attaching a |
| 65 | <tt class="class">SocketHandler</tt> instance to the root logger at the sending end: |
| 66 | |
| 67 | <P> |
| 68 | <div class="verbatim"><pre> |
| 69 | import logging, logging.handlers |
| 70 | |
| 71 | rootLogger = logging.getLogger('') |
| 72 | rootLogger.setLevel(logging.DEBUG) |
| 73 | socketHandler = logging.handlers.SocketHandler('localhost', |
| 74 | logging.handlers.DEFAULT_TCP_LOGGING_PORT) |
| 75 | # don't bother with a formatter, since a socket handler sends the event as |
| 76 | # an unformatted pickle |
| 77 | rootLogger.addHandler(socketHandler) |
| 78 | |
| 79 | # Now, we can log to the root logger, or any other logger. First the root... |
| 80 | logging.info('Jackdaws love my big sphinx of quartz.') |
| 81 | |
| 82 | # Now, define a couple of other loggers which might represent areas in your |
| 83 | # application: |
| 84 | |
| 85 | logger1 = logging.getLogger('myapp.area1') |
| 86 | logger2 = logging.getLogger('myapp.area2') |
| 87 | |
| 88 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') |
| 89 | logger1.info('How quickly daft jumping zebras vex.') |
| 90 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') |
| 91 | logger2.error('The five boxing wizards jump quickly.') |
| 92 | </pre></div> |
| 93 | |
| 94 | <P> |
| 95 | At the receiving end, you can set up a receiver using the |
| 96 | <tt class="module">SocketServer</tt> module. Here is a basic working example: |
| 97 | |
| 98 | <P> |
| 99 | <div class="verbatim"><pre> |
| 100 | import cPickle |
| 101 | import logging |
| 102 | import logging.handlers |
| 103 | import SocketServer |
| 104 | import struct |
| 105 | |
| 106 | |
| 107 | class LogRecordStreamHandler(SocketServer.StreamRequestHandler): |
| 108 | """Handler for a streaming logging request. |
| 109 | |
| 110 | This basically logs the record using whatever logging policy is |
| 111 | configured locally. |
| 112 | """ |
| 113 | |
| 114 | def handle(self): |
| 115 | """ |
| 116 | Handle multiple requests - each expected to be a 4-byte length, |
| 117 | followed by the LogRecord in pickle format. Logs the record |
| 118 | according to whatever policy is configured locally. |
| 119 | """ |
| 120 | while 1: |
| 121 | chunk = self.connection.recv(4) |
| 122 | if len(chunk) < 4: |
| 123 | break |
| 124 | slen = struct.unpack(">L", chunk)[0] |
| 125 | chunk = self.connection.recv(slen) |
| 126 | while len(chunk) < slen: |
| 127 | chunk = chunk + self.connection.recv(slen - len(chunk)) |
| 128 | obj = self.unPickle(chunk) |
| 129 | record = logging.makeLogRecord(obj) |
| 130 | self.handleLogRecord(record) |
| 131 | |
| 132 | def unPickle(self, data): |
| 133 | return cPickle.loads(data) |
| 134 | |
| 135 | def handleLogRecord(self, record): |
| 136 | # if a name is specified, we use the named logger rather than the one |
| 137 | # implied by the record. |
| 138 | if self.server.logname is not None: |
| 139 | name = self.server.logname |
| 140 | else: |
| 141 | name = record.name |
| 142 | logger = logging.getLogger(name) |
| 143 | # N.B. EVERY record gets logged. This is because Logger.handle |
| 144 | # is normally called AFTER logger-level filtering. If you want |
| 145 | # to do filtering, do it at the client end to save wasting |
| 146 | # cycles and network bandwidth! |
| 147 | logger.handle(record) |
| 148 | |
| 149 | class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer): |
| 150 | """simple TCP socket-based logging receiver suitable for testing. |
| 151 | """ |
| 152 | |
| 153 | allow_reuse_address = 1 |
| 154 | |
| 155 | def __init__(self, host='localhost', |
| 156 | port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, |
| 157 | handler=LogRecordStreamHandler): |
| 158 | SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler) |
| 159 | self.abort = 0 |
| 160 | self.timeout = 1 |
| 161 | self.logname = None |
| 162 | |
| 163 | def serve_until_stopped(self): |
| 164 | import select |
| 165 | abort = 0 |
| 166 | while not abort: |
| 167 | rd, wr, ex = select.select([self.socket.fileno()], |
| 168 | [], [], |
| 169 | self.timeout) |
| 170 | if rd: |
| 171 | self.handle_request() |
| 172 | abort = self.abort |
| 173 | |
| 174 | def main(): |
| 175 | logging.basicConfig( |
| 176 | format="%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s") |
| 177 | tcpserver = LogRecordSocketReceiver() |
| 178 | print "About to start TCP server..." |
| 179 | tcpserver.serve_until_stopped() |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | main() |
| 183 | </pre></div> |
| 184 | |
| 185 | <P> |
| 186 | First run the server, and then the client. On the client side, nothing is |
| 187 | printed on the console; on the server side, you should see something like: |
| 188 | |
| 189 | <P> |
| 190 | <div class="verbatim"><pre> |
| 191 | About to start TCP server... |
| 192 | 59 root INFO Jackdaws love my big sphinx of quartz. |
| 193 | 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. |
| 194 | 69 myapp.area1 INFO How quickly daft jumping zebras vex. |
| 195 | 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. |
| 196 | 69 myapp.area2 ERROR The five boxing wizards jump quickly. |
| 197 | </pre></div> |
| 198 | |
| 199 | <P> |
| 200 | |
| 201 | <DIV CLASS="navigation"> |
| 202 | <div class='online-navigation'> |
| 203 | <p></p><hr /> |
| 204 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 205 | <tr> |
| 206 | <td class='online-navigation'><a rel="prev" title="6.29.3 Logging to multiple" |
| 207 | href="multiple-destinations.html"><img src='../icons/previous.png' |
| 208 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 209 | <td class='online-navigation'><a rel="parent" title="6.29 logging " |
| 210 | href="module-logging.html"><img src='../icons/up.png' |
| 211 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 212 | <td class='online-navigation'><a rel="next" title="6.29.5 Handler Objects" |
| 213 | href="node345.html"><img src='../icons/next.png' |
| 214 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 215 | <td align="center" width="100%">Python Library Reference</td> |
| 216 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 217 | href="contents.html"><img src='../icons/contents.png' |
| 218 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 219 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 220 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 221 | <td class='online-navigation'><a rel="index" title="Index" |
| 222 | href="genindex.html"><img src='../icons/index.png' |
| 223 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 224 | </tr></table> |
| 225 | <div class='online-navigation'> |
| 226 | <b class="navlabel">Previous:</b> |
| 227 | <a class="sectref" rel="prev" href="multiple-destinations.html">6.29.3 Logging to multiple</A> |
| 228 | <b class="navlabel">Up:</b> |
| 229 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> |
| 230 | <b class="navlabel">Next:</b> |
| 231 | <a class="sectref" rel="next" href="node345.html">6.29.5 Handler Objects</A> |
| 232 | </div> |
| 233 | </div> |
| 234 | <hr /> |
| 235 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 236 | </DIV> |
| 237 | <!--End of Navigation Panel--> |
| 238 | <ADDRESS> |
| 239 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 240 | </ADDRESS> |
| 241 | </BODY> |
| 242 | </HTML> |