source: python/trunk/Doc/library/logging.rst

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 43.7 KB
RevLine 
[2]1:mod:`logging` --- Logging facility for Python
2==============================================
3
4.. module:: logging
[391]5 :synopsis: Flexible event logging system for applications.
[2]6
7
8.. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
9.. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
10
11
12.. index:: pair: Errors; logging
13
[391]14.. sidebar:: Important
[2]15
[391]16 This page contains the API reference information. For tutorial
17 information and discussion of more advanced topics, see
[2]18
[391]19 * :ref:`Basic Tutorial <logging-basic-tutorial>`
20 * :ref:`Advanced Tutorial <logging-advanced-tutorial>`
21 * :ref:`Logging Cookbook <logging-cookbook>`
[2]22
[391]23**Source code:** :source:`Lib/logging/__init__.py`
[2]24
[391]25--------------
[2]26
[391]27.. versionadded:: 2.3
[2]28
[391]29This module defines functions and classes which implement a flexible event
30logging system for applications and libraries.
31
[2]32The key benefit of having the logging API provided by a standard library module
33is that all Python modules can participate in logging, so your application log
[391]34can include your own messages integrated with messages from third-party
35modules.
[2]36
[391]37The module provides a lot of functionality and flexibility. If you are
38unfamiliar with logging, the best way to get to grips with it is to see the
39tutorials (see the links on the right).
[2]40
[391]41The basic classes defined by the module, together with their functions, are
42listed below.
[2]43
[391]44* Loggers expose the interface that application code directly uses.
45* Handlers send the log records (created by loggers) to the appropriate
46 destination.
47* Filters provide a finer grained facility for determining which log records
48 to output.
49* Formatters specify the layout of log records in the final output.
[2]50
51
[391]52.. _logger:
[2]53
[391]54Logger Objects
[2]55--------------
56
[391]57Loggers have the following attributes and methods. Note that Loggers are never
58instantiated directly, but always through the module-level function
59``logging.getLogger(name)``. Multiple calls to :func:`getLogger` with the same
60name will always return a reference to the same Logger object.
[2]61
[391]62The ``name`` is potentially a period-separated hierarchical value, like
63``foo.bar.baz`` (though it could also be just plain ``foo``, for example).
64Loggers that are further down in the hierarchical list are children of loggers
65higher up in the list. For example, given a logger with a name of ``foo``,
66loggers with names of ``foo.bar``, ``foo.bar.baz``, and ``foo.bam`` are all
67descendants of ``foo``. The logger name hierarchy is analogous to the Python
68package hierarchy, and identical to it if you organise your loggers on a
69per-module basis using the recommended construction
70``logging.getLogger(__name__)``. That's because in a module, ``__name__``
71is the module's name in the Python package namespace.
[2]72
73
[391]74.. class:: Logger
[2]75
[391]76.. attribute:: Logger.propagate
[2]77
[391]78 If this evaluates to true, events logged to this logger will be passed to the
79 handlers of higher level (ancestor) loggers, in addition to any handlers
80 attached to this logger. Messages are passed directly to the ancestor
81 loggers' handlers - neither the level nor filters of the ancestor loggers in
82 question are considered.
[2]83
[391]84 If this evaluates to false, logging messages are not passed to the handlers
85 of ancestor loggers.
[2]86
[391]87 The constructor sets this attribute to ``True``.
[2]88
[391]89 .. note:: If you attach a handler to a logger *and* one or more of its
90 ancestors, it may emit the same record multiple times. In general, you
91 should not need to attach a handler to more than one logger - if you just
92 attach it to the appropriate logger which is highest in the logger
93 hierarchy, then it will see all events logged by all descendant loggers,
94 provided that their propagate setting is left set to ``True``. A common
95 scenario is to attach handlers only to the root logger, and to let
96 propagation take care of the rest.
[2]97
98.. method:: Logger.setLevel(lvl)
99
100 Sets the threshold for this logger to *lvl*. Logging messages which are less
101 severe than *lvl* will be ignored. When a logger is created, the level is set to
102 :const:`NOTSET` (which causes all messages to be processed when the logger is
103 the root logger, or delegation to the parent when the logger is a non-root
104 logger). Note that the root logger is created with level :const:`WARNING`.
105
[391]106 The term 'delegation to the parent' means that if a logger has a level of
[2]107 NOTSET, its chain of ancestor loggers is traversed until either an ancestor with
108 a level other than NOTSET is found, or the root is reached.
109
110 If an ancestor is found with a level other than NOTSET, then that ancestor's
111 level is treated as the effective level of the logger where the ancestor search
112 began, and is used to determine how a logging event is handled.
113
114 If the root is reached, and it has a level of NOTSET, then all messages will be
115 processed. Otherwise, the root's level will be used as the effective level.
116
117
118.. method:: Logger.isEnabledFor(lvl)
119
120 Indicates if a message of severity *lvl* would be processed by this logger.
121 This method checks first the module-level level set by
122 ``logging.disable(lvl)`` and then the logger's effective level as determined
123 by :meth:`getEffectiveLevel`.
124
125
126.. method:: Logger.getEffectiveLevel()
127
128 Indicates the effective level for this logger. If a value other than
129 :const:`NOTSET` has been set using :meth:`setLevel`, it is returned. Otherwise,
130 the hierarchy is traversed towards the root until a value other than
131 :const:`NOTSET` is found, and that value is returned.
132
133
[391]134.. method:: Logger.getChild(suffix)
[2]135
[391]136 Returns a logger which is a descendant to this logger, as determined by the suffix.
137 Thus, ``logging.getLogger('abc').getChild('def.ghi')`` would return the same
138 logger as would be returned by ``logging.getLogger('abc.def.ghi')``. This is a
139 convenience method, useful when the parent logger is named using e.g. ``__name__``
140 rather than a literal string.
141
142 .. versionadded:: 2.7
143
144
145.. method:: Logger.debug(msg, *args, **kwargs)
146
[2]147 Logs a message with level :const:`DEBUG` on this logger. The *msg* is the
148 message format string, and the *args* are the arguments which are merged into
149 *msg* using the string formatting operator. (Note that this means that you can
150 use keywords in the format string, together with a single dictionary argument.)
151
152 There are two keyword arguments in *kwargs* which are inspected: *exc_info*
153 which, if it does not evaluate as false, causes exception information to be
154 added to the logging message. If an exception tuple (in the format returned by
155 :func:`sys.exc_info`) is provided, it is used; otherwise, :func:`sys.exc_info`
156 is called to get the exception information.
157
[391]158 The second keyword argument is *extra* which can be used to pass a
[2]159 dictionary which is used to populate the __dict__ of the LogRecord created for
160 the logging event with user-defined attributes. These custom attributes can then
161 be used as you like. For example, they could be incorporated into logged
162 messages. For example::
163
[391]164 FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
[2]165 logging.basicConfig(format=FORMAT)
[391]166 d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
167 logger = logging.getLogger('tcpserver')
168 logger.warning('Protocol problem: %s', 'connection reset', extra=d)
[2]169
170 would print something like ::
171
172 2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
173
174 The keys in the dictionary passed in *extra* should not clash with the keys used
175 by the logging system. (See the :class:`Formatter` documentation for more
176 information on which keys are used by the logging system.)
177
178 If you choose to use these attributes in logged messages, you need to exercise
179 some care. In the above example, for instance, the :class:`Formatter` has been
180 set up with a format string which expects 'clientip' and 'user' in the attribute
181 dictionary of the LogRecord. If these are missing, the message will not be
182 logged because a string formatting exception will occur. So in this case, you
183 always need to pass the *extra* dictionary with these keys.
184
185 While this might be annoying, this feature is intended for use in specialized
186 circumstances, such as multi-threaded servers where the same code executes in
187 many contexts, and interesting conditions which arise are dependent on this
188 context (such as remote client IP address and authenticated user name, in the
189 above example). In such circumstances, it is likely that specialized
190 :class:`Formatter`\ s would be used with particular :class:`Handler`\ s.
191
192
[391]193.. method:: Logger.info(msg, *args, **kwargs)
[2]194
195 Logs a message with level :const:`INFO` on this logger. The arguments are
196 interpreted as for :meth:`debug`.
197
198
[391]199.. method:: Logger.warning(msg, *args, **kwargs)
[2]200
201 Logs a message with level :const:`WARNING` on this logger. The arguments are
202 interpreted as for :meth:`debug`.
203
204
[391]205.. method:: Logger.error(msg, *args, **kwargs)
[2]206
207 Logs a message with level :const:`ERROR` on this logger. The arguments are
208 interpreted as for :meth:`debug`.
209
210
[391]211.. method:: Logger.critical(msg, *args, **kwargs)
[2]212
213 Logs a message with level :const:`CRITICAL` on this logger. The arguments are
214 interpreted as for :meth:`debug`.
215
216
[391]217.. method:: Logger.log(lvl, msg, *args, **kwargs)
[2]218
219 Logs a message with integer level *lvl* on this logger. The other arguments are
220 interpreted as for :meth:`debug`.
221
222
[391]223.. method:: Logger.exception(msg, *args)
[2]224
225 Logs a message with level :const:`ERROR` on this logger. The arguments are
226 interpreted as for :meth:`debug`. Exception info is added to the logging
227 message. This method should only be called from an exception handler.
228
229
230.. method:: Logger.addFilter(filt)
231
232 Adds the specified filter *filt* to this logger.
233
234
235.. method:: Logger.removeFilter(filt)
236
237 Removes the specified filter *filt* from this logger.
238
239
240.. method:: Logger.filter(record)
241
242 Applies this logger's filters to the record and returns a true value if the
[391]243 record is to be processed. The filters are consulted in turn, until one of
244 them returns a false value. If none of them return a false value, the record
245 will be processed (passed to handlers). If one returns a false value, no
246 further processing of the record occurs.
[2]247
248
249.. method:: Logger.addHandler(hdlr)
250
251 Adds the specified handler *hdlr* to this logger.
252
253
254.. method:: Logger.removeHandler(hdlr)
255
256 Removes the specified handler *hdlr* from this logger.
257
258
259.. method:: Logger.findCaller()
260
261 Finds the caller's source filename and line number. Returns the filename, line
262 number and function name as a 3-element tuple.
263
264 .. versionchanged:: 2.4
[391]265 The function name was added. In earlier versions, the filename and line
266 number were returned as a 2-element tuple.
[2]267
268.. method:: Logger.handle(record)
269
270 Handles a record by passing it to all handlers associated with this logger and
271 its ancestors (until a false value of *propagate* is found). This method is used
272 for unpickled records received from a socket, as well as those created locally.
273 Logger-level filtering is applied using :meth:`~Logger.filter`.
274
275
[391]276.. method:: Logger.makeRecord(name, lvl, fn, lno, msg, args, exc_info, func=None, extra=None)
[2]277
278 This is a factory method which can be overridden in subclasses to create
279 specialized :class:`LogRecord` instances.
280
281 .. versionchanged:: 2.5
282 *func* and *extra* were added.
283
284.. _handler:
285
286Handler Objects
287---------------
288
289Handlers have the following attributes and methods. Note that :class:`Handler`
290is never instantiated directly; this class acts as a base for more useful
291subclasses. However, the :meth:`__init__` method in subclasses needs to call
292:meth:`Handler.__init__`.
293
294
295.. method:: Handler.__init__(level=NOTSET)
296
297 Initializes the :class:`Handler` instance by setting its level, setting the list
298 of filters to the empty list and creating a lock (using :meth:`createLock`) for
299 serializing access to an I/O mechanism.
300
301
302.. method:: Handler.createLock()
303
304 Initializes a thread lock which can be used to serialize access to underlying
305 I/O functionality which may not be threadsafe.
306
307
308.. method:: Handler.acquire()
309
310 Acquires the thread lock created with :meth:`createLock`.
311
312
313.. method:: Handler.release()
314
315 Releases the thread lock acquired with :meth:`acquire`.
316
317
318.. method:: Handler.setLevel(lvl)
319
320 Sets the threshold for this handler to *lvl*. Logging messages which are less
321 severe than *lvl* will be ignored. When a handler is created, the level is set
322 to :const:`NOTSET` (which causes all messages to be processed).
323
324
325.. method:: Handler.setFormatter(form)
326
327 Sets the :class:`Formatter` for this handler to *form*.
328
329
330.. method:: Handler.addFilter(filt)
331
332 Adds the specified filter *filt* to this handler.
333
334
335.. method:: Handler.removeFilter(filt)
336
337 Removes the specified filter *filt* from this handler.
338
339
340.. method:: Handler.filter(record)
341
342 Applies this handler's filters to the record and returns a true value if the
[391]343 record is to be processed. The filters are consulted in turn, until one of
344 them returns a false value. If none of them return a false value, the record
345 will be emitted. If one returns a false value, the handler will not emit the
346 record.
[2]347
348
349.. method:: Handler.flush()
350
351 Ensure all logging output has been flushed. This version does nothing and is
352 intended to be implemented by subclasses.
353
354
355.. method:: Handler.close()
356
357 Tidy up any resources used by the handler. This version does no output but
358 removes the handler from an internal list of handlers which is closed when
359 :func:`shutdown` is called. Subclasses should ensure that this gets called
360 from overridden :meth:`close` methods.
361
362
363.. method:: Handler.handle(record)
364
365 Conditionally emits the specified logging record, depending on filters which may
366 have been added to the handler. Wraps the actual emission of the record with
367 acquisition/release of the I/O thread lock.
368
369
370.. method:: Handler.handleError(record)
371
372 This method should be called from handlers when an exception is encountered
[391]373 during an :meth:`emit` call. If the module-level attribute
374 ``raiseExceptions`` is ``False``, exceptions get silently ignored. This is
375 what is mostly wanted for a logging system - most users will not care about
376 errors in the logging system, they are more interested in application
377 errors. You could, however, replace this with a custom handler if you wish.
378 The specified record is the one which was being processed when the exception
379 occurred. (The default value of ``raiseExceptions`` is ``True``, as that is
380 more useful during development).
[2]381
382
383.. method:: Handler.format(record)
384
385 Do formatting for a record - if a formatter is set, use it. Otherwise, use the
386 default formatter for the module.
387
388
389.. method:: Handler.emit(record)
390
391 Do whatever it takes to actually log the specified logging record. This version
392 is intended to be implemented by subclasses and so raises a
393 :exc:`NotImplementedError`.
394
[391]395For a list of handlers included as standard, see :mod:`logging.handlers`.
[2]396
[391]397.. _formatter-objects:
[2]398
399Formatter Objects
400-----------------
401
402.. currentmodule:: logging
403
[391]404:class:`Formatter` objects have the following attributes and methods. They are
[2]405responsible for converting a :class:`LogRecord` to (usually) a string which can
406be interpreted by either a human or an external system. The base
407:class:`Formatter` allows a formatting string to be specified. If none is
408supplied, the default value of ``'%(message)s'`` is used.
409
410A Formatter can be initialized with a format string which makes use of knowledge
411of the :class:`LogRecord` attributes - such as the default value mentioned above
412making use of the fact that the user's message and arguments are pre-formatted
413into a :class:`LogRecord`'s *message* attribute. This format string contains
414standard Python %-style mapping keys. See section :ref:`string-formatting`
415for more information on string formatting.
416
[391]417The useful mapping keys in a :class:`LogRecord` are given in the section on
418:ref:`logrecord-attributes`.
[2]419
420
[391]421.. class:: Formatter(fmt=None, datefmt=None)
[2]422
[391]423 Returns a new instance of the :class:`Formatter` class. The instance is
424 initialized with a format string for the message as a whole, as well as a
425 format string for the date/time portion of a message. If no *fmt* is
426 specified, ``'%(message)s'`` is used. If no *datefmt* is specified, the
427 ISO8601 date format is used.
[2]428
429 .. method:: format(record)
430
431 The record's attribute dictionary is used as the operand to a string
432 formatting operation. Returns the resulting string. Before formatting the
433 dictionary, a couple of preparatory steps are carried out. The *message*
434 attribute of the record is computed using *msg* % *args*. If the
435 formatting string contains ``'(asctime)'``, :meth:`formatTime` is called
436 to format the event time. If there is exception information, it is
437 formatted using :meth:`formatException` and appended to the message. Note
438 that the formatted exception information is cached in attribute
439 *exc_text*. This is useful because the exception information can be
440 pickled and sent across the wire, but you should be careful if you have
441 more than one :class:`Formatter` subclass which customizes the formatting
442 of exception information. In this case, you will have to clear the cached
443 value after a formatter has done its formatting, so that the next
444 formatter to handle the event doesn't use the cached value but
445 recalculates it afresh.
446
447
[391]448 .. method:: formatTime(record, datefmt=None)
[2]449
450 This method should be called from :meth:`format` by a formatter which
451 wants to make use of a formatted time. This method can be overridden in
452 formatters to provide for any specific requirement, but the basic behavior
453 is as follows: if *datefmt* (a string) is specified, it is used with
454 :func:`time.strftime` to format the creation time of the
455 record. Otherwise, the ISO8601 format is used. The resulting string is
456 returned.
457
[391]458 This function uses a user-configurable function to convert the creation
459 time to a tuple. By default, :func:`time.localtime` is used; to change
460 this for a particular formatter instance, set the ``converter`` attribute
461 to a function with the same signature as :func:`time.localtime` or
462 :func:`time.gmtime`. To change it for all formatters, for example if you
463 want all logging times to be shown in GMT, set the ``converter``
464 attribute in the ``Formatter`` class.
[2]465
466 .. method:: formatException(exc_info)
467
468 Formats the specified exception information (a standard exception tuple as
469 returned by :func:`sys.exc_info`) as a string. This default implementation
470 just uses :func:`traceback.print_exception`. The resulting string is
471 returned.
472
473.. _filter:
474
475Filter Objects
476--------------
477
[391]478``Filters`` can be used by ``Handlers`` and ``Loggers`` for more sophisticated
479filtering than is provided by levels. The base filter class only allows events
480which are below a certain point in the logger hierarchy. For example, a filter
481initialized with 'A.B' will allow events logged by loggers 'A.B', 'A.B.C',
482'A.B.C.D', 'A.B.D' etc. but not 'A.BB', 'B.A.B' etc. If initialized with the
483empty string, all events are passed.
[2]484
485
[391]486.. class:: Filter(name='')
[2]487
488 Returns an instance of the :class:`Filter` class. If *name* is specified, it
489 names a logger which, together with its children, will have its events allowed
[391]490 through the filter. If *name* is the empty string, allows every event.
[2]491
492
493 .. method:: filter(record)
494
495 Is the specified record to be logged? Returns zero for no, nonzero for
496 yes. If deemed appropriate, the record may be modified in-place by this
497 method.
498
[391]499Note that filters attached to handlers are consulted before an event is
500emitted by the handler, whereas filters attached to loggers are consulted
501whenever an event is logged (using :meth:`debug`, :meth:`info`,
502etc.), before sending an event to handlers. This means that events which have
503been generated by descendant loggers will not be filtered by a logger's filter
504setting, unless the filter has also been applied to those descendant loggers.
505
506You don't actually need to subclass ``Filter``: you can pass any instance
507which has a ``filter`` method with the same semantics.
508
509Although filters are used primarily to filter records based on more
510sophisticated criteria than levels, they get to see every record which is
511processed by the handler or logger they're attached to: this can be useful if
512you want to do things like counting how many records were processed by a
513particular logger or handler, or adding, changing or removing attributes in
514the LogRecord being processed. Obviously changing the LogRecord needs to be
515done with some care, but it does allow the injection of contextual information
516into logs (see :ref:`filters-contextual`).
517
[2]518.. _log-record:
519
520LogRecord Objects
521-----------------
522
[391]523:class:`LogRecord` instances are created automatically by the :class:`Logger`
524every time something is logged, and can be created manually via
525:func:`makeLogRecord` (for example, from a pickled event received over the
526wire).
[2]527
528
[391]529.. class:: LogRecord(name, level, pathname, lineno, msg, args, exc_info, func=None)
[2]530
[391]531 Contains all the information pertinent to the event being logged.
[2]532
[391]533 The primary information is passed in :attr:`msg` and :attr:`args`, which
534 are combined using ``msg % args`` to create the :attr:`message` field of the
535 record.
536
537 :param name: The name of the logger used to log the event represented by
538 this LogRecord. Note that this name will always have this
539 value, even though it may be emitted by a handler attached to
540 a different (ancestor) logger.
541 :param level: The numeric level of the logging event (one of DEBUG, INFO etc.)
542 Note that this is converted to *two* attributes of the LogRecord:
543 ``levelno`` for the numeric value and ``levelname`` for the
544 corresponding level name.
545 :param pathname: The full pathname of the source file where the logging call
546 was made.
547 :param lineno: The line number in the source file where the logging call was
548 made.
549 :param msg: The event description message, possibly a format string with
550 placeholders for variable data.
551 :param args: Variable data to merge into the *msg* argument to obtain the
552 event description.
553 :param exc_info: An exception tuple with the current exception information,
554 or *None* if no exception information is available.
555 :param func: The name of the function or method from which the logging call
556 was invoked.
557
[2]558 .. versionchanged:: 2.5
559 *func* was added.
560
561 .. method:: getMessage()
562
563 Returns the message for this :class:`LogRecord` instance after merging any
[391]564 user-supplied arguments with the message. If the user-supplied message
565 argument to the logging call is not a string, :func:`str` is called on it to
566 convert it to a string. This allows use of user-defined classes as
567 messages, whose ``__str__`` method can return the actual format string to
568 be used.
[2]569
[391]570
571.. _logrecord-attributes:
572
573LogRecord attributes
574--------------------
575
576The LogRecord has a number of attributes, most of which are derived from the
577parameters to the constructor. (Note that the names do not always correspond
578exactly between the LogRecord constructor parameters and the LogRecord
579attributes.) These attributes can be used to merge data from the record into
580the format string. The following table lists (in alphabetical order) the
581attribute names, their meanings and the corresponding placeholder in a %-style
582format string.
583
584+----------------+-------------------------+-----------------------------------------------+
585| Attribute name | Format | Description |
586+================+=========================+===============================================+
587| args | You shouldn't need to | The tuple of arguments merged into ``msg`` to |
588| | format this yourself. | produce ``message``. |
589+----------------+-------------------------+-----------------------------------------------+
590| asctime | ``%(asctime)s`` | Human-readable time when the |
591| | | :class:`LogRecord` was created. By default |
592| | | this is of the form '2003-07-08 16:49:45,896' |
593| | | (the numbers after the comma are millisecond |
594| | | portion of the time). |
595+----------------+-------------------------+-----------------------------------------------+
596| created | ``%(created)f`` | Time when the :class:`LogRecord` was created |
597| | | (as returned by :func:`time.time`). |
598+----------------+-------------------------+-----------------------------------------------+
599| exc_info | You shouldn't need to | Exception tuple (à la ``sys.exc_info``) or, |
600| | format this yourself. | if no exception has occurred, *None*. |
601+----------------+-------------------------+-----------------------------------------------+
602| filename | ``%(filename)s`` | Filename portion of ``pathname``. |
603+----------------+-------------------------+-----------------------------------------------+
604| funcName | ``%(funcName)s`` | Name of function containing the logging call. |
605+----------------+-------------------------+-----------------------------------------------+
606| levelname | ``%(levelname)s`` | Text logging level for the message |
607| | | (``'DEBUG'``, ``'INFO'``, ``'WARNING'``, |
608| | | ``'ERROR'``, ``'CRITICAL'``). |
609+----------------+-------------------------+-----------------------------------------------+
610| levelno | ``%(levelno)s`` | Numeric logging level for the message |
611| | | (:const:`DEBUG`, :const:`INFO`, |
612| | | :const:`WARNING`, :const:`ERROR`, |
613| | | :const:`CRITICAL`). |
614+----------------+-------------------------+-----------------------------------------------+
615| lineno | ``%(lineno)d`` | Source line number where the logging call was |
616| | | issued (if available). |
617+----------------+-------------------------+-----------------------------------------------+
618| module | ``%(module)s`` | Module (name portion of ``filename``). |
619+----------------+-------------------------+-----------------------------------------------+
620| msecs | ``%(msecs)d`` | Millisecond portion of the time when the |
621| | | :class:`LogRecord` was created. |
622+----------------+-------------------------+-----------------------------------------------+
623| message | ``%(message)s`` | The logged message, computed as ``msg % |
624| | | args``. This is set when |
625| | | :meth:`Formatter.format` is invoked. |
626+----------------+-------------------------+-----------------------------------------------+
627| msg | You shouldn't need to | The format string passed in the original |
628| | format this yourself. | logging call. Merged with ``args`` to |
629| | | produce ``message``, or an arbitrary object |
630| | | (see :ref:`arbitrary-object-messages`). |
631+----------------+-------------------------+-----------------------------------------------+
632| name | ``%(name)s`` | Name of the logger used to log the call. |
633+----------------+-------------------------+-----------------------------------------------+
634| pathname | ``%(pathname)s`` | Full pathname of the source file where the |
635| | | logging call was issued (if available). |
636+----------------+-------------------------+-----------------------------------------------+
637| process | ``%(process)d`` | Process ID (if available). |
638+----------------+-------------------------+-----------------------------------------------+
639| processName | ``%(processName)s`` | Process name (if available). |
640+----------------+-------------------------+-----------------------------------------------+
641| relativeCreated| ``%(relativeCreated)d`` | Time in milliseconds when the LogRecord was |
642| | | created, relative to the time the logging |
643| | | module was loaded. |
644+----------------+-------------------------+-----------------------------------------------+
645| thread | ``%(thread)d`` | Thread ID (if available). |
646+----------------+-------------------------+-----------------------------------------------+
647| threadName | ``%(threadName)s`` | Thread name (if available). |
648+----------------+-------------------------+-----------------------------------------------+
649
650.. versionchanged:: 2.5
651 *funcName* was added.
652
653.. versionchanged:: 2.6
654 *processName* was added.
655
[2]656.. _logger-adapter:
657
658LoggerAdapter Objects
659---------------------
660
661:class:`LoggerAdapter` instances are used to conveniently pass contextual
662information into logging calls. For a usage example , see the section on
[391]663:ref:`adding contextual information to your logging output <context-info>`.
[2]664
[391]665.. versionadded:: 2.6
[2]666
[391]667
[2]668.. class:: LoggerAdapter(logger, extra)
669
[391]670 Returns an instance of :class:`LoggerAdapter` initialized with an
671 underlying :class:`Logger` instance and a dict-like object.
[2]672
[391]673 .. method:: process(msg, kwargs)
[2]674
[391]675 Modifies the message and/or keyword arguments passed to a logging call in
676 order to insert contextual information. This implementation takes the object
677 passed as *extra* to the constructor and adds it to *kwargs* using key
678 'extra'. The return value is a (*msg*, *kwargs*) tuple which has the
679 (possibly modified) versions of the arguments passed in.
[2]680
[391]681In addition to the above, :class:`LoggerAdapter` supports the following
[2]682methods of :class:`Logger`, i.e. :meth:`debug`, :meth:`info`, :meth:`warning`,
[391]683:meth:`error`, :meth:`exception`, :meth:`critical`, :meth:`log`,
684:meth:`isEnabledFor`, :meth:`getEffectiveLevel`, :meth:`setLevel`,
685:meth:`hasHandlers`. These methods have the same signatures as their
686counterparts in :class:`Logger`, so you can use the two types of instances
687interchangeably.
[2]688
[391]689.. versionchanged:: 2.7
690 The :meth:`isEnabledFor` method was added to :class:`LoggerAdapter`. This
691 method delegates to the underlying logger.
[2]692
[391]693
[2]694Thread Safety
695-------------
696
697The logging module is intended to be thread-safe without any special work
698needing to be done by its clients. It achieves this though using threading
699locks; there is one lock to serialize access to the module's shared data, and
700each handler also creates a lock to serialize access to its underlying I/O.
701
702If you are implementing asynchronous signal handlers using the :mod:`signal`
703module, you may not be able to use logging from within such handlers. This is
704because lock implementations in the :mod:`threading` module are not always
705re-entrant, and so cannot be invoked from such signal handlers.
706
707
[391]708Module-Level Functions
709----------------------
[2]710
[391]711In addition to the classes described above, there are a number of module- level
712functions.
[2]713
714
[391]715.. function:: getLogger([name])
[2]716
[391]717 Return a logger with the specified name or, if no name is specified, return a
718 logger which is the root logger of the hierarchy. If specified, the name is
719 typically a dot-separated hierarchical name like *"a"*, *"a.b"* or *"a.b.c.d"*.
720 Choice of these names is entirely up to the developer who is using logging.
[2]721
[391]722 All calls to this function with a given name return the same logger instance.
723 This means that logger instances never need to be passed between different parts
724 of an application.
[2]725
726
[391]727.. function:: getLoggerClass()
[2]728
[391]729 Return either the standard :class:`Logger` class, or the last class passed to
730 :func:`setLoggerClass`. This function may be called from within a new class
731 definition, to ensure that installing a customised :class:`Logger` class will
732 not undo customisations already applied by other code. For example::
[2]733
[391]734 class MyLogger(logging.getLoggerClass()):
735 # ... override behaviour here
[2]736
737
[391]738.. function:: debug(msg[, *args[, **kwargs]])
[2]739
[391]740 Logs a message with level :const:`DEBUG` on the root logger. The *msg* is the
741 message format string, and the *args* are the arguments which are merged into
742 *msg* using the string formatting operator. (Note that this means that you can
743 use keywords in the format string, together with a single dictionary argument.)
[2]744
[391]745 There are two keyword arguments in *kwargs* which are inspected: *exc_info*
746 which, if it does not evaluate as false, causes exception information to be
747 added to the logging message. If an exception tuple (in the format returned by
748 :func:`sys.exc_info`) is provided, it is used; otherwise, :func:`sys.exc_info`
749 is called to get the exception information.
[2]750
[391]751 The other optional keyword argument is *extra* which can be used to pass a
752 dictionary which is used to populate the __dict__ of the LogRecord created for
753 the logging event with user-defined attributes. These custom attributes can then
754 be used as you like. For example, they could be incorporated into logged
755 messages. For example::
[2]756
[391]757 FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"
758 logging.basicConfig(format=FORMAT)
759 d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
760 logging.warning("Protocol problem: %s", "connection reset", extra=d)
[2]761
[391]762 would print something like::
[2]763
[391]764 2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
[2]765
[391]766 The keys in the dictionary passed in *extra* should not clash with the keys used
767 by the logging system. (See the :class:`Formatter` documentation for more
768 information on which keys are used by the logging system.)
[2]769
[391]770 If you choose to use these attributes in logged messages, you need to exercise
771 some care. In the above example, for instance, the :class:`Formatter` has been
772 set up with a format string which expects 'clientip' and 'user' in the attribute
773 dictionary of the LogRecord. If these are missing, the message will not be
774 logged because a string formatting exception will occur. So in this case, you
775 always need to pass the *extra* dictionary with these keys.
[2]776
[391]777 While this might be annoying, this feature is intended for use in specialized
778 circumstances, such as multi-threaded servers where the same code executes in
779 many contexts, and interesting conditions which arise are dependent on this
780 context (such as remote client IP address and authenticated user name, in the
781 above example). In such circumstances, it is likely that specialized
782 :class:`Formatter`\ s would be used with particular :class:`Handler`\ s.
[2]783
[391]784 .. versionchanged:: 2.5
785 *extra* was added.
[2]786
787
[391]788.. function:: info(msg[, *args[, **kwargs]])
[2]789
[391]790 Logs a message with level :const:`INFO` on the root logger. The arguments are
791 interpreted as for :func:`debug`.
[2]792
793
[391]794.. function:: warning(msg[, *args[, **kwargs]])
[2]795
[391]796 Logs a message with level :const:`WARNING` on the root logger. The arguments are
797 interpreted as for :func:`debug`.
[2]798
799
[391]800.. function:: error(msg[, *args[, **kwargs]])
[2]801
[391]802 Logs a message with level :const:`ERROR` on the root logger. The arguments are
803 interpreted as for :func:`debug`.
[2]804
805
[391]806.. function:: critical(msg[, *args[, **kwargs]])
[2]807
[391]808 Logs a message with level :const:`CRITICAL` on the root logger. The arguments
809 are interpreted as for :func:`debug`.
[2]810
811
[391]812.. function:: exception(msg[, *args])
[2]813
[391]814 Logs a message with level :const:`ERROR` on the root logger. The arguments are
815 interpreted as for :func:`debug`. Exception info is added to the logging
816 message. This function should only be called from an exception handler.
[2]817
818
[391]819.. function:: log(level, msg[, *args[, **kwargs]])
[2]820
[391]821 Logs a message with level *level* on the root logger. The other arguments are
822 interpreted as for :func:`debug`.
[2]823
[391]824 .. note:: The above module-level functions which delegate to the root
825 logger should *not* be used in threads, in versions of Python earlier
826 than 2.7.1 and 3.2, unless at least one handler has been added to the
827 root logger *before* the threads are started. These convenience functions
828 call :func:`basicConfig` to ensure that at least one handler is
829 available; in earlier versions of Python, this can (under rare
830 circumstances) lead to handlers being added multiple times to the root
831 logger, which can in turn lead to multiple messages for the same event.
[2]832
[391]833.. function:: disable(lvl)
[2]834
[391]835 Provides an overriding level *lvl* for all loggers which takes precedence over
836 the logger's own level. When the need arises to temporarily throttle logging
837 output down across the whole application, this function can be useful. Its
838 effect is to disable all logging calls of severity *lvl* and below, so that
839 if you call it with a value of INFO, then all INFO and DEBUG events would be
840 discarded, whereas those of severity WARNING and above would be processed
841 according to the logger's effective level. To undo the effect of a call to
842 ``logging.disable(lvl)``, call ``logging.disable(logging.NOTSET)``.
[2]843
844
[391]845.. function:: addLevelName(lvl, levelName)
[2]846
[391]847 Associates level *lvl* with text *levelName* in an internal dictionary, which is
848 used to map numeric levels to a textual representation, for example when a
849 :class:`Formatter` formats a message. This function can also be used to define
850 your own levels. The only constraints are that all levels used must be
851 registered using this function, levels should be positive integers and they
852 should increase in increasing order of severity.
[2]853
[391]854 .. note:: If you are thinking of defining your own levels, please see the
855 section on :ref:`custom-levels`.
[2]856
[391]857.. function:: getLevelName(lvl)
[2]858
[391]859 Returns the textual representation of logging level *lvl*. If the level is one
860 of the predefined levels :const:`CRITICAL`, :const:`ERROR`, :const:`WARNING`,
861 :const:`INFO` or :const:`DEBUG` then you get the corresponding string. If you
862 have associated levels with names using :func:`addLevelName` then the name you
863 have associated with *lvl* is returned. If a numeric value corresponding to one
864 of the defined levels is passed in, the corresponding string representation is
865 returned. Otherwise, the string "Level %s" % lvl is returned.
[2]866
867
[391]868.. function:: makeLogRecord(attrdict)
[2]869
[391]870 Creates and returns a new :class:`LogRecord` instance whose attributes are
871 defined by *attrdict*. This function is useful for taking a pickled
872 :class:`LogRecord` attribute dictionary, sent over a socket, and reconstituting
873 it as a :class:`LogRecord` instance at the receiving end.
[2]874
875
[391]876.. function:: basicConfig([**kwargs])
[2]877
[391]878 Does basic configuration for the logging system by creating a
879 :class:`StreamHandler` with a default :class:`Formatter` and adding it to the
880 root logger. The functions :func:`debug`, :func:`info`, :func:`warning`,
881 :func:`error` and :func:`critical` will call :func:`basicConfig` automatically
882 if no handlers are defined for the root logger.
[2]883
[391]884 This function does nothing if the root logger already has handlers
885 configured for it.
[2]886
[391]887 .. versionchanged:: 2.4
888 Formerly, :func:`basicConfig` did not take any keyword arguments.
[2]889
[391]890 .. note:: This function should be called from the main thread before other
891 threads are started. In versions of Python prior to 2.7.1 and 3.2, if
892 this function is called from multiple threads, it is possible (in rare
893 circumstances) that a handler will be added to the root logger more than
894 once, leading to unexpected results such as messages being duplicated in
895 the log.
[2]896
[391]897 The following keyword arguments are supported.
[2]898
[391]899 .. tabularcolumns:: |l|L|
[2]900
[391]901 +--------------+---------------------------------------------+
902 | Format | Description |
903 +==============+=============================================+
904 | ``filename`` | Specifies that a FileHandler be created, |
905 | | using the specified filename, rather than a |
906 | | StreamHandler. |
907 +--------------+---------------------------------------------+
908 | ``filemode`` | Specifies the mode to open the file, if |
909 | | filename is specified (if filemode is |
910 | | unspecified, it defaults to 'a'). |
911 +--------------+---------------------------------------------+
912 | ``format`` | Use the specified format string for the |
913 | | handler. |
914 +--------------+---------------------------------------------+
915 | ``datefmt`` | Use the specified date/time format. |
916 +--------------+---------------------------------------------+
917 | ``level`` | Set the root logger level to the specified |
918 | | level. |
919 +--------------+---------------------------------------------+
920 | ``stream`` | Use the specified stream to initialize the |
921 | | StreamHandler. Note that this argument is |
922 | | incompatible with 'filename' - if both are |
923 | | present, 'stream' is ignored. |
924 +--------------+---------------------------------------------+
[2]925
926
[391]927.. function:: shutdown()
[2]928
[391]929 Informs the logging system to perform an orderly shutdown by flushing and
930 closing all handlers. This should be called at application exit and no
931 further use of the logging system should be made after this call.
[2]932
933
[391]934.. function:: setLoggerClass(klass)
[2]935
[391]936 Tells the logging system to use the class *klass* when instantiating a logger.
937 The class should define :meth:`__init__` such that only a name argument is
938 required, and the :meth:`__init__` should call :meth:`Logger.__init__`. This
939 function is typically called before any loggers are instantiated by applications
940 which need to use custom logger behavior.
[2]941
942
[391]943Integration with the warnings module
944------------------------------------
[2]945
[391]946The :func:`captureWarnings` function can be used to integrate :mod:`logging`
947with the :mod:`warnings` module.
[2]948
[391]949.. function:: captureWarnings(capture)
[2]950
[391]951 This function is used to turn the capture of warnings by logging on and
952 off.
[2]953
[391]954 If *capture* is ``True``, warnings issued by the :mod:`warnings` module will
955 be redirected to the logging system. Specifically, a warning will be
956 formatted using :func:`warnings.formatwarning` and the resulting string
957 logged to a logger named ``'py.warnings'`` with a severity of :const:`WARNING`.
[2]958
[391]959 If *capture* is ``False``, the redirection of warnings to the logging system
960 will stop, and warnings will be redirected to their original destinations
961 (i.e. those in effect before ``captureWarnings(True)`` was called).
[2]962
963
[391]964.. seealso::
[2]965
[391]966 Module :mod:`logging.config`
967 Configuration API for the logging module.
[2]968
[391]969 Module :mod:`logging.handlers`
970 Useful handlers included with the logging module.
[2]971
[391]972 :pep:`282` - A Logging System
973 The proposal which described this feature for inclusion in the Python standard
974 library.
[2]975
[391]976 `Original Python logging package <http://www.red-dove.com/python_logging.html>`_
977 This is the original source for the :mod:`logging` package. The version of the
978 package available from this site is suitable for use with Python 1.5.2, 2.1.x
979 and 2.2.x, which do not include the :mod:`logging` package in the standard
980 library.
[2]981
Note: See TracBrowser for help on using the repository browser.