blob: a7928a0647cc5b9b0ecc8ad2549bb752655fcb2d [file] [log] [blame]
Vinay Sajipc63619b2010-12-19 12:56:57 +00001:mod:`logging.config` --- Logging configuration
2===============================================
3
4.. module:: logging.config
5 :synopsis: Configuration of the logging module.
6
Vinay Sajipc63619b2010-12-19 12:56:57 +00007.. moduleauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
8.. sectionauthor:: Vinay Sajip <vinay_sajip@red-dove.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/logging/config.py`
11
Vinay Sajip01094e12010-12-19 13:41:26 +000012.. sidebar:: Important
13
14 This page contains only reference information. For tutorials,
15 please see
16
17 * :ref:`Basic Tutorial <logging-basic-tutorial>`
18 * :ref:`Advanced Tutorial <logging-advanced-tutorial>`
19 * :ref:`Logging Cookbook <logging-cookbook>`
20
Vinay Sajip31b862d2013-09-05 23:01:07 +010021--------------
22
Vinay Sajip01094e12010-12-19 13:41:26 +000023This section describes the API for configuring the logging module.
Vinay Sajipc63619b2010-12-19 12:56:57 +000024
25.. _logging-config-api:
26
27Configuration functions
28^^^^^^^^^^^^^^^^^^^^^^^
29
30The following functions configure the logging module. They are located in the
31:mod:`logging.config` module. Their use is optional --- you can configure the
32logging module using these functions or by making calls to the main API (defined
33in :mod:`logging` itself) and defining handlers which are declared either in
34:mod:`logging` or :mod:`logging.handlers`.
35
36.. function:: dictConfig(config)
37
38 Takes the logging configuration from a dictionary. The contents of
39 this dictionary are described in :ref:`logging-config-dictschema`
40 below.
41
42 If an error is encountered during configuration, this function will
43 raise a :exc:`ValueError`, :exc:`TypeError`, :exc:`AttributeError`
44 or :exc:`ImportError` with a suitably descriptive message. The
45 following is a (possibly incomplete) list of conditions which will
46 raise an error:
47
48 * A ``level`` which is not a string or which is a string not
49 corresponding to an actual logging level.
50 * A ``propagate`` value which is not a boolean.
51 * An id which does not have a corresponding destination.
52 * A non-existent handler id found during an incremental call.
53 * An invalid logger name.
54 * Inability to resolve to an internal or external object.
55
56 Parsing is performed by the :class:`DictConfigurator` class, whose
57 constructor is passed the dictionary used for configuration, and
58 has a :meth:`configure` method. The :mod:`logging.config` module
59 has a callable attribute :attr:`dictConfigClass`
60 which is initially set to :class:`DictConfigurator`.
61 You can replace the value of :attr:`dictConfigClass` with a
62 suitable implementation of your own.
63
64 :func:`dictConfig` calls :attr:`dictConfigClass` passing
65 the specified dictionary, and then calls the :meth:`configure` method on
66 the returned object to put the configuration into effect::
67
68 def dictConfig(config):
69 dictConfigClass(config).configure()
70
71 For example, a subclass of :class:`DictConfigurator` could call
72 ``DictConfigurator.__init__()`` in its own :meth:`__init__()`, then
73 set up custom prefixes which would be usable in the subsequent
74 :meth:`configure` call. :attr:`dictConfigClass` would be bound to
75 this new subclass, and then :func:`dictConfig` could be called exactly as
76 in the default, uncustomized state.
77
Vinay Sajip1a90f9c2011-01-27 19:14:16 +000078 .. versionadded:: 3.2
79
Vinay Sajipf97255f2011-04-19 13:56:39 +010080.. function:: fileConfig(fname, defaults=None, disable_existing_loggers=True)
Vinay Sajipc63619b2010-12-19 12:56:57 +000081
Vinay Sajipd8b24d72014-02-04 16:42:04 +000082 Reads the logging configuration from a :mod:`configparser`\-format file. The
83 format of the file should be as described in
84 :ref:`logging-config-fileformat`.
Vinay Sajipcf9e2f22012-10-09 09:06:03 +010085 This function can be called several times from an application, allowing an
86 end user to select from various pre-canned configurations (if the developer
87 provides a mechanism to present the choices and load the chosen
88 configuration).
89
90 :param fname: A filename, or a file-like object, or an instance derived
91 from :class:`~configparser.RawConfigParser`. If a
92 ``RawConfigParser``-derived instance is passed, it is used as
93 is. Otherwise, a :class:`~configparser.Configparser` is
94 instantiated, and the configuration read by it from the
95 object passed in ``fname``. If that has a :meth:`readline`
96 method, it is assumed to be a file-like object and read using
97 :meth:`~configparser.ConfigParser.read_file`; otherwise,
98 it is assumed to be a filename and passed to
99 :meth:`~configparser.ConfigParser.read`.
100
Vinay Sajipf97255f2011-04-19 13:56:39 +0100101
102 :param defaults: Defaults to be passed to the ConfigParser can be specified
103 in this argument.
104
105 :param disable_existing_loggers: If specified as ``False``, loggers which
106 exist when this call is made are left
Vinay Sajip12d9ddf2014-08-05 10:34:16 +0100107 enabled. The default is ``True`` because this
Martin Panter8f137832017-01-14 08:24:20 +0000108 enables old behaviour in a
109 backward-compatible way. This behaviour is to
Vinay Sajipf97255f2011-04-19 13:56:39 +0100110 disable any existing loggers unless they or
111 their ancestors are explicitly named in the
112 logging configuration.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000113
Vinay Sajipcf9e2f22012-10-09 09:06:03 +0100114 .. versionchanged:: 3.4
115 An instance of a subclass of :class:`~configparser.RawConfigParser` is
116 now accepted as a value for ``fname``. This facilitates:
117
118 * Use of a configuration file where logging configuration is just part
119 of the overall application configuration.
120 * Use of a configuration read from a file, and then modified by the using
121 application (e.g. based on command-line parameters or other aspects
122 of the runtime environment) before being passed to ``fileConfig``.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000123
Vinay Sajip4ded5512012-10-02 15:56:16 +0100124.. function:: listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)
Vinay Sajipc63619b2010-12-19 12:56:57 +0000125
126 Starts up a socket server on the specified port, and listens for new
127 configurations. If no port is specified, the module's default
128 :const:`DEFAULT_LOGGING_CONFIG_PORT` is used. Logging configurations will be
Vinay Sajip3d639b22016-09-11 13:15:06 +0100129 sent as a file suitable for processing by :func:`dictConfig` or
130 :func:`fileConfig`. Returns a :class:`~threading.Thread` instance on which
131 you can call :meth:`~threading.Thread.start` to start the server, and which
132 you can :meth:`~threading.Thread.join` when appropriate. To stop the server,
Vinay Sajipc63619b2010-12-19 12:56:57 +0000133 call :func:`stopListening`.
134
Vinay Sajip4ded5512012-10-02 15:56:16 +0100135 The ``verify`` argument, if specified, should be a callable which should
136 verify whether bytes received across the socket are valid and should be
137 processed. This could be done by encrypting and/or signing what is sent
138 across the socket, such that the ``verify`` callable can perform
139 signature verification and/or decryption. The ``verify`` callable is called
140 with a single argument - the bytes received across the socket - and should
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300141 return the bytes to be processed, or ``None`` to indicate that the bytes should
Vinay Sajip4ded5512012-10-02 15:56:16 +0100142 be discarded. The returned bytes could be the same as the passed in bytes
143 (e.g. when only verification is done), or they could be completely different
144 (perhaps if decryption were performed).
145
Vinay Sajipc63619b2010-12-19 12:56:57 +0000146 To send a configuration to the socket, read in the configuration file and
R David Murray48de2822016-08-23 20:43:56 -0400147 send it to the socket as a sequence of bytes preceded by a four-byte length
Vinay Sajipc63619b2010-12-19 12:56:57 +0000148 string packed in binary using ``struct.pack('>L', n)``.
149
Larry Hastings3732ed22014-03-15 21:13:56 -0700150 .. note::
151
152 Because portions of the configuration are passed through
Vinay Sajip393e23f2012-07-25 19:19:25 +0100153 :func:`eval`, use of this function may open its users to a security risk.
154 While the function only binds to a socket on ``localhost``, and so does
155 not accept connections from remote machines, there are scenarios where
156 untrusted code could be run under the account of the process which calls
157 :func:`listen`. Specifically, if the process calling :func:`listen` runs
158 on a multi-user machine where users cannot trust each other, then a
159 malicious user could arrange to run essentially arbitrary code in a
160 victim user's process, simply by connecting to the victim's
161 :func:`listen` socket and sending a configuration which runs whatever
162 code the attacker wants to have executed in the victim's process. This is
163 especially easy to do if the default port is used, but not hard even if a
Vinay Sajip4ded5512012-10-02 15:56:16 +0100164 different port is used). To avoid the risk of this happening, use the
165 ``verify`` argument to :func:`listen` to prevent unrecognised
166 configurations from being applied.
167
Vinay Sajip3d639b22016-09-11 13:15:06 +0100168 .. versionchanged:: 3.4
Vinay Sajip4ded5512012-10-02 15:56:16 +0100169 The ``verify`` argument was added.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000170
Vinay Sajip3d639b22016-09-11 13:15:06 +0100171 .. note::
172
173 If you want to send configurations to the listener which don't
174 disable existing loggers, you will need to use a JSON format for
175 the configuration, which will use :func:`dictConfig` for configuration.
176 This method allows you to specify ``disable_existing_loggers`` as
177 ``False`` in the configuration you send.
178
179
Vinay Sajipc63619b2010-12-19 12:56:57 +0000180.. function:: stopListening()
181
182 Stops the listening server which was created with a call to :func:`listen`.
183 This is typically called before calling :meth:`join` on the return value from
184 :func:`listen`.
185
186
187.. _logging-config-dictschema:
188
189Configuration dictionary schema
190^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191
192Describing a logging configuration requires listing the various
193objects to create and the connections between them; for example, you
194may create a handler named 'console' and then say that the logger
195named 'startup' will send its messages to the 'console' handler.
196These objects aren't limited to those provided by the :mod:`logging`
197module because you might write your own formatter or handler class.
198The parameters to these classes may also need to include external
199objects such as ``sys.stderr``. The syntax for describing these
200objects and connections is defined in :ref:`logging-config-dict-connections`
201below.
202
203Dictionary Schema Details
204"""""""""""""""""""""""""
205
206The dictionary passed to :func:`dictConfig` must contain the following
207keys:
208
209* *version* - to be set to an integer value representing the schema
210 version. The only valid value at present is 1, but having this key
211 allows the schema to evolve while still preserving backwards
212 compatibility.
213
214All other keys are optional, but if present they will be interpreted
215as described below. In all cases below where a 'configuring dict' is
216mentioned, it will be checked for the special ``'()'`` key to see if a
217custom instantiation is required. If so, the mechanism described in
218:ref:`logging-config-dict-userdef` below is used to create an instance;
219otherwise, the context is used to determine what to instantiate.
220
221* *formatters* - the corresponding value will be a dict in which each
222 key is a formatter id and each value is a dict describing how to
Vinay Sajip67f39772013-08-17 00:39:42 +0100223 configure the corresponding :class:`~logging.Formatter` instance.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000224
225 The configuring dict is searched for keys ``format`` and ``datefmt``
226 (with defaults of ``None``) and these are used to construct a
Vinay Sajip67f39772013-08-17 00:39:42 +0100227 :class:`~logging.Formatter` instance.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000228
229* *filters* - the corresponding value will be a dict in which each key
230 is a filter id and each value is a dict describing how to configure
231 the corresponding Filter instance.
232
233 The configuring dict is searched for the key ``name`` (defaulting to the
234 empty string) and this is used to construct a :class:`logging.Filter`
235 instance.
236
237* *handlers* - the corresponding value will be a dict in which each
238 key is a handler id and each value is a dict describing how to
239 configure the corresponding Handler instance.
240
241 The configuring dict is searched for the following keys:
242
243 * ``class`` (mandatory). This is the fully qualified name of the
244 handler class.
245
246 * ``level`` (optional). The level of the handler.
247
248 * ``formatter`` (optional). The id of the formatter for this
249 handler.
250
251 * ``filters`` (optional). A list of ids of the filters for this
252 handler.
253
254 All *other* keys are passed through as keyword arguments to the
Martin Panter1050d2d2016-07-26 11:18:21 +0200255 handler's constructor. For example, given the snippet:
256
257 .. code-block:: yaml
Vinay Sajipc63619b2010-12-19 12:56:57 +0000258
259 handlers:
260 console:
261 class : logging.StreamHandler
262 formatter: brief
263 level : INFO
264 filters: [allow_foo]
265 stream : ext://sys.stdout
266 file:
267 class : logging.handlers.RotatingFileHandler
268 formatter: precise
269 filename: logconfig.log
270 maxBytes: 1024
271 backupCount: 3
272
273 the handler with id ``console`` is instantiated as a
274 :class:`logging.StreamHandler`, using ``sys.stdout`` as the underlying
275 stream. The handler with id ``file`` is instantiated as a
276 :class:`logging.handlers.RotatingFileHandler` with the keyword arguments
277 ``filename='logconfig.log', maxBytes=1024, backupCount=3``.
278
279* *loggers* - the corresponding value will be a dict in which each key
280 is a logger name and each value is a dict describing how to
281 configure the corresponding Logger instance.
282
283 The configuring dict is searched for the following keys:
284
285 * ``level`` (optional). The level of the logger.
286
287 * ``propagate`` (optional). The propagation setting of the logger.
288
289 * ``filters`` (optional). A list of ids of the filters for this
290 logger.
291
292 * ``handlers`` (optional). A list of ids of the handlers for this
293 logger.
294
295 The specified loggers will be configured according to the level,
296 propagation, filters and handlers specified.
297
298* *root* - this will be the configuration for the root logger.
299 Processing of the configuration will be as for any logger, except
300 that the ``propagate`` setting will not be applicable.
301
302* *incremental* - whether the configuration is to be interpreted as
303 incremental to the existing configuration. This value defaults to
304 ``False``, which means that the specified configuration replaces the
305 existing configuration with the same semantics as used by the
306 existing :func:`fileConfig` API.
307
308 If the specified value is ``True``, the configuration is processed
309 as described in the section on :ref:`logging-config-dict-incremental`.
310
311* *disable_existing_loggers* - whether any existing loggers are to be
312 disabled. This setting mirrors the parameter of the same name in
313 :func:`fileConfig`. If absent, this parameter defaults to ``True``.
314 This value is ignored if *incremental* is ``True``.
315
316.. _logging-config-dict-incremental:
317
318Incremental Configuration
319"""""""""""""""""""""""""
320
321It is difficult to provide complete flexibility for incremental
322configuration. For example, because objects such as filters
323and formatters are anonymous, once a configuration is set up, it is
324not possible to refer to such anonymous objects when augmenting a
325configuration.
326
327Furthermore, there is not a compelling case for arbitrarily altering
328the object graph of loggers, handlers, filters, formatters at
329run-time, once a configuration is set up; the verbosity of loggers and
330handlers can be controlled just by setting levels (and, in the case of
331loggers, propagation flags). Changing the object graph arbitrarily in
332a safe way is problematic in a multi-threaded environment; while not
333impossible, the benefits are not worth the complexity it adds to the
334implementation.
335
336Thus, when the ``incremental`` key of a configuration dict is present
337and is ``True``, the system will completely ignore any ``formatters`` and
338``filters`` entries, and process only the ``level``
339settings in the ``handlers`` entries, and the ``level`` and
340``propagate`` settings in the ``loggers`` and ``root`` entries.
341
342Using a value in the configuration dict lets configurations to be sent
343over the wire as pickled dicts to a socket listener. Thus, the logging
344verbosity of a long-running application can be altered over time with
345no need to stop and restart the application.
346
347.. _logging-config-dict-connections:
348
349Object connections
350""""""""""""""""""
351
352The schema describes a set of logging objects - loggers,
353handlers, formatters, filters - which are connected to each other in
354an object graph. Thus, the schema needs to represent connections
355between the objects. For example, say that, once configured, a
356particular logger has attached to it a particular handler. For the
357purposes of this discussion, we can say that the logger represents the
358source, and the handler the destination, of a connection between the
359two. Of course in the configured objects this is represented by the
360logger holding a reference to the handler. In the configuration dict,
361this is done by giving each destination object an id which identifies
362it unambiguously, and then using the id in the source object's
363configuration to indicate that a connection exists between the source
364and the destination object with that id.
365
Martin Panter1050d2d2016-07-26 11:18:21 +0200366So, for example, consider the following YAML snippet:
367
368.. code-block:: yaml
Vinay Sajipc63619b2010-12-19 12:56:57 +0000369
370 formatters:
371 brief:
372 # configuration for formatter with id 'brief' goes here
373 precise:
374 # configuration for formatter with id 'precise' goes here
375 handlers:
376 h1: #This is an id
377 # configuration of handler with id 'h1' goes here
378 formatter: brief
379 h2: #This is another id
380 # configuration of handler with id 'h2' goes here
381 formatter: precise
382 loggers:
383 foo.bar.baz:
384 # other configuration for logger 'foo.bar.baz'
385 handlers: [h1, h2]
386
387(Note: YAML used here because it's a little more readable than the
388equivalent Python source form for the dictionary.)
389
390The ids for loggers are the logger names which would be used
391programmatically to obtain a reference to those loggers, e.g.
392``foo.bar.baz``. The ids for Formatters and Filters can be any string
393value (such as ``brief``, ``precise`` above) and they are transient,
394in that they are only meaningful for processing the configuration
395dictionary and used to determine connections between objects, and are
396not persisted anywhere when the configuration call is complete.
397
398The above snippet indicates that logger named ``foo.bar.baz`` should
399have two handlers attached to it, which are described by the handler
400ids ``h1`` and ``h2``. The formatter for ``h1`` is that described by id
401``brief``, and the formatter for ``h2`` is that described by id
402``precise``.
403
404
405.. _logging-config-dict-userdef:
406
407User-defined objects
408""""""""""""""""""""
409
410The schema supports user-defined objects for handlers, filters and
411formatters. (Loggers do not need to have different types for
412different instances, so there is no support in this configuration
413schema for user-defined logger classes.)
414
415Objects to be configured are described by dictionaries
416which detail their configuration. In some places, the logging system
417will be able to infer from the context how an object is to be
418instantiated, but when a user-defined object is to be instantiated,
419the system will not know how to do this. In order to provide complete
420flexibility for user-defined object instantiation, the user needs
421to provide a 'factory' - a callable which is called with a
422configuration dictionary and which returns the instantiated object.
423This is signalled by an absolute import path to the factory being
424made available under the special key ``'()'``. Here's a concrete
Martin Panter1050d2d2016-07-26 11:18:21 +0200425example:
426
427.. code-block:: yaml
Vinay Sajipc63619b2010-12-19 12:56:57 +0000428
429 formatters:
430 brief:
431 format: '%(message)s'
432 default:
433 format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
434 datefmt: '%Y-%m-%d %H:%M:%S'
435 custom:
436 (): my.package.customFormatterFactory
437 bar: baz
438 spam: 99.9
439 answer: 42
440
441The above YAML snippet defines three formatters. The first, with id
442``brief``, is a standard :class:`logging.Formatter` instance with the
443specified format string. The second, with id ``default``, has a
444longer format and also defines the time format explicitly, and will
445result in a :class:`logging.Formatter` initialized with those two format
446strings. Shown in Python source form, the ``brief`` and ``default``
447formatters have configuration sub-dictionaries::
448
449 {
450 'format' : '%(message)s'
451 }
452
453and::
454
455 {
456 'format' : '%(asctime)s %(levelname)-8s %(name)-15s %(message)s',
457 'datefmt' : '%Y-%m-%d %H:%M:%S'
458 }
459
460respectively, and as these dictionaries do not contain the special key
461``'()'``, the instantiation is inferred from the context: as a result,
462standard :class:`logging.Formatter` instances are created. The
463configuration sub-dictionary for the third formatter, with id
464``custom``, is::
465
466 {
467 '()' : 'my.package.customFormatterFactory',
468 'bar' : 'baz',
469 'spam' : 99.9,
470 'answer' : 42
471 }
472
473and this contains the special key ``'()'``, which means that
474user-defined instantiation is wanted. In this case, the specified
475factory callable will be used. If it is an actual callable it will be
476used directly - otherwise, if you specify a string (as in the example)
477the actual callable will be located using normal import mechanisms.
478The callable will be called with the **remaining** items in the
479configuration sub-dictionary as keyword arguments. In the above
480example, the formatter with id ``custom`` will be assumed to be
481returned by the call::
482
483 my.package.customFormatterFactory(bar='baz', spam=99.9, answer=42)
484
485The key ``'()'`` has been used as the special key because it is not a
486valid keyword parameter name, and so will not clash with the names of
487the keyword arguments used in the call. The ``'()'`` also serves as a
488mnemonic that the corresponding value is a callable.
489
490
491.. _logging-config-dict-externalobj:
492
493Access to external objects
494""""""""""""""""""""""""""
495
496There are times where a configuration needs to refer to objects
497external to the configuration, for example ``sys.stderr``. If the
498configuration dict is constructed using Python code, this is
499straightforward, but a problem arises when the configuration is
500provided via a text file (e.g. JSON, YAML). In a text file, there is
501no standard way to distinguish ``sys.stderr`` from the literal string
502``'sys.stderr'``. To facilitate this distinction, the configuration
503system looks for certain special prefixes in string values and
504treat them specially. For example, if the literal string
505``'ext://sys.stderr'`` is provided as a value in the configuration,
506then the ``ext://`` will be stripped off and the remainder of the
507value processed using normal import mechanisms.
508
509The handling of such prefixes is done in a way analogous to protocol
510handling: there is a generic mechanism to look for prefixes which
511match the regular expression ``^(?P<prefix>[a-z]+)://(?P<suffix>.*)$``
512whereby, if the ``prefix`` is recognised, the ``suffix`` is processed
513in a prefix-dependent manner and the result of the processing replaces
514the string value. If the prefix is not recognised, then the string
515value will be left as-is.
516
517
518.. _logging-config-dict-internalobj:
519
520Access to internal objects
521""""""""""""""""""""""""""
522
523As well as external objects, there is sometimes also a need to refer
524to objects in the configuration. This will be done implicitly by the
525configuration system for things that it knows about. For example, the
526string value ``'DEBUG'`` for a ``level`` in a logger or handler will
527automatically be converted to the value ``logging.DEBUG``, and the
528``handlers``, ``filters`` and ``formatter`` entries will take an
529object id and resolve to the appropriate destination object.
530
531However, a more generic mechanism is needed for user-defined
532objects which are not known to the :mod:`logging` module. For
533example, consider :class:`logging.handlers.MemoryHandler`, which takes
534a ``target`` argument which is another handler to delegate to. Since
535the system already knows about this class, then in the configuration,
536the given ``target`` just needs to be the object id of the relevant
537target handler, and the system will resolve to the handler from the
538id. If, however, a user defines a ``my.package.MyHandler`` which has
539an ``alternate`` handler, the configuration system would not know that
540the ``alternate`` referred to a handler. To cater for this, a generic
541resolution system allows the user to specify::
542
543 handlers:
544 file:
545 # configuration of file handler goes here
546
547 custom:
548 (): my.package.MyHandler
549 alternate: cfg://handlers.file
550
551The literal string ``'cfg://handlers.file'`` will be resolved in an
552analogous way to strings with the ``ext://`` prefix, but looking
553in the configuration itself rather than the import namespace. The
554mechanism allows access by dot or by index, in a similar way to
555that provided by ``str.format``. Thus, given the following snippet::
556
557 handlers:
558 email:
559 class: logging.handlers.SMTPHandler
560 mailhost: localhost
561 fromaddr: my_app@domain.tld
562 toaddrs:
563 - support_team@domain.tld
564 - dev_team@domain.tld
565 subject: Houston, we have a problem.
566
567in the configuration, the string ``'cfg://handlers'`` would resolve to
568the dict with key ``handlers``, the string ``'cfg://handlers.email``
569would resolve to the dict with key ``email`` in the ``handlers`` dict,
570and so on. The string ``'cfg://handlers.email.toaddrs[1]`` would
571resolve to ``'dev_team.domain.tld'`` and the string
572``'cfg://handlers.email.toaddrs[0]'`` would resolve to the value
573``'support_team@domain.tld'``. The ``subject`` value could be accessed
574using either ``'cfg://handlers.email.subject'`` or, equivalently,
575``'cfg://handlers.email[subject]'``. The latter form only needs to be
576used if the key contains spaces or non-alphanumeric characters. If an
577index value consists only of decimal digits, access will be attempted
578using the corresponding integer value, falling back to the string
579value if needed.
580
581Given a string ``cfg://handlers.myhandler.mykey.123``, this will
582resolve to ``config_dict['handlers']['myhandler']['mykey']['123']``.
583If the string is specified as ``cfg://handlers.myhandler.mykey[123]``,
584the system will attempt to retrieve the value from
585``config_dict['handlers']['myhandler']['mykey'][123]``, and fall back
586to ``config_dict['handlers']['myhandler']['mykey']['123']`` if that
587fails.
588
Vinay Sajip817495a2011-08-11 13:45:48 +0100589
590.. _logging-import-resolution:
591
592Import resolution and custom importers
593""""""""""""""""""""""""""""""""""""""
594
595Import resolution, by default, uses the builtin :func:`__import__` function
596to do its importing. You may want to replace this with your own importing
597mechanism: if so, you can replace the :attr:`importer` attribute of the
598:class:`DictConfigurator` or its superclass, the
599:class:`BaseConfigurator` class. However, you need to be
600careful because of the way functions are accessed from classes via
601descriptors. If you are using a Python callable to do your imports, and you
602want to define it at class level rather than instance level, you need to wrap
603it with :func:`staticmethod`. For example::
604
605 from importlib import import_module
606 from logging.config import BaseConfigurator
607
608 BaseConfigurator.importer = staticmethod(import_module)
609
610You don't need to wrap with :func:`staticmethod` if you're setting the import
611callable on a configurator *instance*.
612
613
Vinay Sajipc63619b2010-12-19 12:56:57 +0000614.. _logging-config-fileformat:
615
616Configuration file format
617^^^^^^^^^^^^^^^^^^^^^^^^^
618
619The configuration file format understood by :func:`fileConfig` is based on
620:mod:`configparser` functionality. The file must contain sections called
621``[loggers]``, ``[handlers]`` and ``[formatters]`` which identify by name the
622entities of each type which are defined in the file. For each such entity, there
623is a separate section which identifies how that entity is configured. Thus, for
624a logger named ``log01`` in the ``[loggers]`` section, the relevant
625configuration details are held in a section ``[logger_log01]``. Similarly, a
626handler called ``hand01`` in the ``[handlers]`` section will have its
627configuration held in a section called ``[handler_hand01]``, while a formatter
628called ``form01`` in the ``[formatters]`` section will have its configuration
629specified in a section called ``[formatter_form01]``. The root logger
630configuration must be specified in a section called ``[logger_root]``.
631
Vinay Sajip2d965c22015-04-18 13:14:10 +0100632.. note::
633
634 The :func:`fileConfig` API is older than the :func:`dictConfig` API and does
635 not provide functionality to cover certain aspects of logging. For example,
636 you cannot configure :class:`~logging.Filter` objects, which provide for
637 filtering of messages beyond simple integer levels, using :func:`fileConfig`.
638 If you need to have instances of :class:`~logging.Filter` in your logging
639 configuration, you will need to use :func:`dictConfig`. Note that future
640 enhancements to configuration functionality will be added to
641 :func:`dictConfig`, so it's worth considering transitioning to this newer
642 API when it's convenient to do so.
643
Martin Panter1050d2d2016-07-26 11:18:21 +0200644Examples of these sections in the file are given below.
645
646.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000647
648 [loggers]
649 keys=root,log02,log03,log04,log05,log06,log07
650
651 [handlers]
652 keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
653
654 [formatters]
655 keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
656
657The root logger must specify a level and a list of handlers. An example of a
Martin Panter1050d2d2016-07-26 11:18:21 +0200658root logger section is given below.
659
660.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000661
662 [logger_root]
663 level=NOTSET
664 handlers=hand01
665
666The ``level`` entry can be one of ``DEBUG, INFO, WARNING, ERROR, CRITICAL`` or
667``NOTSET``. For the root logger only, ``NOTSET`` means that all messages will be
668logged. Level values are :func:`eval`\ uated in the context of the ``logging``
669package's namespace.
670
671The ``handlers`` entry is a comma-separated list of handler names, which must
672appear in the ``[handlers]`` section. These names must appear in the
673``[handlers]`` section and have corresponding sections in the configuration
674file.
675
676For loggers other than the root logger, some additional information is required.
Martin Panter1050d2d2016-07-26 11:18:21 +0200677This is illustrated by the following example.
678
679.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000680
681 [logger_parser]
682 level=DEBUG
683 handlers=hand01
684 propagate=1
685 qualname=compiler.parser
686
687The ``level`` and ``handlers`` entries are interpreted as for the root logger,
688except that if a non-root logger's level is specified as ``NOTSET``, the system
689consults loggers higher up the hierarchy to determine the effective level of the
690logger. The ``propagate`` entry is set to 1 to indicate that messages must
691propagate to handlers higher up the logger hierarchy from this logger, or 0 to
692indicate that messages are **not** propagated to handlers up the hierarchy. The
693``qualname`` entry is the hierarchical channel name of the logger, that is to
694say the name used by the application to get the logger.
695
696Sections which specify handler configuration are exemplified by the following.
Martin Panter1050d2d2016-07-26 11:18:21 +0200697
698.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000699
700 [handler_hand01]
701 class=StreamHandler
702 level=NOTSET
703 formatter=form01
704 args=(sys.stdout,)
705
706The ``class`` entry indicates the handler's class (as determined by :func:`eval`
707in the ``logging`` package's namespace). The ``level`` is interpreted as for
708loggers, and ``NOTSET`` is taken to mean 'log everything'.
709
710The ``formatter`` entry indicates the key name of the formatter for this
711handler. If blank, a default formatter (``logging._defaultFormatter``) is used.
712If a name is specified, it must appear in the ``[formatters]`` section and have
713a corresponding section in the configuration file.
714
715The ``args`` entry, when :func:`eval`\ uated in the context of the ``logging``
716package's namespace, is the list of arguments to the constructor for the handler
717class. Refer to the constructors for the relevant handlers, or to the examples
Martin Panter1050d2d2016-07-26 11:18:21 +0200718below, to see how typical entries are constructed.
719
720.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000721
722 [handler_hand02]
723 class=FileHandler
724 level=DEBUG
725 formatter=form02
726 args=('python.log', 'w')
727
728 [handler_hand03]
729 class=handlers.SocketHandler
730 level=INFO
731 formatter=form03
732 args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
733
734 [handler_hand04]
735 class=handlers.DatagramHandler
736 level=WARN
737 formatter=form04
738 args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)
739
740 [handler_hand05]
741 class=handlers.SysLogHandler
742 level=ERROR
743 formatter=form05
744 args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
745
746 [handler_hand06]
747 class=handlers.NTEventLogHandler
748 level=CRITICAL
749 formatter=form06
750 args=('Python Application', '', 'Application')
751
752 [handler_hand07]
753 class=handlers.SMTPHandler
754 level=WARN
755 formatter=form07
756 args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
757
758 [handler_hand08]
759 class=handlers.MemoryHandler
760 level=NOTSET
761 formatter=form08
762 target=
763 args=(10, ERROR)
764
765 [handler_hand09]
766 class=handlers.HTTPHandler
767 level=NOTSET
768 formatter=form09
769 args=('localhost:9022', '/log', 'GET')
770
Martin Panter1050d2d2016-07-26 11:18:21 +0200771Sections which specify formatter configuration are typified by the following.
772
773.. code-block:: ini
Vinay Sajipc63619b2010-12-19 12:56:57 +0000774
775 [formatter_form01]
776 format=F1 %(asctime)s %(levelname)s %(message)s
777 datefmt=
778 class=logging.Formatter
779
780The ``format`` entry is the overall format string, and the ``datefmt`` entry is
781the :func:`strftime`\ -compatible date/time format string. If empty, the
782package substitutes ISO8601 format date/times, which is almost equivalent to
783specifying the date format string ``'%Y-%m-%d %H:%M:%S'``. The ISO8601 format
784also specifies milliseconds, which are appended to the result of using the above
785format string, with a comma separator. An example time in ISO8601 format is
786``2003-01-23 00:29:50,411``.
787
788The ``class`` entry is optional. It indicates the name of the formatter's class
789(as a dotted module and class name.) This option is useful for instantiating a
Vinay Sajip67f39772013-08-17 00:39:42 +0100790:class:`~logging.Formatter` subclass. Subclasses of
791:class:`~logging.Formatter` can present exception tracebacks in an expanded or
792condensed format.
Vinay Sajipc63619b2010-12-19 12:56:57 +0000793
Larry Hastings3732ed22014-03-15 21:13:56 -0700794.. note::
795
796 Due to the use of :func:`eval` as described above, there are
Vinay Sajip393e23f2012-07-25 19:19:25 +0100797 potential security risks which result from using the :func:`listen` to send
798 and receive configurations via sockets. The risks are limited to where
799 multiple users with no mutual trust run code on the same machine; see the
800 :func:`listen` documentation for more information.
801
Vinay Sajipc63619b2010-12-19 12:56:57 +0000802.. seealso::
803
804 Module :mod:`logging`
805 API reference for the logging module.
806
807 Module :mod:`logging.handlers`
808 Useful handlers included with the logging module.