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