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