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