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