Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1 | .. _logging-cookbook: |
| 2 | |
| 3 | ================ |
| 4 | Logging Cookbook |
| 5 | ================ |
| 6 | |
| 7 | :Author: Vinay Sajip <vinay_sajip at red-dove dot com> |
| 8 | |
Georg Brandl | 375aec2 | 2011-01-15 17:03:02 +0000 | [diff] [blame] | 9 | This page contains a number of recipes related to logging, which have been found |
| 10 | useful in the past. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 11 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 12 | .. currentmodule:: logging |
| 13 | |
| 14 | Using logging in multiple modules |
| 15 | --------------------------------- |
| 16 | |
Vinay Sajip | 1397ce1 | 2010-12-24 12:03:48 +0000 | [diff] [blame] | 17 | Multiple calls to ``logging.getLogger('someLogger')`` return a reference to the |
| 18 | same logger object. This is true not only within the same module, but also |
| 19 | across modules as long as it is in the same Python interpreter process. It is |
| 20 | true for references to the same object; additionally, application code can |
| 21 | define and configure a parent logger in one module and create (but not |
| 22 | configure) a child logger in a separate module, and all logger calls to the |
| 23 | child will pass up to the parent. Here is a main module:: |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 24 | |
| 25 | import logging |
| 26 | import auxiliary_module |
| 27 | |
| 28 | # create logger with 'spam_application' |
| 29 | logger = logging.getLogger('spam_application') |
| 30 | logger.setLevel(logging.DEBUG) |
| 31 | # create file handler which logs even debug messages |
| 32 | fh = logging.FileHandler('spam.log') |
| 33 | fh.setLevel(logging.DEBUG) |
| 34 | # create console handler with a higher log level |
| 35 | ch = logging.StreamHandler() |
| 36 | ch.setLevel(logging.ERROR) |
| 37 | # create formatter and add it to the handlers |
| 38 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 39 | fh.setFormatter(formatter) |
| 40 | ch.setFormatter(formatter) |
| 41 | # add the handlers to the logger |
| 42 | logger.addHandler(fh) |
| 43 | logger.addHandler(ch) |
| 44 | |
| 45 | logger.info('creating an instance of auxiliary_module.Auxiliary') |
| 46 | a = auxiliary_module.Auxiliary() |
| 47 | logger.info('created an instance of auxiliary_module.Auxiliary') |
| 48 | logger.info('calling auxiliary_module.Auxiliary.do_something') |
| 49 | a.do_something() |
| 50 | logger.info('finished auxiliary_module.Auxiliary.do_something') |
| 51 | logger.info('calling auxiliary_module.some_function()') |
| 52 | auxiliary_module.some_function() |
| 53 | logger.info('done with auxiliary_module.some_function()') |
| 54 | |
| 55 | Here is the auxiliary module:: |
| 56 | |
| 57 | import logging |
| 58 | |
| 59 | # create logger |
| 60 | module_logger = logging.getLogger('spam_application.auxiliary') |
| 61 | |
| 62 | class Auxiliary: |
| 63 | def __init__(self): |
| 64 | self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary') |
| 65 | self.logger.info('creating an instance of Auxiliary') |
| 66 | def do_something(self): |
| 67 | self.logger.info('doing something') |
| 68 | a = 1 + 1 |
| 69 | self.logger.info('done doing something') |
| 70 | |
| 71 | def some_function(): |
| 72 | module_logger.info('received a call to "some_function"') |
| 73 | |
| 74 | The output looks like this:: |
| 75 | |
| 76 | 2005-03-23 23:47:11,663 - spam_application - INFO - |
| 77 | creating an instance of auxiliary_module.Auxiliary |
| 78 | 2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO - |
| 79 | creating an instance of Auxiliary |
| 80 | 2005-03-23 23:47:11,665 - spam_application - INFO - |
| 81 | created an instance of auxiliary_module.Auxiliary |
| 82 | 2005-03-23 23:47:11,668 - spam_application - INFO - |
| 83 | calling auxiliary_module.Auxiliary.do_something |
| 84 | 2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO - |
| 85 | doing something |
| 86 | 2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO - |
| 87 | done doing something |
| 88 | 2005-03-23 23:47:11,670 - spam_application - INFO - |
| 89 | finished auxiliary_module.Auxiliary.do_something |
| 90 | 2005-03-23 23:47:11,671 - spam_application - INFO - |
| 91 | calling auxiliary_module.some_function() |
| 92 | 2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO - |
| 93 | received a call to 'some_function' |
| 94 | 2005-03-23 23:47:11,673 - spam_application - INFO - |
| 95 | done with auxiliary_module.some_function() |
| 96 | |
| 97 | Multiple handlers and formatters |
| 98 | -------------------------------- |
| 99 | |
| 100 | Loggers are plain Python objects. The :func:`addHandler` method has no minimum |
| 101 | or maximum quota for the number of handlers you may add. Sometimes it will be |
| 102 | beneficial for an application to log all messages of all severities to a text |
| 103 | file while simultaneously logging errors or above to the console. To set this |
| 104 | up, simply configure the appropriate handlers. The logging calls in the |
| 105 | application code will remain unchanged. Here is a slight modification to the |
| 106 | previous simple module-based configuration example:: |
| 107 | |
| 108 | import logging |
| 109 | |
| 110 | logger = logging.getLogger('simple_example') |
| 111 | logger.setLevel(logging.DEBUG) |
| 112 | # create file handler which logs even debug messages |
| 113 | fh = logging.FileHandler('spam.log') |
| 114 | fh.setLevel(logging.DEBUG) |
| 115 | # create console handler with a higher log level |
| 116 | ch = logging.StreamHandler() |
| 117 | ch.setLevel(logging.ERROR) |
| 118 | # create formatter and add it to the handlers |
| 119 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 120 | ch.setFormatter(formatter) |
| 121 | fh.setFormatter(formatter) |
| 122 | # add the handlers to logger |
| 123 | logger.addHandler(ch) |
| 124 | logger.addHandler(fh) |
| 125 | |
| 126 | # 'application' code |
| 127 | logger.debug('debug message') |
| 128 | logger.info('info message') |
| 129 | logger.warn('warn message') |
| 130 | logger.error('error message') |
| 131 | logger.critical('critical message') |
| 132 | |
| 133 | Notice that the 'application' code does not care about multiple handlers. All |
| 134 | that changed was the addition and configuration of a new handler named *fh*. |
| 135 | |
| 136 | The ability to create new handlers with higher- or lower-severity filters can be |
| 137 | very helpful when writing and testing an application. Instead of using many |
| 138 | ``print`` statements for debugging, use ``logger.debug``: Unlike the print |
| 139 | statements, which you will have to delete or comment out later, the logger.debug |
| 140 | statements can remain intact in the source code and remain dormant until you |
| 141 | need them again. At that time, the only change that needs to happen is to |
| 142 | modify the severity level of the logger and/or handler to debug. |
| 143 | |
| 144 | .. _multiple-destinations: |
| 145 | |
| 146 | Logging to multiple destinations |
| 147 | -------------------------------- |
| 148 | |
| 149 | Let's say you want to log to console and file with different message formats and |
| 150 | in differing circumstances. Say you want to log messages with levels of DEBUG |
| 151 | and higher to file, and those messages at level INFO and higher to the console. |
| 152 | Let's also assume that the file should contain timestamps, but the console |
| 153 | messages should not. Here's how you can achieve this:: |
| 154 | |
| 155 | import logging |
| 156 | |
| 157 | # set up logging to file - see previous section for more details |
| 158 | logging.basicConfig(level=logging.DEBUG, |
| 159 | format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', |
| 160 | datefmt='%m-%d %H:%M', |
| 161 | filename='/temp/myapp.log', |
| 162 | filemode='w') |
| 163 | # define a Handler which writes INFO messages or higher to the sys.stderr |
| 164 | console = logging.StreamHandler() |
| 165 | console.setLevel(logging.INFO) |
| 166 | # set a format which is simpler for console use |
| 167 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') |
| 168 | # tell the handler to use this format |
| 169 | console.setFormatter(formatter) |
| 170 | # add the handler to the root logger |
| 171 | logging.getLogger('').addHandler(console) |
| 172 | |
| 173 | # Now, we can log to the root logger, or any other logger. First the root... |
| 174 | logging.info('Jackdaws love my big sphinx of quartz.') |
| 175 | |
| 176 | # Now, define a couple of other loggers which might represent areas in your |
| 177 | # application: |
| 178 | |
| 179 | logger1 = logging.getLogger('myapp.area1') |
| 180 | logger2 = logging.getLogger('myapp.area2') |
| 181 | |
| 182 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') |
| 183 | logger1.info('How quickly daft jumping zebras vex.') |
| 184 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') |
| 185 | logger2.error('The five boxing wizards jump quickly.') |
| 186 | |
| 187 | When you run this, on the console you will see :: |
| 188 | |
| 189 | root : INFO Jackdaws love my big sphinx of quartz. |
| 190 | myapp.area1 : INFO How quickly daft jumping zebras vex. |
| 191 | myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack. |
| 192 | myapp.area2 : ERROR The five boxing wizards jump quickly. |
| 193 | |
| 194 | and in the file you will see something like :: |
| 195 | |
| 196 | 10-22 22:19 root INFO Jackdaws love my big sphinx of quartz. |
| 197 | 10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. |
| 198 | 10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex. |
| 199 | 10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. |
| 200 | 10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly. |
| 201 | |
| 202 | As you can see, the DEBUG message only shows up in the file. The other messages |
| 203 | are sent to both destinations. |
| 204 | |
| 205 | This example uses console and file handlers, but you can use any number and |
| 206 | combination of handlers you choose. |
| 207 | |
| 208 | |
| 209 | Configuration server example |
| 210 | ---------------------------- |
| 211 | |
| 212 | Here is an example of a module using the logging configuration server:: |
| 213 | |
| 214 | import logging |
| 215 | import logging.config |
| 216 | import time |
| 217 | import os |
| 218 | |
| 219 | # read initial config file |
| 220 | logging.config.fileConfig('logging.conf') |
| 221 | |
| 222 | # create and start listener on port 9999 |
| 223 | t = logging.config.listen(9999) |
| 224 | t.start() |
| 225 | |
| 226 | logger = logging.getLogger('simpleExample') |
| 227 | |
| 228 | try: |
| 229 | # loop through logging calls to see the difference |
| 230 | # new configurations make, until Ctrl+C is pressed |
| 231 | while True: |
| 232 | logger.debug('debug message') |
| 233 | logger.info('info message') |
| 234 | logger.warn('warn message') |
| 235 | logger.error('error message') |
| 236 | logger.critical('critical message') |
| 237 | time.sleep(5) |
| 238 | except KeyboardInterrupt: |
| 239 | # cleanup |
| 240 | logging.config.stopListening() |
| 241 | t.join() |
| 242 | |
| 243 | And here is a script that takes a filename and sends that file to the server, |
| 244 | properly preceded with the binary-encoded length, as the new logging |
| 245 | configuration:: |
| 246 | |
| 247 | #!/usr/bin/env python |
| 248 | import socket, sys, struct |
| 249 | |
Vinay Sajip | 689b68a | 2010-12-22 15:04:15 +0000 | [diff] [blame] | 250 | with open(sys.argv[1], 'rb') as f: |
| 251 | data_to_send = f.read() |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 252 | |
| 253 | HOST = 'localhost' |
| 254 | PORT = 9999 |
| 255 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 256 | print('connecting...') |
| 257 | s.connect((HOST, PORT)) |
| 258 | print('sending config...') |
| 259 | s.send(struct.pack('>L', len(data_to_send))) |
| 260 | s.send(data_to_send) |
| 261 | s.close() |
| 262 | print('complete') |
| 263 | |
| 264 | |
| 265 | Dealing with handlers that block |
| 266 | -------------------------------- |
| 267 | |
| 268 | .. currentmodule:: logging.handlers |
| 269 | |
| 270 | Sometimes you have to get your logging handlers to do their work without |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 271 | blocking the thread you're logging from. This is common in Web applications, |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 272 | though of course it also occurs in other scenarios. |
| 273 | |
| 274 | A common culprit which demonstrates sluggish behaviour is the |
| 275 | :class:`SMTPHandler`: sending emails can take a long time, for a |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 276 | number of reasons outside the developer's control (for example, a poorly |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 277 | performing mail or network infrastructure). But almost any network-based |
| 278 | handler can block: Even a :class:`SocketHandler` operation may do a |
| 279 | DNS query under the hood which is too slow (and this query can be deep in the |
| 280 | socket library code, below the Python layer, and outside your control). |
| 281 | |
| 282 | One solution is to use a two-part approach. For the first part, attach only a |
| 283 | :class:`QueueHandler` to those loggers which are accessed from |
| 284 | performance-critical threads. They simply write to their queue, which can be |
| 285 | sized to a large enough capacity or initialized with no upper bound to their |
| 286 | size. The write to the queue will typically be accepted quickly, though you |
Georg Brandl | 375aec2 | 2011-01-15 17:03:02 +0000 | [diff] [blame] | 287 | will probably need to catch the :exc:`queue.Full` exception as a precaution |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 288 | in your code. If you are a library developer who has performance-critical |
| 289 | threads in their code, be sure to document this (together with a suggestion to |
| 290 | attach only ``QueueHandlers`` to your loggers) for the benefit of other |
| 291 | developers who will use your code. |
| 292 | |
| 293 | The second part of the solution is :class:`QueueListener`, which has been |
| 294 | designed as the counterpart to :class:`QueueHandler`. A |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 295 | :class:`QueueListener` is very simple: it's passed a queue and some handlers, |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 296 | and it fires up an internal thread which listens to its queue for LogRecords |
| 297 | sent from ``QueueHandlers`` (or any other source of ``LogRecords``, for that |
| 298 | matter). The ``LogRecords`` are removed from the queue and passed to the |
| 299 | handlers for processing. |
| 300 | |
| 301 | The advantage of having a separate :class:`QueueListener` class is that you |
| 302 | can use the same instance to service multiple ``QueueHandlers``. This is more |
| 303 | resource-friendly than, say, having threaded versions of the existing handler |
| 304 | classes, which would eat up one thread per handler for no particular benefit. |
| 305 | |
| 306 | An example of using these two classes follows (imports omitted):: |
| 307 | |
| 308 | que = queue.Queue(-1) # no limit on size |
| 309 | queue_handler = QueueHandler(que) |
| 310 | handler = logging.StreamHandler() |
| 311 | listener = QueueListener(que, handler) |
| 312 | root = logging.getLogger() |
| 313 | root.addHandler(queue_handler) |
| 314 | formatter = logging.Formatter('%(threadName)s: %(message)s') |
| 315 | handler.setFormatter(formatter) |
| 316 | listener.start() |
| 317 | # The log output will display the thread which generated |
| 318 | # the event (the main thread) rather than the internal |
| 319 | # thread which monitors the internal queue. This is what |
| 320 | # you want to happen. |
| 321 | root.warning('Look out!') |
| 322 | listener.stop() |
| 323 | |
| 324 | which, when run, will produce:: |
| 325 | |
| 326 | MainThread: Look out! |
| 327 | |
| 328 | |
| 329 | .. _network-logging: |
| 330 | |
| 331 | Sending and receiving logging events across a network |
| 332 | ----------------------------------------------------- |
| 333 | |
| 334 | Let's say you want to send logging events across a network, and handle them at |
| 335 | the receiving end. A simple way of doing this is attaching a |
| 336 | :class:`SocketHandler` instance to the root logger at the sending end:: |
| 337 | |
| 338 | import logging, logging.handlers |
| 339 | |
| 340 | rootLogger = logging.getLogger('') |
| 341 | rootLogger.setLevel(logging.DEBUG) |
| 342 | socketHandler = logging.handlers.SocketHandler('localhost', |
| 343 | logging.handlers.DEFAULT_TCP_LOGGING_PORT) |
| 344 | # don't bother with a formatter, since a socket handler sends the event as |
| 345 | # an unformatted pickle |
| 346 | rootLogger.addHandler(socketHandler) |
| 347 | |
| 348 | # Now, we can log to the root logger, or any other logger. First the root... |
| 349 | logging.info('Jackdaws love my big sphinx of quartz.') |
| 350 | |
| 351 | # Now, define a couple of other loggers which might represent areas in your |
| 352 | # application: |
| 353 | |
| 354 | logger1 = logging.getLogger('myapp.area1') |
| 355 | logger2 = logging.getLogger('myapp.area2') |
| 356 | |
| 357 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') |
| 358 | logger1.info('How quickly daft jumping zebras vex.') |
| 359 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') |
| 360 | logger2.error('The five boxing wizards jump quickly.') |
| 361 | |
| 362 | At the receiving end, you can set up a receiver using the :mod:`socketserver` |
| 363 | module. Here is a basic working example:: |
| 364 | |
| 365 | import pickle |
| 366 | import logging |
| 367 | import logging.handlers |
| 368 | import socketserver |
| 369 | import struct |
| 370 | |
| 371 | |
| 372 | class LogRecordStreamHandler(socketserver.StreamRequestHandler): |
| 373 | """Handler for a streaming logging request. |
| 374 | |
| 375 | This basically logs the record using whatever logging policy is |
| 376 | configured locally. |
| 377 | """ |
| 378 | |
| 379 | def handle(self): |
| 380 | """ |
| 381 | Handle multiple requests - each expected to be a 4-byte length, |
| 382 | followed by the LogRecord in pickle format. Logs the record |
| 383 | according to whatever policy is configured locally. |
| 384 | """ |
| 385 | while True: |
| 386 | chunk = self.connection.recv(4) |
| 387 | if len(chunk) < 4: |
| 388 | break |
| 389 | slen = struct.unpack('>L', chunk)[0] |
| 390 | chunk = self.connection.recv(slen) |
| 391 | while len(chunk) < slen: |
| 392 | chunk = chunk + self.connection.recv(slen - len(chunk)) |
| 393 | obj = self.unPickle(chunk) |
| 394 | record = logging.makeLogRecord(obj) |
| 395 | self.handleLogRecord(record) |
| 396 | |
| 397 | def unPickle(self, data): |
| 398 | return pickle.loads(data) |
| 399 | |
| 400 | def handleLogRecord(self, record): |
| 401 | # if a name is specified, we use the named logger rather than the one |
| 402 | # implied by the record. |
| 403 | if self.server.logname is not None: |
| 404 | name = self.server.logname |
| 405 | else: |
| 406 | name = record.name |
| 407 | logger = logging.getLogger(name) |
| 408 | # N.B. EVERY record gets logged. This is because Logger.handle |
| 409 | # is normally called AFTER logger-level filtering. If you want |
| 410 | # to do filtering, do it at the client end to save wasting |
| 411 | # cycles and network bandwidth! |
| 412 | logger.handle(record) |
| 413 | |
| 414 | class LogRecordSocketReceiver(socketserver.ThreadingTCPServer): |
| 415 | """ |
| 416 | Simple TCP socket-based logging receiver suitable for testing. |
| 417 | """ |
| 418 | |
| 419 | allow_reuse_address = 1 |
| 420 | |
| 421 | def __init__(self, host='localhost', |
| 422 | port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, |
| 423 | handler=LogRecordStreamHandler): |
| 424 | socketserver.ThreadingTCPServer.__init__(self, (host, port), handler) |
| 425 | self.abort = 0 |
| 426 | self.timeout = 1 |
| 427 | self.logname = None |
| 428 | |
| 429 | def serve_until_stopped(self): |
| 430 | import select |
| 431 | abort = 0 |
| 432 | while not abort: |
| 433 | rd, wr, ex = select.select([self.socket.fileno()], |
| 434 | [], [], |
| 435 | self.timeout) |
| 436 | if rd: |
| 437 | self.handle_request() |
| 438 | abort = self.abort |
| 439 | |
| 440 | def main(): |
| 441 | logging.basicConfig( |
| 442 | format='%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s') |
| 443 | tcpserver = LogRecordSocketReceiver() |
| 444 | print('About to start TCP server...') |
| 445 | tcpserver.serve_until_stopped() |
| 446 | |
| 447 | if __name__ == '__main__': |
| 448 | main() |
| 449 | |
| 450 | First run the server, and then the client. On the client side, nothing is |
| 451 | printed on the console; on the server side, you should see something like:: |
| 452 | |
| 453 | About to start TCP server... |
| 454 | 59 root INFO Jackdaws love my big sphinx of quartz. |
| 455 | 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. |
| 456 | 69 myapp.area1 INFO How quickly daft jumping zebras vex. |
| 457 | 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. |
| 458 | 69 myapp.area2 ERROR The five boxing wizards jump quickly. |
| 459 | |
| 460 | Note that there are some security issues with pickle in some scenarios. If |
| 461 | these affect you, you can use an alternative serialization scheme by overriding |
| 462 | the :meth:`makePickle` method and implementing your alternative there, as |
| 463 | well as adapting the above script to use your alternative serialization. |
| 464 | |
| 465 | |
| 466 | .. _context-info: |
| 467 | |
| 468 | Adding contextual information to your logging output |
| 469 | ---------------------------------------------------- |
| 470 | |
| 471 | Sometimes you want logging output to contain contextual information in |
| 472 | addition to the parameters passed to the logging call. For example, in a |
| 473 | networked application, it may be desirable to log client-specific information |
| 474 | in the log (e.g. remote client's username, or IP address). Although you could |
| 475 | use the *extra* parameter to achieve this, it's not always convenient to pass |
| 476 | the information in this way. While it might be tempting to create |
| 477 | :class:`Logger` instances on a per-connection basis, this is not a good idea |
| 478 | because these instances are not garbage collected. While this is not a problem |
| 479 | in practice, when the number of :class:`Logger` instances is dependent on the |
| 480 | level of granularity you want to use in logging an application, it could |
| 481 | be hard to manage if the number of :class:`Logger` instances becomes |
| 482 | effectively unbounded. |
| 483 | |
| 484 | |
| 485 | Using LoggerAdapters to impart contextual information |
| 486 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 487 | |
| 488 | An easy way in which you can pass contextual information to be output along |
| 489 | with logging event information is to use the :class:`LoggerAdapter` class. |
| 490 | This class is designed to look like a :class:`Logger`, so that you can call |
| 491 | :meth:`debug`, :meth:`info`, :meth:`warning`, :meth:`error`, |
| 492 | :meth:`exception`, :meth:`critical` and :meth:`log`. These methods have the |
| 493 | same signatures as their counterparts in :class:`Logger`, so you can use the |
| 494 | two types of instances interchangeably. |
| 495 | |
| 496 | When you create an instance of :class:`LoggerAdapter`, you pass it a |
| 497 | :class:`Logger` instance and a dict-like object which contains your contextual |
| 498 | information. When you call one of the logging methods on an instance of |
| 499 | :class:`LoggerAdapter`, it delegates the call to the underlying instance of |
| 500 | :class:`Logger` passed to its constructor, and arranges to pass the contextual |
| 501 | information in the delegated call. Here's a snippet from the code of |
| 502 | :class:`LoggerAdapter`:: |
| 503 | |
| 504 | def debug(self, msg, *args, **kwargs): |
| 505 | """ |
| 506 | Delegate a debug call to the underlying logger, after adding |
| 507 | contextual information from this adapter instance. |
| 508 | """ |
| 509 | msg, kwargs = self.process(msg, kwargs) |
| 510 | self.logger.debug(msg, *args, **kwargs) |
| 511 | |
| 512 | The :meth:`process` method of :class:`LoggerAdapter` is where the contextual |
| 513 | information is added to the logging output. It's passed the message and |
| 514 | keyword arguments of the logging call, and it passes back (potentially) |
| 515 | modified versions of these to use in the call to the underlying logger. The |
| 516 | default implementation of this method leaves the message alone, but inserts |
| 517 | an 'extra' key in the keyword argument whose value is the dict-like object |
| 518 | passed to the constructor. Of course, if you had passed an 'extra' keyword |
| 519 | argument in the call to the adapter, it will be silently overwritten. |
| 520 | |
| 521 | The advantage of using 'extra' is that the values in the dict-like object are |
| 522 | merged into the :class:`LogRecord` instance's __dict__, allowing you to use |
| 523 | customized strings with your :class:`Formatter` instances which know about |
| 524 | the keys of the dict-like object. If you need a different method, e.g. if you |
| 525 | want to prepend or append the contextual information to the message string, |
| 526 | you just need to subclass :class:`LoggerAdapter` and override :meth:`process` |
| 527 | to do what you need. Here's an example script which uses this class, which |
| 528 | also illustrates what dict-like behaviour is needed from an arbitrary |
| 529 | 'dict-like' object for use in the constructor:: |
| 530 | |
| 531 | import logging |
| 532 | |
| 533 | class ConnInfo: |
| 534 | """ |
| 535 | An example class which shows how an arbitrary class can be used as |
| 536 | the 'extra' context information repository passed to a LoggerAdapter. |
| 537 | """ |
| 538 | |
| 539 | def __getitem__(self, name): |
| 540 | """ |
| 541 | To allow this instance to look like a dict. |
| 542 | """ |
| 543 | from random import choice |
| 544 | if name == 'ip': |
| 545 | result = choice(['127.0.0.1', '192.168.0.1']) |
| 546 | elif name == 'user': |
| 547 | result = choice(['jim', 'fred', 'sheila']) |
| 548 | else: |
| 549 | result = self.__dict__.get(name, '?') |
| 550 | return result |
| 551 | |
| 552 | def __iter__(self): |
| 553 | """ |
| 554 | To allow iteration over keys, which will be merged into |
| 555 | the LogRecord dict before formatting and output. |
| 556 | """ |
| 557 | keys = ['ip', 'user'] |
| 558 | keys.extend(self.__dict__.keys()) |
| 559 | return keys.__iter__() |
| 560 | |
| 561 | if __name__ == '__main__': |
| 562 | from random import choice |
| 563 | levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL) |
| 564 | a1 = logging.LoggerAdapter(logging.getLogger('a.b.c'), |
| 565 | { 'ip' : '123.231.231.123', 'user' : 'sheila' }) |
| 566 | logging.basicConfig(level=logging.DEBUG, |
| 567 | format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s') |
| 568 | a1.debug('A debug message') |
| 569 | a1.info('An info message with %s', 'some parameters') |
| 570 | a2 = logging.LoggerAdapter(logging.getLogger('d.e.f'), ConnInfo()) |
| 571 | for x in range(10): |
| 572 | lvl = choice(levels) |
| 573 | lvlname = logging.getLevelName(lvl) |
| 574 | a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters') |
| 575 | |
| 576 | When this script is run, the output should look something like this:: |
| 577 | |
| 578 | 2008-01-18 14:49:54,023 a.b.c DEBUG IP: 123.231.231.123 User: sheila A debug message |
| 579 | 2008-01-18 14:49:54,023 a.b.c INFO IP: 123.231.231.123 User: sheila An info message with some parameters |
| 580 | 2008-01-18 14:49:54,023 d.e.f CRITICAL IP: 192.168.0.1 User: jim A message at CRITICAL level with 2 parameters |
| 581 | 2008-01-18 14:49:54,033 d.e.f INFO IP: 192.168.0.1 User: jim A message at INFO level with 2 parameters |
| 582 | 2008-01-18 14:49:54,033 d.e.f WARNING IP: 192.168.0.1 User: sheila A message at WARNING level with 2 parameters |
| 583 | 2008-01-18 14:49:54,033 d.e.f ERROR IP: 127.0.0.1 User: fred A message at ERROR level with 2 parameters |
| 584 | 2008-01-18 14:49:54,033 d.e.f ERROR IP: 127.0.0.1 User: sheila A message at ERROR level with 2 parameters |
| 585 | 2008-01-18 14:49:54,033 d.e.f WARNING IP: 192.168.0.1 User: sheila A message at WARNING level with 2 parameters |
| 586 | 2008-01-18 14:49:54,033 d.e.f WARNING IP: 192.168.0.1 User: jim A message at WARNING level with 2 parameters |
| 587 | 2008-01-18 14:49:54,033 d.e.f INFO IP: 192.168.0.1 User: fred A message at INFO level with 2 parameters |
| 588 | 2008-01-18 14:49:54,033 d.e.f WARNING IP: 192.168.0.1 User: sheila A message at WARNING level with 2 parameters |
| 589 | 2008-01-18 14:49:54,033 d.e.f WARNING IP: 127.0.0.1 User: jim A message at WARNING level with 2 parameters |
| 590 | |
| 591 | |
| 592 | .. _filters-contextual: |
| 593 | |
| 594 | Using Filters to impart contextual information |
| 595 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 596 | |
| 597 | You can also add contextual information to log output using a user-defined |
| 598 | :class:`Filter`. ``Filter`` instances are allowed to modify the ``LogRecords`` |
| 599 | passed to them, including adding additional attributes which can then be output |
| 600 | using a suitable format string, or if needed a custom :class:`Formatter`. |
| 601 | |
| 602 | For example in a web application, the request being processed (or at least, |
| 603 | the interesting parts of it) can be stored in a threadlocal |
| 604 | (:class:`threading.local`) variable, and then accessed from a ``Filter`` to |
| 605 | add, say, information from the request - say, the remote IP address and remote |
| 606 | user's username - to the ``LogRecord``, using the attribute names 'ip' and |
| 607 | 'user' as in the ``LoggerAdapter`` example above. In that case, the same format |
| 608 | string can be used to get similar output to that shown above. Here's an example |
| 609 | script:: |
| 610 | |
| 611 | import logging |
| 612 | from random import choice |
| 613 | |
| 614 | class ContextFilter(logging.Filter): |
| 615 | """ |
| 616 | This is a filter which injects contextual information into the log. |
| 617 | |
| 618 | Rather than use actual contextual information, we just use random |
| 619 | data in this demo. |
| 620 | """ |
| 621 | |
| 622 | USERS = ['jim', 'fred', 'sheila'] |
| 623 | IPS = ['123.231.231.123', '127.0.0.1', '192.168.0.1'] |
| 624 | |
| 625 | def filter(self, record): |
| 626 | |
| 627 | record.ip = choice(ContextFilter.IPS) |
| 628 | record.user = choice(ContextFilter.USERS) |
| 629 | return True |
| 630 | |
| 631 | if __name__ == '__main__': |
| 632 | levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 633 | logging.basicConfig(level=logging.DEBUG, |
| 634 | format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s') |
| 635 | a1 = logging.getLogger('a.b.c') |
| 636 | a2 = logging.getLogger('d.e.f') |
| 637 | |
| 638 | f = ContextFilter() |
| 639 | a1.addFilter(f) |
| 640 | a2.addFilter(f) |
| 641 | a1.debug('A debug message') |
| 642 | a1.info('An info message with %s', 'some parameters') |
| 643 | for x in range(10): |
| 644 | lvl = choice(levels) |
| 645 | lvlname = logging.getLevelName(lvl) |
| 646 | a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters') |
| 647 | |
| 648 | which, when run, produces something like:: |
| 649 | |
| 650 | 2010-09-06 22:38:15,292 a.b.c DEBUG IP: 123.231.231.123 User: fred A debug message |
| 651 | 2010-09-06 22:38:15,300 a.b.c INFO IP: 192.168.0.1 User: sheila An info message with some parameters |
| 652 | 2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL level with 2 parameters |
| 653 | 2010-09-06 22:38:15,300 d.e.f ERROR IP: 127.0.0.1 User: jim A message at ERROR level with 2 parameters |
| 654 | 2010-09-06 22:38:15,300 d.e.f DEBUG IP: 127.0.0.1 User: sheila A message at DEBUG level with 2 parameters |
| 655 | 2010-09-06 22:38:15,300 d.e.f ERROR IP: 123.231.231.123 User: fred A message at ERROR level with 2 parameters |
| 656 | 2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 192.168.0.1 User: jim A message at CRITICAL level with 2 parameters |
| 657 | 2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL level with 2 parameters |
| 658 | 2010-09-06 22:38:15,300 d.e.f DEBUG IP: 192.168.0.1 User: jim A message at DEBUG level with 2 parameters |
| 659 | 2010-09-06 22:38:15,301 d.e.f ERROR IP: 127.0.0.1 User: sheila A message at ERROR level with 2 parameters |
| 660 | 2010-09-06 22:38:15,301 d.e.f DEBUG IP: 123.231.231.123 User: fred A message at DEBUG level with 2 parameters |
| 661 | 2010-09-06 22:38:15,301 d.e.f INFO IP: 123.231.231.123 User: fred A message at INFO level with 2 parameters |
| 662 | |
| 663 | |
| 664 | .. _multiple-processes: |
| 665 | |
| 666 | Logging to a single file from multiple processes |
| 667 | ------------------------------------------------ |
| 668 | |
| 669 | Although logging is thread-safe, and logging to a single file from multiple |
| 670 | threads in a single process *is* supported, logging to a single file from |
| 671 | *multiple processes* is *not* supported, because there is no standard way to |
| 672 | serialize access to a single file across multiple processes in Python. If you |
| 673 | need to log to a single file from multiple processes, one way of doing this is |
| 674 | to have all the processes log to a :class:`SocketHandler`, and have a separate |
| 675 | process which implements a socket server which reads from the socket and logs |
| 676 | to file. (If you prefer, you can dedicate one thread in one of the existing |
Vinay Sajip | 6187664 | 2011-10-27 17:50:55 +0100 | [diff] [blame] | 677 | processes to perform this function.) :ref:`This section <network-logging>` |
| 678 | documents this approach in more detail and includes a working socket receiver |
| 679 | which can be used as a starting point for you to adapt in your own |
| 680 | applications. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 681 | |
| 682 | If you are using a recent version of Python which includes the |
| 683 | :mod:`multiprocessing` module, you could write your own handler which uses the |
| 684 | :class:`Lock` class from this module to serialize access to the file from |
| 685 | your processes. The existing :class:`FileHandler` and subclasses do not make |
| 686 | use of :mod:`multiprocessing` at present, though they may do so in the future. |
| 687 | Note that at present, the :mod:`multiprocessing` module does not provide |
| 688 | working lock functionality on all platforms (see |
| 689 | http://bugs.python.org/issue3770). |
| 690 | |
| 691 | .. currentmodule:: logging.handlers |
| 692 | |
| 693 | Alternatively, you can use a ``Queue`` and a :class:`QueueHandler` to send |
| 694 | all logging events to one of the processes in your multi-process application. |
| 695 | The following example script demonstrates how you can do this; in the example |
| 696 | a separate listener process listens for events sent by other processes and logs |
| 697 | them according to its own logging configuration. Although the example only |
| 698 | demonstrates one way of doing it (for example, you may want to use a listener |
Georg Brandl | 7a0afd3 | 2011-02-07 15:44:27 +0000 | [diff] [blame] | 699 | thread rather than a separate listener process -- the implementation would be |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 700 | analogous) it does allow for completely different logging configurations for |
| 701 | the listener and the other processes in your application, and can be used as |
| 702 | the basis for code meeting your own specific requirements:: |
| 703 | |
| 704 | # You'll need these imports in your own code |
| 705 | import logging |
| 706 | import logging.handlers |
| 707 | import multiprocessing |
| 708 | |
| 709 | # Next two import lines for this demo only |
| 710 | from random import choice, random |
| 711 | import time |
| 712 | |
| 713 | # |
| 714 | # Because you'll want to define the logging configurations for listener and workers, the |
| 715 | # listener and worker process functions take a configurer parameter which is a callable |
| 716 | # for configuring logging for that process. These functions are also passed the queue, |
| 717 | # which they use for communication. |
| 718 | # |
| 719 | # In practice, you can configure the listener however you want, but note that in this |
| 720 | # simple example, the listener does not apply level or filter logic to received records. |
Georg Brandl | 7a0afd3 | 2011-02-07 15:44:27 +0000 | [diff] [blame] | 721 | # In practice, you would probably want to do this logic in the worker processes, to avoid |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 722 | # sending events which would be filtered out between processes. |
| 723 | # |
| 724 | # The size of the rotated files is made small so you can see the results easily. |
| 725 | def listener_configurer(): |
| 726 | root = logging.getLogger() |
Raymond Hettinger | b34705f | 2011-06-26 15:29:06 +0200 | [diff] [blame] | 727 | h = logging.handlers.RotatingFileHandler('mptest.log', 'a', 300, 10) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 728 | f = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s') |
| 729 | h.setFormatter(f) |
| 730 | root.addHandler(h) |
| 731 | |
| 732 | # This is the listener process top-level loop: wait for logging events |
| 733 | # (LogRecords)on the queue and handle them, quit when you get a None for a |
| 734 | # LogRecord. |
| 735 | def listener_process(queue, configurer): |
| 736 | configurer() |
| 737 | while True: |
| 738 | try: |
| 739 | record = queue.get() |
| 740 | if record is None: # We send this as a sentinel to tell the listener to quit. |
| 741 | break |
| 742 | logger = logging.getLogger(record.name) |
| 743 | logger.handle(record) # No level or filter logic applied - just do it! |
| 744 | except (KeyboardInterrupt, SystemExit): |
| 745 | raise |
| 746 | except: |
| 747 | import sys, traceback |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 748 | print('Whoops! Problem:', file=sys.stderr) |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 749 | traceback.print_exc(file=sys.stderr) |
| 750 | |
| 751 | # Arrays used for random selections in this demo |
| 752 | |
| 753 | LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, |
| 754 | logging.ERROR, logging.CRITICAL] |
| 755 | |
| 756 | LOGGERS = ['a.b.c', 'd.e.f'] |
| 757 | |
| 758 | MESSAGES = [ |
| 759 | 'Random message #1', |
| 760 | 'Random message #2', |
| 761 | 'Random message #3', |
| 762 | ] |
| 763 | |
| 764 | # The worker configuration is done at the start of the worker process run. |
| 765 | # Note that on Windows you can't rely on fork semantics, so each process |
| 766 | # will run the logging configuration code when it starts. |
| 767 | def worker_configurer(queue): |
| 768 | h = logging.handlers.QueueHandler(queue) # Just the one handler needed |
| 769 | root = logging.getLogger() |
| 770 | root.addHandler(h) |
| 771 | root.setLevel(logging.DEBUG) # send all messages, for demo; no other level or filter logic applied. |
| 772 | |
| 773 | # This is the worker process top-level loop, which just logs ten events with |
| 774 | # random intervening delays before terminating. |
| 775 | # The print messages are just so you know it's doing something! |
| 776 | def worker_process(queue, configurer): |
| 777 | configurer(queue) |
| 778 | name = multiprocessing.current_process().name |
| 779 | print('Worker started: %s' % name) |
| 780 | for i in range(10): |
| 781 | time.sleep(random()) |
| 782 | logger = logging.getLogger(choice(LOGGERS)) |
| 783 | level = choice(LEVELS) |
| 784 | message = choice(MESSAGES) |
| 785 | logger.log(level, message) |
| 786 | print('Worker finished: %s' % name) |
| 787 | |
| 788 | # Here's where the demo gets orchestrated. Create the queue, create and start |
| 789 | # the listener, create ten workers and start them, wait for them to finish, |
| 790 | # then send a None to the queue to tell the listener to finish. |
| 791 | def main(): |
| 792 | queue = multiprocessing.Queue(-1) |
| 793 | listener = multiprocessing.Process(target=listener_process, |
| 794 | args=(queue, listener_configurer)) |
| 795 | listener.start() |
| 796 | workers = [] |
| 797 | for i in range(10): |
| 798 | worker = multiprocessing.Process(target=worker_process, |
| 799 | args=(queue, worker_configurer)) |
| 800 | workers.append(worker) |
| 801 | worker.start() |
| 802 | for w in workers: |
| 803 | w.join() |
| 804 | queue.put_nowait(None) |
| 805 | listener.join() |
| 806 | |
| 807 | if __name__ == '__main__': |
| 808 | main() |
| 809 | |
Vinay Sajip | e6f1e43 | 2010-12-26 18:47:51 +0000 | [diff] [blame] | 810 | A variant of the above script keeps the logging in the main process, in a |
| 811 | separate thread:: |
| 812 | |
| 813 | import logging |
| 814 | import logging.config |
| 815 | import logging.handlers |
| 816 | from multiprocessing import Process, Queue |
| 817 | import random |
| 818 | import threading |
| 819 | import time |
| 820 | |
| 821 | def logger_thread(q): |
| 822 | while True: |
| 823 | record = q.get() |
| 824 | if record is None: |
| 825 | break |
| 826 | logger = logging.getLogger(record.name) |
| 827 | logger.handle(record) |
| 828 | |
| 829 | |
| 830 | def worker_process(q): |
| 831 | qh = logging.handlers.QueueHandler(q) |
| 832 | root = logging.getLogger() |
| 833 | root.setLevel(logging.DEBUG) |
| 834 | root.addHandler(qh) |
| 835 | levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, |
| 836 | logging.CRITICAL] |
| 837 | loggers = ['foo', 'foo.bar', 'foo.bar.baz', |
| 838 | 'spam', 'spam.ham', 'spam.ham.eggs'] |
| 839 | for i in range(100): |
| 840 | lvl = random.choice(levels) |
| 841 | logger = logging.getLogger(random.choice(loggers)) |
| 842 | logger.log(lvl, 'Message no. %d', i) |
| 843 | |
| 844 | if __name__ == '__main__': |
| 845 | q = Queue() |
| 846 | d = { |
| 847 | 'version': 1, |
| 848 | 'formatters': { |
| 849 | 'detailed': { |
| 850 | 'class': 'logging.Formatter', |
| 851 | 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s' |
| 852 | } |
| 853 | }, |
| 854 | 'handlers': { |
| 855 | 'console': { |
| 856 | 'class': 'logging.StreamHandler', |
| 857 | 'level': 'INFO', |
| 858 | }, |
| 859 | 'file': { |
| 860 | 'class': 'logging.FileHandler', |
| 861 | 'filename': 'mplog.log', |
| 862 | 'mode': 'w', |
| 863 | 'formatter': 'detailed', |
| 864 | }, |
| 865 | 'foofile': { |
| 866 | 'class': 'logging.FileHandler', |
| 867 | 'filename': 'mplog-foo.log', |
| 868 | 'mode': 'w', |
| 869 | 'formatter': 'detailed', |
| 870 | }, |
| 871 | 'errors': { |
| 872 | 'class': 'logging.FileHandler', |
| 873 | 'filename': 'mplog-errors.log', |
| 874 | 'mode': 'w', |
| 875 | 'level': 'ERROR', |
| 876 | 'formatter': 'detailed', |
| 877 | }, |
| 878 | }, |
| 879 | 'loggers': { |
| 880 | 'foo': { |
| 881 | 'handlers' : ['foofile'] |
| 882 | } |
| 883 | }, |
| 884 | 'root': { |
| 885 | 'level': 'DEBUG', |
| 886 | 'handlers': ['console', 'file', 'errors'] |
| 887 | }, |
| 888 | } |
| 889 | workers = [] |
| 890 | for i in range(5): |
| 891 | wp = Process(target=worker_process, name='worker %d' % (i + 1), args=(q,)) |
| 892 | workers.append(wp) |
| 893 | wp.start() |
| 894 | logging.config.dictConfig(d) |
| 895 | lp = threading.Thread(target=logger_thread, args=(q,)) |
| 896 | lp.start() |
| 897 | # At this point, the main process could do some useful work of its own |
| 898 | # Once it's done that, it can wait for the workers to terminate... |
| 899 | for wp in workers: |
| 900 | wp.join() |
| 901 | # And now tell the logging thread to finish up, too |
| 902 | q.put(None) |
| 903 | lp.join() |
| 904 | |
| 905 | This variant shows how you can e.g. apply configuration for particular loggers |
| 906 | - e.g. the ``foo`` logger has a special handler which stores all events in the |
| 907 | ``foo`` subsystem in a file ``mplog-foo.log``. This will be used by the logging |
| 908 | machinery in the main process (even though the logging events are generated in |
| 909 | the worker processes) to direct the messages to the appropriate destinations. |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 910 | |
| 911 | Using file rotation |
| 912 | ------------------- |
| 913 | |
| 914 | .. sectionauthor:: Doug Hellmann, Vinay Sajip (changes) |
| 915 | .. (see <http://blog.doughellmann.com/2007/05/pymotw-logging.html>) |
| 916 | |
| 917 | Sometimes you want to let a log file grow to a certain size, then open a new |
| 918 | file and log to that. You may want to keep a certain number of these files, and |
| 919 | when that many files have been created, rotate the files so that the number of |
Georg Brandl | 7a0afd3 | 2011-02-07 15:44:27 +0000 | [diff] [blame] | 920 | files and the size of the files both remain bounded. For this usage pattern, the |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 921 | logging package provides a :class:`RotatingFileHandler`:: |
| 922 | |
| 923 | import glob |
| 924 | import logging |
| 925 | import logging.handlers |
| 926 | |
| 927 | LOG_FILENAME = 'logging_rotatingfile_example.out' |
| 928 | |
| 929 | # Set up a specific logger with our desired output level |
| 930 | my_logger = logging.getLogger('MyLogger') |
| 931 | my_logger.setLevel(logging.DEBUG) |
| 932 | |
| 933 | # Add the log message handler to the logger |
| 934 | handler = logging.handlers.RotatingFileHandler( |
| 935 | LOG_FILENAME, maxBytes=20, backupCount=5) |
| 936 | |
| 937 | my_logger.addHandler(handler) |
| 938 | |
| 939 | # Log some messages |
| 940 | for i in range(20): |
| 941 | my_logger.debug('i = %d' % i) |
| 942 | |
| 943 | # See what files are created |
| 944 | logfiles = glob.glob('%s*' % LOG_FILENAME) |
| 945 | |
| 946 | for filename in logfiles: |
| 947 | print(filename) |
| 948 | |
| 949 | The result should be 6 separate files, each with part of the log history for the |
| 950 | application:: |
| 951 | |
| 952 | logging_rotatingfile_example.out |
| 953 | logging_rotatingfile_example.out.1 |
| 954 | logging_rotatingfile_example.out.2 |
| 955 | logging_rotatingfile_example.out.3 |
| 956 | logging_rotatingfile_example.out.4 |
| 957 | logging_rotatingfile_example.out.5 |
| 958 | |
| 959 | The most current file is always :file:`logging_rotatingfile_example.out`, |
| 960 | and each time it reaches the size limit it is renamed with the suffix |
| 961 | ``.1``. Each of the existing backup files is renamed to increment the suffix |
| 962 | (``.1`` becomes ``.2``, etc.) and the ``.6`` file is erased. |
| 963 | |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 964 | Obviously this example sets the log length much too small as an extreme |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 965 | example. You would want to set *maxBytes* to an appropriate value. |
| 966 | |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 967 | .. _format-styles: |
| 968 | |
| 969 | Use of alternative formatting styles |
| 970 | ------------------------------------ |
| 971 | |
| 972 | When logging was added to the Python standard library, the only way of |
| 973 | formatting messages with variable content was to use the %-formatting |
| 974 | method. Since then, Python has gained two new formatting approaches: |
Vinay Sajip | 39b83ac | 2012-02-28 08:05:23 +0000 | [diff] [blame] | 975 | :class:`string.Template` (added in Python 2.4) and :meth:`str.format` |
| 976 | (added in Python 2.6). |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 977 | |
Vinay Sajip | 39b83ac | 2012-02-28 08:05:23 +0000 | [diff] [blame] | 978 | Logging (as of 3.2) provides improved support for these two additional |
| 979 | formatting styles. The :class:`Formatter` class been enhanced to take an |
| 980 | additional, optional keyword parameter named ``style``. This defaults to |
| 981 | ``'%'``, but other possible values are ``'{'`` and ``'$'``, which correspond |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 982 | to the other two formatting styles. Backwards compatibility is maintained by |
| 983 | default (as you would expect), but by explicitly specifying a style parameter, |
| 984 | you get the ability to specify format strings which work with |
| 985 | :meth:`str.format` or :class:`string.Template`. Here's an example console |
| 986 | session to show the possibilities: |
| 987 | |
| 988 | .. code-block:: pycon |
| 989 | |
| 990 | >>> import logging |
| 991 | >>> root = logging.getLogger() |
| 992 | >>> root.setLevel(logging.DEBUG) |
| 993 | >>> handler = logging.StreamHandler() |
| 994 | >>> bf = logging.Formatter('{asctime} {name} {levelname:8s} {message}', |
| 995 | ... style='{') |
| 996 | >>> handler.setFormatter(bf) |
| 997 | >>> root.addHandler(handler) |
| 998 | >>> logger = logging.getLogger('foo.bar') |
| 999 | >>> logger.debug('This is a DEBUG message') |
| 1000 | 2010-10-28 15:11:55,341 foo.bar DEBUG This is a DEBUG message |
| 1001 | >>> logger.critical('This is a CRITICAL message') |
| 1002 | 2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message |
| 1003 | >>> df = logging.Formatter('$asctime $name ${levelname} $message', |
| 1004 | ... style='$') |
| 1005 | >>> handler.setFormatter(df) |
| 1006 | >>> logger.debug('This is a DEBUG message') |
| 1007 | 2010-10-28 15:13:06,924 foo.bar DEBUG This is a DEBUG message |
| 1008 | >>> logger.critical('This is a CRITICAL message') |
| 1009 | 2010-10-28 15:13:11,494 foo.bar CRITICAL This is a CRITICAL message |
| 1010 | >>> |
| 1011 | |
| 1012 | Note that the formatting of logging messages for final output to logs is |
| 1013 | completely independent of how an individual logging message is constructed. |
| 1014 | That can still use %-formatting, as shown here:: |
| 1015 | |
| 1016 | >>> logger.error('This is an%s %s %s', 'other,', 'ERROR,', 'message') |
| 1017 | 2010-10-28 15:19:29,833 foo.bar ERROR This is another, ERROR, message |
| 1018 | >>> |
| 1019 | |
| 1020 | Logging calls (``logger.debug()``, ``logger.info()`` etc.) only take |
| 1021 | positional parameters for the actual logging message itself, with keyword |
| 1022 | parameters used only for determining options for how to handle the actual |
| 1023 | logging call (e.g. the ``exc_info`` keyword parameter to indicate that |
| 1024 | traceback information should be logged, or the ``extra`` keyword parameter |
| 1025 | to indicate additional contextual information to be added to the log). So |
| 1026 | you cannot directly make logging calls using :meth:`str.format` or |
| 1027 | :class:`string.Template` syntax, because internally the logging package |
| 1028 | uses %-formatting to merge the format string and the variable arguments. |
| 1029 | There would no changing this while preserving backward compatibility, since |
| 1030 | all logging calls which are out there in existing code will be using %-format |
| 1031 | strings. |
| 1032 | |
| 1033 | There is, however, a way that you can use {}- and $- formatting to construct |
| 1034 | your individual log messages. Recall that for a message you can use an |
| 1035 | arbitrary object as a message format string, and that the logging package will |
| 1036 | call ``str()`` on that object to get the actual format string. Consider the |
| 1037 | following two classes:: |
| 1038 | |
| 1039 | class BraceMessage(object): |
| 1040 | def __init__(self, fmt, *args, **kwargs): |
| 1041 | self.fmt = fmt |
| 1042 | self.args = args |
| 1043 | self.kwargs = kwargs |
| 1044 | |
| 1045 | def __str__(self): |
| 1046 | return self.fmt.format(*self.args, **self.kwargs) |
| 1047 | |
| 1048 | class DollarMessage(object): |
| 1049 | def __init__(self, fmt, **kwargs): |
| 1050 | self.fmt = fmt |
| 1051 | self.kwargs = kwargs |
| 1052 | |
| 1053 | def __str__(self): |
| 1054 | from string import Template |
| 1055 | return Template(self.fmt).substitute(**self.kwargs) |
| 1056 | |
| 1057 | Either of these can be used in place of a format string, to allow {}- or |
| 1058 | $-formatting to be used to build the actual "message" part which appears in the |
| 1059 | formatted log output in place of "%(message)s" or "{message}" or "$message". |
| 1060 | It's a little unwieldy to use the class names whenever you want to log |
| 1061 | something, but it's quite palatable if you use an alias such as __ (double |
| 1062 | underscore – not to be confused with _, the single underscore used as a |
| 1063 | synonym/alias for :func:`gettext.gettext` or its brethren). |
| 1064 | |
| 1065 | The above classes are not included in Python, though they're easy enough to |
| 1066 | copy and paste into your own code. They can be used as follows (assuming that |
| 1067 | they're declared in a module called ``wherever``): |
| 1068 | |
| 1069 | .. code-block:: pycon |
| 1070 | |
| 1071 | >>> from wherever import BraceMessage as __ |
Vinay Sajip | 39b83ac | 2012-02-28 08:05:23 +0000 | [diff] [blame] | 1072 | >>> print(__('Message with {0} {name}', 2, name='placeholders')) |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 1073 | Message with 2 placeholders |
| 1074 | >>> class Point: pass |
| 1075 | ... |
| 1076 | >>> p = Point() |
| 1077 | >>> p.x = 0.5 |
| 1078 | >>> p.y = 0.5 |
| 1079 | >>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})', |
| 1080 | ... point=p)) |
| 1081 | Message with coordinates: (0.50, 0.50) |
| 1082 | >>> from wherever import DollarMessage as __ |
| 1083 | >>> print(__('Message with $num $what', num=2, what='placeholders')) |
| 1084 | Message with 2 placeholders |
| 1085 | >>> |
| 1086 | |
Vinay Sajip | 39b83ac | 2012-02-28 08:05:23 +0000 | [diff] [blame] | 1087 | While the above examples use ``print()`` to show how the formatting works, you |
| 1088 | would of course use ``logger.debug()`` or similar to actually log using this |
| 1089 | approach. |
| 1090 | |
Vinay Sajip | 6b883a2 | 2012-02-27 11:02:45 +0000 | [diff] [blame] | 1091 | One thing to note is that you pay no significant performance penalty with this |
| 1092 | approach: the actual formatting happens not when you make the logging call, but |
| 1093 | when (and if) the logged message is actually about to be output to a log by a |
| 1094 | handler. So the only slightly unusual thing which might trip you up is that the |
| 1095 | parentheses go around the format string and the arguments, not just the format |
| 1096 | string. That's because the __ notation is just syntax sugar for a constructor |
| 1097 | call to one of the XXXMessage classes. |
| 1098 | |
| 1099 | |
Vinay Sajip | 982f534 | 2012-02-27 11:56:29 +0000 | [diff] [blame] | 1100 | .. currentmodule:: logging |
| 1101 | |
Georg Brandl | e998386 | 2012-02-28 08:21:40 +0100 | [diff] [blame] | 1102 | .. _custom-logrecord: |
Vinay Sajip | 982f534 | 2012-02-27 11:56:29 +0000 | [diff] [blame] | 1103 | |
| 1104 | Customising ``LogRecord`` |
| 1105 | ------------------------- |
| 1106 | |
| 1107 | Every logging event is represented by a :class:`LogRecord` instance. |
| 1108 | When an event is logged and not filtered out by a logger's level, a |
| 1109 | :class:`LogRecord` is created, populated with information about the event and |
| 1110 | then passed to the handlers for that logger (and its ancestors, up to and |
| 1111 | including the logger where further propagation up the hierarchy is disabled). |
| 1112 | Before Python 3.2, there were only two places where this creation was done: |
| 1113 | |
| 1114 | * :meth:`Logger.makeRecord`, which is called in the normal process of |
| 1115 | logging an event. This invoked :class:`LogRecord` directly to create an |
| 1116 | instance. |
| 1117 | * :func:`makeLogRecord`, which is called with a dictionary containing |
| 1118 | attributes to be added to the LogRecord. This is typically invoked when a |
| 1119 | suitable dictionary has been received over the network (e.g. in pickle form |
| 1120 | via a :class:`~handlers.SocketHandler`, or in JSON form via an |
| 1121 | :class:`~handlers.HTTPHandler`). |
| 1122 | |
| 1123 | This has usually meant that if you need to do anything special with a |
| 1124 | :class:`LogRecord`, you've had to do one of the following. |
| 1125 | |
| 1126 | * Create your own :class:`Logger` subclass, which overrides |
| 1127 | :meth:`Logger.makeRecord`, and set it using :func:`~logging.setLoggerClass` |
| 1128 | before any loggers that you care about are instantiated. |
| 1129 | * Add a :class:`Filter` to a logger or handler, which does the |
| 1130 | necessary special manipulation you need when its |
| 1131 | :meth:`~Filter.filter` method is called. |
| 1132 | |
| 1133 | The first approach would be a little unwieldy in the scenario where (say) |
| 1134 | several different libraries wanted to do different things. Each would attempt |
| 1135 | to set its own :class:`Logger` subclass, and the one which did this last would |
| 1136 | win. |
| 1137 | |
| 1138 | The second approach works reasonably well for many cases, but does not allow |
| 1139 | you to e.g. use a specialized subclass of :class:`LogRecord`. Library |
| 1140 | developers can set a suitable filter on their loggers, but they would have to |
| 1141 | remember to do this every time they introduced a new logger (which they would |
Georg Brandl | e998386 | 2012-02-28 08:21:40 +0100 | [diff] [blame] | 1142 | do simply by adding new packages or modules and doing :: |
Vinay Sajip | 982f534 | 2012-02-27 11:56:29 +0000 | [diff] [blame] | 1143 | |
| 1144 | logger = logging.getLogger(__name__) |
| 1145 | |
| 1146 | at module level). It's probably one too many things to think about. Developers |
| 1147 | could also add the filter to a :class:`~logging.NullHandler` attached to their |
| 1148 | top-level logger, but this would not be invoked if an application developer |
| 1149 | attached a handler to a lower-level library logger – so output from that |
| 1150 | handler would not reflect the intentions of the library developer. |
| 1151 | |
| 1152 | In Python 3.2 and later, :class:`~logging.LogRecord` creation is done through a |
| 1153 | factory, which you can specify. The factory is just a callable you can set with |
| 1154 | :func:`~logging.setLogRecordFactory`, and interrogate with |
| 1155 | :func:`~logging.getLogRecordFactory`. The factory is invoked with the same |
| 1156 | signature as the :class:`~logging.LogRecord` constructor, as :class:`LogRecord` |
| 1157 | is the default setting for the factory. |
| 1158 | |
| 1159 | This approach allows a custom factory to control all aspects of LogRecord |
| 1160 | creation. For example, you could return a subclass, or just add some additional |
| 1161 | attributes to the record once created, using a pattern similar to this:: |
| 1162 | |
| 1163 | old_factory = logging.getLogRecordFactory() |
| 1164 | |
| 1165 | def record_factory(*args, **kwargs): |
| 1166 | record = old_factory(*args, **kwargs) |
| 1167 | record.custom_attribute = 0xdecafbad |
| 1168 | return record |
| 1169 | |
| 1170 | logging.setLogRecordFactory(record_factory) |
| 1171 | |
| 1172 | This pattern allows different libraries to chain factories together, and as |
| 1173 | long as they don't overwrite each other's attributes or unintentionally |
| 1174 | overwrite the attributes provided as standard, there should be no surprises. |
| 1175 | However, it should be borne in mind that each link in the chain adds run-time |
| 1176 | overhead to all logging operations, and the technique should only be used when |
| 1177 | the use of a :class:`Filter` does not provide the desired result. |
| 1178 | |
| 1179 | |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1180 | .. _zeromq-handlers: |
| 1181 | |
Vinay Sajip | 7d10129 | 2010-12-26 21:22:33 +0000 | [diff] [blame] | 1182 | Subclassing QueueHandler - a ZeroMQ example |
| 1183 | ------------------------------------------- |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1184 | |
| 1185 | You can use a :class:`QueueHandler` subclass to send messages to other kinds |
| 1186 | of queues, for example a ZeroMQ 'publish' socket. In the example below,the |
| 1187 | socket is created separately and passed to the handler (as its 'queue'):: |
| 1188 | |
| 1189 | import zmq # using pyzmq, the Python binding for ZeroMQ |
| 1190 | import json # for serializing records portably |
| 1191 | |
| 1192 | ctx = zmq.Context() |
| 1193 | sock = zmq.Socket(ctx, zmq.PUB) # or zmq.PUSH, or other suitable value |
| 1194 | sock.bind('tcp://*:5556') # or wherever |
| 1195 | |
| 1196 | class ZeroMQSocketHandler(QueueHandler): |
| 1197 | def enqueue(self, record): |
| 1198 | data = json.dumps(record.__dict__) |
| 1199 | self.queue.send(data) |
| 1200 | |
| 1201 | handler = ZeroMQSocketHandler(sock) |
| 1202 | |
| 1203 | |
| 1204 | Of course there are other ways of organizing this, for example passing in the |
| 1205 | data needed by the handler to create the socket:: |
| 1206 | |
| 1207 | class ZeroMQSocketHandler(QueueHandler): |
| 1208 | def __init__(self, uri, socktype=zmq.PUB, ctx=None): |
| 1209 | self.ctx = ctx or zmq.Context() |
| 1210 | socket = zmq.Socket(self.ctx, socktype) |
| 1211 | socket.bind(uri) |
| 1212 | QueueHandler.__init__(self, socket) |
| 1213 | |
| 1214 | def enqueue(self, record): |
| 1215 | data = json.dumps(record.__dict__) |
| 1216 | self.queue.send(data) |
| 1217 | |
| 1218 | def close(self): |
| 1219 | self.queue.close() |
| 1220 | |
| 1221 | |
Vinay Sajip | 7d10129 | 2010-12-26 21:22:33 +0000 | [diff] [blame] | 1222 | Subclassing QueueListener - a ZeroMQ example |
| 1223 | -------------------------------------------- |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1224 | |
| 1225 | You can also subclass :class:`QueueListener` to get messages from other kinds |
| 1226 | of queues, for example a ZeroMQ 'subscribe' socket. Here's an example:: |
| 1227 | |
| 1228 | class ZeroMQSocketListener(QueueListener): |
| 1229 | def __init__(self, uri, *handlers, **kwargs): |
| 1230 | self.ctx = kwargs.get('ctx') or zmq.Context() |
| 1231 | socket = zmq.Socket(self.ctx, zmq.SUB) |
| 1232 | socket.setsockopt(zmq.SUBSCRIBE, '') # subscribe to everything |
| 1233 | socket.connect(uri) |
| 1234 | |
| 1235 | def dequeue(self): |
| 1236 | msg = self.queue.recv() |
| 1237 | return logging.makeLogRecord(json.loads(msg)) |
| 1238 | |
| 1239 | |
Vinay Sajip | 7d10129 | 2010-12-26 21:22:33 +0000 | [diff] [blame] | 1240 | .. seealso:: |
Vinay Sajip | c63619b | 2010-12-19 12:56:57 +0000 | [diff] [blame] | 1241 | |
Vinay Sajip | 7d10129 | 2010-12-26 21:22:33 +0000 | [diff] [blame] | 1242 | Module :mod:`logging` |
| 1243 | API reference for the logging module. |
| 1244 | |
| 1245 | Module :mod:`logging.config` |
| 1246 | Configuration API for the logging module. |
| 1247 | |
| 1248 | Module :mod:`logging.handlers` |
| 1249 | Useful handlers included with the logging module. |
| 1250 | |
| 1251 | :ref:`A basic logging tutorial <logging-basic-tutorial>` |
| 1252 | |
| 1253 | :ref:`A more advanced logging tutorial <logging-advanced-tutorial>` |
Vinay Sajip | 631a7e2 | 2011-11-23 14:27:54 +0000 | [diff] [blame] | 1254 | |
| 1255 | |
| 1256 | An example dictionary-based configuration |
| 1257 | ----------------------------------------- |
| 1258 | |
| 1259 | Below is an example of a logging configuration dictionary - it's taken from |
| 1260 | the `documentation on the Django project <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_. |
| 1261 | This dictionary is passed to :func:`~logging.config.dictConfig` to put the configuration into effect:: |
| 1262 | |
| 1263 | LOGGING = { |
| 1264 | 'version': 1, |
| 1265 | 'disable_existing_loggers': True, |
| 1266 | 'formatters': { |
| 1267 | 'verbose': { |
| 1268 | 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' |
| 1269 | }, |
| 1270 | 'simple': { |
| 1271 | 'format': '%(levelname)s %(message)s' |
| 1272 | }, |
| 1273 | }, |
| 1274 | 'filters': { |
| 1275 | 'special': { |
| 1276 | '()': 'project.logging.SpecialFilter', |
| 1277 | 'foo': 'bar', |
| 1278 | } |
| 1279 | }, |
| 1280 | 'handlers': { |
| 1281 | 'null': { |
| 1282 | 'level':'DEBUG', |
| 1283 | 'class':'django.utils.log.NullHandler', |
| 1284 | }, |
| 1285 | 'console':{ |
| 1286 | 'level':'DEBUG', |
| 1287 | 'class':'logging.StreamHandler', |
| 1288 | 'formatter': 'simple' |
| 1289 | }, |
| 1290 | 'mail_admins': { |
| 1291 | 'level': 'ERROR', |
| 1292 | 'class': 'django.utils.log.AdminEmailHandler', |
| 1293 | 'filters': ['special'] |
| 1294 | } |
| 1295 | }, |
| 1296 | 'loggers': { |
| 1297 | 'django': { |
| 1298 | 'handlers':['null'], |
| 1299 | 'propagate': True, |
| 1300 | 'level':'INFO', |
| 1301 | }, |
| 1302 | 'django.request': { |
| 1303 | 'handlers': ['mail_admins'], |
| 1304 | 'level': 'ERROR', |
| 1305 | 'propagate': False, |
| 1306 | }, |
| 1307 | 'myproject.custom': { |
| 1308 | 'handlers': ['console', 'mail_admins'], |
| 1309 | 'level': 'INFO', |
| 1310 | 'filters': ['special'] |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | For more information about this configuration, you can see the `relevant |
| 1316 | section <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_ |
| 1317 | of the Django documentation. |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 1318 | |
| 1319 | .. _cookbook-rotator-namer: |
| 1320 | |
| 1321 | Using a rotator and namer to customise log rotation processing |
| 1322 | -------------------------------------------------------------- |
| 1323 | |
| 1324 | An example of how you can define a namer and rotator is given in the following |
| 1325 | snippet, which shows zlib-based compression of the log file:: |
| 1326 | |
| 1327 | def namer(name): |
| 1328 | return name + ".gz" |
| 1329 | |
| 1330 | def rotator(source, dest): |
| 1331 | with open(source, "rb") as sf: |
| 1332 | data = sf.read() |
| 1333 | compressed = zlib.compress(data, 9) |
| 1334 | with open(dest, "wb") as df: |
| 1335 | df.write(compressed) |
| 1336 | os.remove(source) |
| 1337 | |
| 1338 | rh = logging.handlers.RotatingFileHandler(...) |
| 1339 | rh.rotator = rotator |
| 1340 | rh.namer = namer |
| 1341 | |
Ezio Melotti | 226231c | 2012-01-18 05:40:00 +0200 | [diff] [blame] | 1342 | These are not "true" .gz files, as they are bare compressed data, with no |
| 1343 | "container" such as you’d find in an actual gzip file. This snippet is just |
Vinay Sajip | 23b94d0 | 2012-01-04 12:02:26 +0000 | [diff] [blame] | 1344 | for illustration purposes. |
| 1345 | |
Vinay Sajip | 0292fa9 | 2012-04-08 01:49:12 +0100 | [diff] [blame] | 1346 | A more elaborate multiprocessing example |
| 1347 | ---------------------------------------- |
| 1348 | |
| 1349 | The following working example shows how logging can be used with multiprocessing |
| 1350 | using configuration files. The configurations are fairly simple, but serve to |
| 1351 | illustrate how more complex ones could be implemented in a real multiprocessing |
| 1352 | scenario. |
| 1353 | |
| 1354 | In the example, the main process spawns a listener process and some worker |
| 1355 | processes. Each of the main process, the listener and the workers have three |
| 1356 | separate configurations (the workers all share the same configuration). We can |
| 1357 | see logging in the main process, how the workers log to a QueueHandler and how |
| 1358 | the listener implements a QueueListener and a more complex logging |
| 1359 | configuration, and arranges to dispatch events received via the queue to the |
| 1360 | handlers specified in the configuration. Note that these configurations are |
| 1361 | purely illustrative, but you should be able to adapt this example to your own |
| 1362 | scenario. |
| 1363 | |
| 1364 | Here's the script - the docstrings and the comments hopefully explain how it |
| 1365 | works:: |
| 1366 | |
| 1367 | import logging |
| 1368 | import logging.config |
| 1369 | import logging.handlers |
| 1370 | from multiprocessing import Process, Queue, Event, current_process |
| 1371 | import os |
| 1372 | import random |
| 1373 | import time |
| 1374 | |
| 1375 | class MyHandler(object): |
| 1376 | """ |
| 1377 | A simple handler for logging events. It runs in the listener process and |
| 1378 | dispatches events to loggers based on the name in the received record, |
| 1379 | which then get dispatched, by the logging system, to the handlers |
Vinay Sajip | 838e638 | 2012-04-09 19:46:24 +0100 | [diff] [blame] | 1380 | configured for those loggers. |
Vinay Sajip | 0292fa9 | 2012-04-08 01:49:12 +0100 | [diff] [blame] | 1381 | """ |
| 1382 | def handle(self, record): |
| 1383 | logger = logging.getLogger(record.name) |
| 1384 | # The process name is transformed just to show that it's the listener |
| 1385 | # doing the logging to files and console |
| 1386 | record.processName = '%s (for %s)' % (current_process().name, record.processName) |
| 1387 | logger.handle(record) |
| 1388 | |
| 1389 | def listener_process(q, stop_event, config): |
| 1390 | """ |
| 1391 | This could be done in the main process, but is just done in a separate |
| 1392 | process for illustrative purposes. |
| 1393 | |
| 1394 | This initialises logging according to the specified configuration, |
| 1395 | starts the listener and waits for the main process to signal completion |
| 1396 | via the event. The listener is then stopped, and the process exits. |
| 1397 | """ |
| 1398 | logging.config.dictConfig(config) |
| 1399 | listener = logging.handlers.QueueListener(q, MyHandler()) |
| 1400 | listener.start() |
| 1401 | if os.name == 'posix': |
| 1402 | # On POSIX, the setup logger will have been configured in the |
| 1403 | # parent process, but should have been disabled following the |
| 1404 | # dictConfig call. |
| 1405 | # On Windows, since fork isn't used, the setup logger won't |
| 1406 | # exist in the child, so it would be created and the message |
| 1407 | # would appear - hence the "if posix" clause. |
| 1408 | logger = logging.getLogger('setup') |
| 1409 | logger.critical('Should not appear, because of disabled logger ...') |
| 1410 | stop_event.wait() |
| 1411 | listener.stop() |
| 1412 | |
| 1413 | def worker_process(config): |
| 1414 | """ |
| 1415 | A number of these are spawned for the purpose of illustration. In |
| 1416 | practice, they could be a heterogenous bunch of processes rather than |
| 1417 | ones which are identical to each other. |
| 1418 | |
| 1419 | This initialises logging according to the specified configuration, |
| 1420 | and logs a hundred messages with random levels to randomly selected |
| 1421 | loggers. |
| 1422 | |
| 1423 | A small sleep is added to allow other processes a chance to run. This |
| 1424 | is not strictly needed, but it mixes the output from the different |
| 1425 | processes a bit more than if it's left out. |
| 1426 | """ |
| 1427 | logging.config.dictConfig(config) |
| 1428 | levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, |
| 1429 | logging.CRITICAL] |
| 1430 | loggers = ['foo', 'foo.bar', 'foo.bar.baz', |
| 1431 | 'spam', 'spam.ham', 'spam.ham.eggs'] |
| 1432 | if os.name == 'posix': |
| 1433 | # On POSIX, the setup logger will have been configured in the |
| 1434 | # parent process, but should have been disabled following the |
| 1435 | # dictConfig call. |
| 1436 | # On Windows, since fork isn't used, the setup logger won't |
| 1437 | # exist in the child, so it would be created and the message |
| 1438 | # would appear - hence the "if posix" clause. |
| 1439 | logger = logging.getLogger('setup') |
| 1440 | logger.critical('Should not appear, because of disabled logger ...') |
| 1441 | for i in range(100): |
| 1442 | lvl = random.choice(levels) |
| 1443 | logger = logging.getLogger(random.choice(loggers)) |
| 1444 | logger.log(lvl, 'Message no. %d', i) |
| 1445 | time.sleep(0.01) |
| 1446 | |
| 1447 | def main(): |
| 1448 | q = Queue() |
| 1449 | # The main process gets a simple configuration which prints to the console. |
| 1450 | config_initial = { |
| 1451 | 'version': 1, |
| 1452 | 'formatters': { |
| 1453 | 'detailed': { |
| 1454 | 'class': 'logging.Formatter', |
| 1455 | 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s' |
| 1456 | } |
| 1457 | }, |
| 1458 | 'handlers': { |
| 1459 | 'console': { |
| 1460 | 'class': 'logging.StreamHandler', |
| 1461 | 'level': 'INFO', |
| 1462 | }, |
| 1463 | }, |
| 1464 | 'root': { |
| 1465 | 'level': 'DEBUG', |
| 1466 | 'handlers': ['console'] |
| 1467 | }, |
| 1468 | } |
| 1469 | # The worker process configuration is just a QueueHandler attached to the |
| 1470 | # root logger, which allows all messages to be sent to the queue. |
| 1471 | # We disable existing loggers to disable the "setup" logger used in the |
| 1472 | # parent process. This is needed on POSIX because the logger will |
| 1473 | # be there in the child following a fork(). |
| 1474 | config_worker = { |
| 1475 | 'version': 1, |
| 1476 | 'disable_existing_loggers': True, |
| 1477 | 'handlers': { |
| 1478 | 'queue': { |
| 1479 | 'class': 'logging.handlers.QueueHandler', |
| 1480 | 'queue': q, |
| 1481 | }, |
| 1482 | }, |
| 1483 | 'root': { |
| 1484 | 'level': 'DEBUG', |
| 1485 | 'handlers': ['queue'] |
| 1486 | }, |
| 1487 | } |
| 1488 | # The listener process configuration shows that the full flexibility of |
| 1489 | # logging configuration is available to dispatch events to handlers however |
| 1490 | # you want. |
| 1491 | # We disable existing loggers to disable the "setup" logger used in the |
| 1492 | # parent process. This is needed on POSIX because the logger will |
| 1493 | # be there in the child following a fork(). |
| 1494 | config_listener = { |
| 1495 | 'version': 1, |
| 1496 | 'disable_existing_loggers': True, |
| 1497 | 'formatters': { |
| 1498 | 'detailed': { |
| 1499 | 'class': 'logging.Formatter', |
| 1500 | 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s' |
| 1501 | }, |
| 1502 | 'simple': { |
| 1503 | 'class': 'logging.Formatter', |
| 1504 | 'format': '%(name)-15s %(levelname)-8s %(processName)-10s %(message)s' |
| 1505 | } |
| 1506 | }, |
| 1507 | 'handlers': { |
| 1508 | 'console': { |
| 1509 | 'class': 'logging.StreamHandler', |
| 1510 | 'level': 'INFO', |
| 1511 | 'formatter': 'simple', |
| 1512 | }, |
| 1513 | 'file': { |
| 1514 | 'class': 'logging.FileHandler', |
| 1515 | 'filename': 'mplog.log', |
| 1516 | 'mode': 'w', |
| 1517 | 'formatter': 'detailed', |
| 1518 | }, |
| 1519 | 'foofile': { |
| 1520 | 'class': 'logging.FileHandler', |
| 1521 | 'filename': 'mplog-foo.log', |
| 1522 | 'mode': 'w', |
| 1523 | 'formatter': 'detailed', |
| 1524 | }, |
| 1525 | 'errors': { |
| 1526 | 'class': 'logging.FileHandler', |
| 1527 | 'filename': 'mplog-errors.log', |
| 1528 | 'mode': 'w', |
| 1529 | 'level': 'ERROR', |
| 1530 | 'formatter': 'detailed', |
| 1531 | }, |
| 1532 | }, |
| 1533 | 'loggers': { |
| 1534 | 'foo': { |
| 1535 | 'handlers' : ['foofile'] |
| 1536 | } |
| 1537 | }, |
| 1538 | 'root': { |
| 1539 | 'level': 'DEBUG', |
| 1540 | 'handlers': ['console', 'file', 'errors'] |
| 1541 | }, |
| 1542 | } |
| 1543 | # Log some initial events, just to show that logging in the parent works |
| 1544 | # normally. |
| 1545 | logging.config.dictConfig(config_initial) |
| 1546 | logger = logging.getLogger('setup') |
| 1547 | logger.info('About to create workers ...') |
| 1548 | workers = [] |
| 1549 | for i in range(5): |
| 1550 | wp = Process(target=worker_process, name='worker %d' % (i + 1), |
| 1551 | args=(config_worker,)) |
| 1552 | workers.append(wp) |
| 1553 | wp.start() |
| 1554 | logger.info('Started worker: %s', wp.name) |
| 1555 | logger.info('About to create listener ...') |
| 1556 | stop_event = Event() |
| 1557 | lp = Process(target=listener_process, name='listener', |
| 1558 | args=(q, stop_event, config_listener)) |
| 1559 | lp.start() |
| 1560 | logger.info('Started listener') |
| 1561 | # We now hang around for the workers to finish their work. |
| 1562 | for wp in workers: |
| 1563 | wp.join() |
| 1564 | # Workers all done, listening can now stop. |
| 1565 | # Logging in the parent still works normally. |
| 1566 | logger.info('Telling listener to stop ...') |
| 1567 | stop_event.set() |
| 1568 | lp.join() |
| 1569 | logger.info('All done.') |
| 1570 | |
| 1571 | if __name__ == '__main__': |
| 1572 | main() |
| 1573 | |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1574 | |
| 1575 | Inserting a BOM into messages sent to a SysLogHandler |
| 1576 | ----------------------------------------------------- |
| 1577 | |
| 1578 | `RFC 5424 <http://tools.ietf.org/html/rfc5424>`_ requires that a |
| 1579 | Unicode message be sent to a syslog daemon as a set of bytes which have the |
| 1580 | following structure: an optional pure-ASCII component, followed by a UTF-8 Byte |
| 1581 | Order Mark (BOM), followed by Unicode encoded using UTF-8. (See the `relevant |
| 1582 | section of the specification <http://tools.ietf.org/html/rfc5424#section-6>`_.) |
| 1583 | |
Vinay Sajip | 62930e1 | 2012-04-17 00:40:48 +0100 | [diff] [blame] | 1584 | In Python 3.1, code was added to |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1585 | :class:`~logging.handlers.SysLogHandler` to insert a BOM into the message, but |
| 1586 | unfortunately, it was implemented incorrectly, with the BOM appearing at the |
| 1587 | beginning of the message and hence not allowing any pure-ASCII component to |
| 1588 | appear before it. |
| 1589 | |
| 1590 | As this behaviour is broken, the incorrect BOM insertion code is being removed |
Vinay Sajip | 62930e1 | 2012-04-17 00:40:48 +0100 | [diff] [blame] | 1591 | from Python 3.2.4 and later. However, it is not being replaced, and if you |
Vinay Sajip | a58d668 | 2012-07-27 10:54:10 +0100 | [diff] [blame] | 1592 | want to produce RFC 5424-compliant messages which include a BOM, an optional |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1593 | pure-ASCII sequence before it and arbitrary Unicode after it, encoded using |
| 1594 | UTF-8, then you need to do the following: |
| 1595 | |
| 1596 | #. Attach a :class:`~logging.Formatter` instance to your |
| 1597 | :class:`~logging.handlers.SysLogHandler` instance, with a format string |
| 1598 | such as:: |
| 1599 | |
Vinay Sajip | 59b9a79 | 2012-04-16 15:46:18 +0100 | [diff] [blame] | 1600 | 'ASCII section\ufeffUnicode section' |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1601 | |
Georg Brandl | ebb2964 | 2012-05-01 09:29:56 +0200 | [diff] [blame] | 1602 | The Unicode code point ``'\feff'``, when encoded using UTF-8, will be |
Vinay Sajip | 59b9a79 | 2012-04-16 15:46:18 +0100 | [diff] [blame] | 1603 | encoded as a UTF-8 BOM -- the byte-string ``b'\xef\xbb\xbf'``. |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1604 | |
| 1605 | #. Replace the ASCII section with whatever placeholders you like, but make sure |
| 1606 | that the data that appears in there after substitution is always ASCII (that |
| 1607 | way, it will remain unchanged after UTF-8 encoding). |
| 1608 | |
| 1609 | #. Replace the Unicode section with whatever placeholders you like; if the data |
Vinay Sajip | a58d668 | 2012-07-27 10:54:10 +0100 | [diff] [blame] | 1610 | which appears there after substitution contains characters outside the ASCII |
| 1611 | range, that's fine -- it will be encoded using UTF-8. |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1612 | |
Vinay Sajip | 59b9a79 | 2012-04-16 15:46:18 +0100 | [diff] [blame] | 1613 | The formatted message *will* be encoded using UTF-8 encoding by |
| 1614 | ``SysLogHandler``. If you follow the above rules, you should be able to produce |
Vinay Sajip | b00e8f1 | 2012-04-16 15:28:50 +0100 | [diff] [blame] | 1615 | RFC 5424-compliant messages. If you don't, logging may not complain, but your |
| 1616 | messages will not be RFC 5424-compliant, and your syslog daemon may complain. |
| 1617 | |