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