Vinay Sajip | 5dbca9c | 2011-04-08 11:40:38 +0100 | [diff] [blame] | 1 | :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 | |
| 11 | .. 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 | |
| 20 | This section describes the API for configuring the logging module. |
| 21 | |
| 22 | .. _logging-config-api: |
| 23 | |
| 24 | Configuration functions |
| 25 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 26 | |
| 27 | The following functions configure the logging module. They are located in the |
| 28 | :mod:`logging.config` module. Their use is optional --- you can configure the |
| 29 | logging module using these functions or by making calls to the main API (defined |
| 30 | in :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 | |
| 75 | .. versionadded:: 2.7 |
| 76 | |
| 77 | .. function:: fileConfig(fname[, defaults]) |
| 78 | |
| 79 | Reads the logging configuration from a :mod:`configparser`\-format file named |
| 80 | *fname*. This function can be called several times from an application, |
| 81 | allowing an end user to select from various pre-canned |
| 82 | configurations (if the developer provides a mechanism to present the choices |
| 83 | and load the chosen configuration). Defaults to be passed to the ConfigParser |
| 84 | can be specified in the *defaults* argument. |
| 85 | |
| 86 | |
| 87 | .. function:: listen(port=DEFAULT_LOGGING_CONFIG_PORT) |
| 88 | |
| 89 | Starts up a socket server on the specified port, and listens for new |
| 90 | configurations. If no port is specified, the module's default |
| 91 | :const:`DEFAULT_LOGGING_CONFIG_PORT` is used. Logging configurations will be |
| 92 | sent as a file suitable for processing by :func:`fileConfig`. Returns a |
| 93 | :class:`Thread` instance on which you can call :meth:`start` to start the |
| 94 | server, and which you can :meth:`join` when appropriate. To stop the server, |
| 95 | call :func:`stopListening`. |
| 96 | |
| 97 | To send a configuration to the socket, read in the configuration file and |
| 98 | send it to the socket as a string of bytes preceded by a four-byte length |
| 99 | string packed in binary using ``struct.pack('>L', n)``. |
| 100 | |
| 101 | |
| 102 | .. function:: stopListening() |
| 103 | |
| 104 | Stops the listening server which was created with a call to :func:`listen`. |
| 105 | This is typically called before calling :meth:`join` on the return value from |
| 106 | :func:`listen`. |
| 107 | |
| 108 | |
| 109 | .. _logging-config-dictschema: |
| 110 | |
| 111 | Configuration dictionary schema |
| 112 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 113 | |
| 114 | Describing a logging configuration requires listing the various |
| 115 | objects to create and the connections between them; for example, you |
| 116 | may create a handler named 'console' and then say that the logger |
| 117 | named 'startup' will send its messages to the 'console' handler. |
| 118 | These objects aren't limited to those provided by the :mod:`logging` |
| 119 | module because you might write your own formatter or handler class. |
| 120 | The parameters to these classes may also need to include external |
| 121 | objects such as ``sys.stderr``. The syntax for describing these |
| 122 | objects and connections is defined in :ref:`logging-config-dict-connections` |
| 123 | below. |
| 124 | |
| 125 | Dictionary Schema Details |
| 126 | """"""""""""""""""""""""" |
| 127 | |
| 128 | The dictionary passed to :func:`dictConfig` must contain the following |
| 129 | keys: |
| 130 | |
| 131 | * *version* - to be set to an integer value representing the schema |
| 132 | version. The only valid value at present is 1, but having this key |
| 133 | allows the schema to evolve while still preserving backwards |
| 134 | compatibility. |
| 135 | |
| 136 | All other keys are optional, but if present they will be interpreted |
| 137 | as described below. In all cases below where a 'configuring dict' is |
| 138 | mentioned, it will be checked for the special ``'()'`` key to see if a |
| 139 | custom instantiation is required. If so, the mechanism described in |
| 140 | :ref:`logging-config-dict-userdef` below is used to create an instance; |
| 141 | otherwise, the context is used to determine what to instantiate. |
| 142 | |
| 143 | * *formatters* - the corresponding value will be a dict in which each |
| 144 | key is a formatter id and each value is a dict describing how to |
| 145 | configure the corresponding Formatter instance. |
| 146 | |
| 147 | The configuring dict is searched for keys ``format`` and ``datefmt`` |
| 148 | (with defaults of ``None``) and these are used to construct a |
| 149 | :class:`logging.Formatter` instance. |
| 150 | |
| 151 | * *filters* - the corresponding value will be a dict in which each key |
| 152 | is a filter id and each value is a dict describing how to configure |
| 153 | the corresponding Filter instance. |
| 154 | |
| 155 | The configuring dict is searched for the key ``name`` (defaulting to the |
| 156 | empty string) and this is used to construct a :class:`logging.Filter` |
| 157 | instance. |
| 158 | |
| 159 | * *handlers* - the corresponding value will be a dict in which each |
| 160 | key is a handler id and each value is a dict describing how to |
| 161 | configure the corresponding Handler instance. |
| 162 | |
| 163 | The configuring dict is searched for the following keys: |
| 164 | |
| 165 | * ``class`` (mandatory). This is the fully qualified name of the |
| 166 | handler class. |
| 167 | |
| 168 | * ``level`` (optional). The level of the handler. |
| 169 | |
| 170 | * ``formatter`` (optional). The id of the formatter for this |
| 171 | handler. |
| 172 | |
| 173 | * ``filters`` (optional). A list of ids of the filters for this |
| 174 | handler. |
| 175 | |
| 176 | All *other* keys are passed through as keyword arguments to the |
| 177 | handler's constructor. For example, given the snippet:: |
| 178 | |
| 179 | handlers: |
| 180 | console: |
| 181 | class : logging.StreamHandler |
| 182 | formatter: brief |
| 183 | level : INFO |
| 184 | filters: [allow_foo] |
| 185 | stream : ext://sys.stdout |
| 186 | file: |
| 187 | class : logging.handlers.RotatingFileHandler |
| 188 | formatter: precise |
| 189 | filename: logconfig.log |
| 190 | maxBytes: 1024 |
| 191 | backupCount: 3 |
| 192 | |
| 193 | the handler with id ``console`` is instantiated as a |
| 194 | :class:`logging.StreamHandler`, using ``sys.stdout`` as the underlying |
| 195 | stream. The handler with id ``file`` is instantiated as a |
| 196 | :class:`logging.handlers.RotatingFileHandler` with the keyword arguments |
| 197 | ``filename='logconfig.log', maxBytes=1024, backupCount=3``. |
| 198 | |
| 199 | * *loggers* - the corresponding value will be a dict in which each key |
| 200 | is a logger name and each value is a dict describing how to |
| 201 | configure the corresponding Logger instance. |
| 202 | |
| 203 | The configuring dict is searched for the following keys: |
| 204 | |
| 205 | * ``level`` (optional). The level of the logger. |
| 206 | |
| 207 | * ``propagate`` (optional). The propagation setting of the logger. |
| 208 | |
| 209 | * ``filters`` (optional). A list of ids of the filters for this |
| 210 | logger. |
| 211 | |
| 212 | * ``handlers`` (optional). A list of ids of the handlers for this |
| 213 | logger. |
| 214 | |
| 215 | The specified loggers will be configured according to the level, |
| 216 | propagation, filters and handlers specified. |
| 217 | |
| 218 | * *root* - this will be the configuration for the root logger. |
| 219 | Processing of the configuration will be as for any logger, except |
| 220 | that the ``propagate`` setting will not be applicable. |
| 221 | |
| 222 | * *incremental* - whether the configuration is to be interpreted as |
| 223 | incremental to the existing configuration. This value defaults to |
| 224 | ``False``, which means that the specified configuration replaces the |
| 225 | existing configuration with the same semantics as used by the |
| 226 | existing :func:`fileConfig` API. |
| 227 | |
| 228 | If the specified value is ``True``, the configuration is processed |
| 229 | as described in the section on :ref:`logging-config-dict-incremental`. |
| 230 | |
| 231 | * *disable_existing_loggers* - whether any existing loggers are to be |
| 232 | disabled. This setting mirrors the parameter of the same name in |
| 233 | :func:`fileConfig`. If absent, this parameter defaults to ``True``. |
| 234 | This value is ignored if *incremental* is ``True``. |
| 235 | |
| 236 | .. _logging-config-dict-incremental: |
| 237 | |
| 238 | Incremental Configuration |
| 239 | """"""""""""""""""""""""" |
| 240 | |
| 241 | It is difficult to provide complete flexibility for incremental |
| 242 | configuration. For example, because objects such as filters |
| 243 | and formatters are anonymous, once a configuration is set up, it is |
| 244 | not possible to refer to such anonymous objects when augmenting a |
| 245 | configuration. |
| 246 | |
| 247 | Furthermore, there is not a compelling case for arbitrarily altering |
| 248 | the object graph of loggers, handlers, filters, formatters at |
| 249 | run-time, once a configuration is set up; the verbosity of loggers and |
| 250 | handlers can be controlled just by setting levels (and, in the case of |
| 251 | loggers, propagation flags). Changing the object graph arbitrarily in |
| 252 | a safe way is problematic in a multi-threaded environment; while not |
| 253 | impossible, the benefits are not worth the complexity it adds to the |
| 254 | implementation. |
| 255 | |
| 256 | Thus, when the ``incremental`` key of a configuration dict is present |
| 257 | and is ``True``, the system will completely ignore any ``formatters`` and |
| 258 | ``filters`` entries, and process only the ``level`` |
| 259 | settings in the ``handlers`` entries, and the ``level`` and |
| 260 | ``propagate`` settings in the ``loggers`` and ``root`` entries. |
| 261 | |
| 262 | Using a value in the configuration dict lets configurations to be sent |
| 263 | over the wire as pickled dicts to a socket listener. Thus, the logging |
| 264 | verbosity of a long-running application can be altered over time with |
| 265 | no need to stop and restart the application. |
| 266 | |
| 267 | .. _logging-config-dict-connections: |
| 268 | |
| 269 | Object connections |
| 270 | """""""""""""""""" |
| 271 | |
| 272 | The schema describes a set of logging objects - loggers, |
| 273 | handlers, formatters, filters - which are connected to each other in |
| 274 | an object graph. Thus, the schema needs to represent connections |
| 275 | between the objects. For example, say that, once configured, a |
| 276 | particular logger has attached to it a particular handler. For the |
| 277 | purposes of this discussion, we can say that the logger represents the |
| 278 | source, and the handler the destination, of a connection between the |
| 279 | two. Of course in the configured objects this is represented by the |
| 280 | logger holding a reference to the handler. In the configuration dict, |
| 281 | this is done by giving each destination object an id which identifies |
| 282 | it unambiguously, and then using the id in the source object's |
| 283 | configuration to indicate that a connection exists between the source |
| 284 | and the destination object with that id. |
| 285 | |
| 286 | So, for example, consider the following YAML snippet:: |
| 287 | |
| 288 | formatters: |
| 289 | brief: |
| 290 | # configuration for formatter with id 'brief' goes here |
| 291 | precise: |
| 292 | # configuration for formatter with id 'precise' goes here |
| 293 | handlers: |
| 294 | h1: #This is an id |
| 295 | # configuration of handler with id 'h1' goes here |
| 296 | formatter: brief |
| 297 | h2: #This is another id |
| 298 | # configuration of handler with id 'h2' goes here |
| 299 | formatter: precise |
| 300 | loggers: |
| 301 | foo.bar.baz: |
| 302 | # other configuration for logger 'foo.bar.baz' |
| 303 | handlers: [h1, h2] |
| 304 | |
| 305 | (Note: YAML used here because it's a little more readable than the |
| 306 | equivalent Python source form for the dictionary.) |
| 307 | |
| 308 | The ids for loggers are the logger names which would be used |
| 309 | programmatically to obtain a reference to those loggers, e.g. |
| 310 | ``foo.bar.baz``. The ids for Formatters and Filters can be any string |
| 311 | value (such as ``brief``, ``precise`` above) and they are transient, |
| 312 | in that they are only meaningful for processing the configuration |
| 313 | dictionary and used to determine connections between objects, and are |
| 314 | not persisted anywhere when the configuration call is complete. |
| 315 | |
| 316 | The above snippet indicates that logger named ``foo.bar.baz`` should |
| 317 | have two handlers attached to it, which are described by the handler |
| 318 | ids ``h1`` and ``h2``. The formatter for ``h1`` is that described by id |
| 319 | ``brief``, and the formatter for ``h2`` is that described by id |
| 320 | ``precise``. |
| 321 | |
| 322 | |
| 323 | .. _logging-config-dict-userdef: |
| 324 | |
| 325 | User-defined objects |
| 326 | """""""""""""""""""" |
| 327 | |
| 328 | The schema supports user-defined objects for handlers, filters and |
| 329 | formatters. (Loggers do not need to have different types for |
| 330 | different instances, so there is no support in this configuration |
| 331 | schema for user-defined logger classes.) |
| 332 | |
| 333 | Objects to be configured are described by dictionaries |
| 334 | which detail their configuration. In some places, the logging system |
| 335 | will be able to infer from the context how an object is to be |
| 336 | instantiated, but when a user-defined object is to be instantiated, |
| 337 | the system will not know how to do this. In order to provide complete |
| 338 | flexibility for user-defined object instantiation, the user needs |
| 339 | to provide a 'factory' - a callable which is called with a |
| 340 | configuration dictionary and which returns the instantiated object. |
| 341 | This is signalled by an absolute import path to the factory being |
| 342 | made available under the special key ``'()'``. Here's a concrete |
| 343 | example:: |
| 344 | |
| 345 | formatters: |
| 346 | brief: |
| 347 | format: '%(message)s' |
| 348 | default: |
| 349 | format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s' |
| 350 | datefmt: '%Y-%m-%d %H:%M:%S' |
| 351 | custom: |
| 352 | (): my.package.customFormatterFactory |
| 353 | bar: baz |
| 354 | spam: 99.9 |
| 355 | answer: 42 |
| 356 | |
| 357 | The above YAML snippet defines three formatters. The first, with id |
| 358 | ``brief``, is a standard :class:`logging.Formatter` instance with the |
| 359 | specified format string. The second, with id ``default``, has a |
| 360 | longer format and also defines the time format explicitly, and will |
| 361 | result in a :class:`logging.Formatter` initialized with those two format |
| 362 | strings. Shown in Python source form, the ``brief`` and ``default`` |
| 363 | formatters have configuration sub-dictionaries:: |
| 364 | |
| 365 | { |
| 366 | 'format' : '%(message)s' |
| 367 | } |
| 368 | |
| 369 | and:: |
| 370 | |
| 371 | { |
| 372 | 'format' : '%(asctime)s %(levelname)-8s %(name)-15s %(message)s', |
| 373 | 'datefmt' : '%Y-%m-%d %H:%M:%S' |
| 374 | } |
| 375 | |
| 376 | respectively, and as these dictionaries do not contain the special key |
| 377 | ``'()'``, the instantiation is inferred from the context: as a result, |
| 378 | standard :class:`logging.Formatter` instances are created. The |
| 379 | configuration sub-dictionary for the third formatter, with id |
| 380 | ``custom``, is:: |
| 381 | |
| 382 | { |
| 383 | '()' : 'my.package.customFormatterFactory', |
| 384 | 'bar' : 'baz', |
| 385 | 'spam' : 99.9, |
| 386 | 'answer' : 42 |
| 387 | } |
| 388 | |
| 389 | and this contains the special key ``'()'``, which means that |
| 390 | user-defined instantiation is wanted. In this case, the specified |
| 391 | factory callable will be used. If it is an actual callable it will be |
| 392 | used directly - otherwise, if you specify a string (as in the example) |
| 393 | the actual callable will be located using normal import mechanisms. |
| 394 | The callable will be called with the **remaining** items in the |
| 395 | configuration sub-dictionary as keyword arguments. In the above |
| 396 | example, the formatter with id ``custom`` will be assumed to be |
| 397 | returned by the call:: |
| 398 | |
| 399 | my.package.customFormatterFactory(bar='baz', spam=99.9, answer=42) |
| 400 | |
| 401 | The key ``'()'`` has been used as the special key because it is not a |
| 402 | valid keyword parameter name, and so will not clash with the names of |
| 403 | the keyword arguments used in the call. The ``'()'`` also serves as a |
| 404 | mnemonic that the corresponding value is a callable. |
| 405 | |
| 406 | |
| 407 | .. _logging-config-dict-externalobj: |
| 408 | |
| 409 | Access to external objects |
| 410 | """""""""""""""""""""""""" |
| 411 | |
| 412 | There are times where a configuration needs to refer to objects |
| 413 | external to the configuration, for example ``sys.stderr``. If the |
| 414 | configuration dict is constructed using Python code, this is |
| 415 | straightforward, but a problem arises when the configuration is |
| 416 | provided via a text file (e.g. JSON, YAML). In a text file, there is |
| 417 | no standard way to distinguish ``sys.stderr`` from the literal string |
| 418 | ``'sys.stderr'``. To facilitate this distinction, the configuration |
| 419 | system looks for certain special prefixes in string values and |
| 420 | treat them specially. For example, if the literal string |
| 421 | ``'ext://sys.stderr'`` is provided as a value in the configuration, |
| 422 | then the ``ext://`` will be stripped off and the remainder of the |
| 423 | value processed using normal import mechanisms. |
| 424 | |
| 425 | The handling of such prefixes is done in a way analogous to protocol |
| 426 | handling: there is a generic mechanism to look for prefixes which |
| 427 | match the regular expression ``^(?P<prefix>[a-z]+)://(?P<suffix>.*)$`` |
| 428 | whereby, if the ``prefix`` is recognised, the ``suffix`` is processed |
| 429 | in a prefix-dependent manner and the result of the processing replaces |
| 430 | the string value. If the prefix is not recognised, then the string |
| 431 | value will be left as-is. |
| 432 | |
| 433 | |
| 434 | .. _logging-config-dict-internalobj: |
| 435 | |
| 436 | Access to internal objects |
| 437 | """""""""""""""""""""""""" |
| 438 | |
| 439 | As well as external objects, there is sometimes also a need to refer |
| 440 | to objects in the configuration. This will be done implicitly by the |
| 441 | configuration system for things that it knows about. For example, the |
| 442 | string value ``'DEBUG'`` for a ``level`` in a logger or handler will |
| 443 | automatically be converted to the value ``logging.DEBUG``, and the |
| 444 | ``handlers``, ``filters`` and ``formatter`` entries will take an |
| 445 | object id and resolve to the appropriate destination object. |
| 446 | |
| 447 | However, a more generic mechanism is needed for user-defined |
| 448 | objects which are not known to the :mod:`logging` module. For |
| 449 | example, consider :class:`logging.handlers.MemoryHandler`, which takes |
| 450 | a ``target`` argument which is another handler to delegate to. Since |
| 451 | the system already knows about this class, then in the configuration, |
| 452 | the given ``target`` just needs to be the object id of the relevant |
| 453 | target handler, and the system will resolve to the handler from the |
| 454 | id. If, however, a user defines a ``my.package.MyHandler`` which has |
| 455 | an ``alternate`` handler, the configuration system would not know that |
| 456 | the ``alternate`` referred to a handler. To cater for this, a generic |
| 457 | resolution system allows the user to specify:: |
| 458 | |
| 459 | handlers: |
| 460 | file: |
| 461 | # configuration of file handler goes here |
| 462 | |
| 463 | custom: |
| 464 | (): my.package.MyHandler |
| 465 | alternate: cfg://handlers.file |
| 466 | |
| 467 | The literal string ``'cfg://handlers.file'`` will be resolved in an |
| 468 | analogous way to strings with the ``ext://`` prefix, but looking |
| 469 | in the configuration itself rather than the import namespace. The |
| 470 | mechanism allows access by dot or by index, in a similar way to |
| 471 | that provided by ``str.format``. Thus, given the following snippet:: |
| 472 | |
| 473 | handlers: |
| 474 | email: |
| 475 | class: logging.handlers.SMTPHandler |
| 476 | mailhost: localhost |
| 477 | fromaddr: my_app@domain.tld |
| 478 | toaddrs: |
| 479 | - support_team@domain.tld |
| 480 | - dev_team@domain.tld |
| 481 | subject: Houston, we have a problem. |
| 482 | |
| 483 | in the configuration, the string ``'cfg://handlers'`` would resolve to |
| 484 | the dict with key ``handlers``, the string ``'cfg://handlers.email`` |
| 485 | would resolve to the dict with key ``email`` in the ``handlers`` dict, |
| 486 | and so on. The string ``'cfg://handlers.email.toaddrs[1]`` would |
| 487 | resolve to ``'dev_team.domain.tld'`` and the string |
| 488 | ``'cfg://handlers.email.toaddrs[0]'`` would resolve to the value |
| 489 | ``'support_team@domain.tld'``. The ``subject`` value could be accessed |
| 490 | using either ``'cfg://handlers.email.subject'`` or, equivalently, |
| 491 | ``'cfg://handlers.email[subject]'``. The latter form only needs to be |
| 492 | used if the key contains spaces or non-alphanumeric characters. If an |
| 493 | index value consists only of decimal digits, access will be attempted |
| 494 | using the corresponding integer value, falling back to the string |
| 495 | value if needed. |
| 496 | |
| 497 | Given a string ``cfg://handlers.myhandler.mykey.123``, this will |
| 498 | resolve to ``config_dict['handlers']['myhandler']['mykey']['123']``. |
| 499 | If the string is specified as ``cfg://handlers.myhandler.mykey[123]``, |
| 500 | the system will attempt to retrieve the value from |
| 501 | ``config_dict['handlers']['myhandler']['mykey'][123]``, and fall back |
| 502 | to ``config_dict['handlers']['myhandler']['mykey']['123']`` if that |
| 503 | fails. |
| 504 | |
| 505 | .. _logging-config-fileformat: |
| 506 | |
| 507 | Configuration file format |
| 508 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 509 | |
| 510 | The configuration file format understood by :func:`fileConfig` is based on |
| 511 | :mod:`configparser` functionality. The file must contain sections called |
| 512 | ``[loggers]``, ``[handlers]`` and ``[formatters]`` which identify by name the |
| 513 | entities of each type which are defined in the file. For each such entity, there |
| 514 | is a separate section which identifies how that entity is configured. Thus, for |
| 515 | a logger named ``log01`` in the ``[loggers]`` section, the relevant |
| 516 | configuration details are held in a section ``[logger_log01]``. Similarly, a |
| 517 | handler called ``hand01`` in the ``[handlers]`` section will have its |
| 518 | configuration held in a section called ``[handler_hand01]``, while a formatter |
| 519 | called ``form01`` in the ``[formatters]`` section will have its configuration |
| 520 | specified in a section called ``[formatter_form01]``. The root logger |
| 521 | configuration must be specified in a section called ``[logger_root]``. |
| 522 | |
| 523 | Examples of these sections in the file are given below. :: |
| 524 | |
| 525 | [loggers] |
| 526 | keys=root,log02,log03,log04,log05,log06,log07 |
| 527 | |
| 528 | [handlers] |
| 529 | keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09 |
| 530 | |
| 531 | [formatters] |
| 532 | keys=form01,form02,form03,form04,form05,form06,form07,form08,form09 |
| 533 | |
| 534 | The root logger must specify a level and a list of handlers. An example of a |
| 535 | root logger section is given below. :: |
| 536 | |
| 537 | [logger_root] |
| 538 | level=NOTSET |
| 539 | handlers=hand01 |
| 540 | |
| 541 | The ``level`` entry can be one of ``DEBUG, INFO, WARNING, ERROR, CRITICAL`` or |
| 542 | ``NOTSET``. For the root logger only, ``NOTSET`` means that all messages will be |
| 543 | logged. Level values are :func:`eval`\ uated in the context of the ``logging`` |
| 544 | package's namespace. |
| 545 | |
| 546 | The ``handlers`` entry is a comma-separated list of handler names, which must |
| 547 | appear in the ``[handlers]`` section. These names must appear in the |
| 548 | ``[handlers]`` section and have corresponding sections in the configuration |
| 549 | file. |
| 550 | |
| 551 | For loggers other than the root logger, some additional information is required. |
| 552 | This is illustrated by the following example. :: |
| 553 | |
| 554 | [logger_parser] |
| 555 | level=DEBUG |
| 556 | handlers=hand01 |
| 557 | propagate=1 |
| 558 | qualname=compiler.parser |
| 559 | |
| 560 | The ``level`` and ``handlers`` entries are interpreted as for the root logger, |
| 561 | except that if a non-root logger's level is specified as ``NOTSET``, the system |
| 562 | consults loggers higher up the hierarchy to determine the effective level of the |
| 563 | logger. The ``propagate`` entry is set to 1 to indicate that messages must |
| 564 | propagate to handlers higher up the logger hierarchy from this logger, or 0 to |
| 565 | indicate that messages are **not** propagated to handlers up the hierarchy. The |
| 566 | ``qualname`` entry is the hierarchical channel name of the logger, that is to |
| 567 | say the name used by the application to get the logger. |
| 568 | |
| 569 | Sections which specify handler configuration are exemplified by the following. |
| 570 | :: |
| 571 | |
| 572 | [handler_hand01] |
| 573 | class=StreamHandler |
| 574 | level=NOTSET |
| 575 | formatter=form01 |
| 576 | args=(sys.stdout,) |
| 577 | |
| 578 | The ``class`` entry indicates the handler's class (as determined by :func:`eval` |
| 579 | in the ``logging`` package's namespace). The ``level`` is interpreted as for |
| 580 | loggers, and ``NOTSET`` is taken to mean 'log everything'. |
| 581 | |
| 582 | .. versionchanged:: 2.6 |
| 583 | Added support for resolving the handler’s class as a dotted module and |
| 584 | class name. |
| 585 | |
| 586 | The ``formatter`` entry indicates the key name of the formatter for this |
| 587 | handler. If blank, a default formatter (``logging._defaultFormatter``) is used. |
| 588 | If a name is specified, it must appear in the ``[formatters]`` section and have |
| 589 | a corresponding section in the configuration file. |
| 590 | |
| 591 | The ``args`` entry, when :func:`eval`\ uated in the context of the ``logging`` |
| 592 | package's namespace, is the list of arguments to the constructor for the handler |
| 593 | class. Refer to the constructors for the relevant handlers, or to the examples |
| 594 | below, to see how typical entries are constructed. :: |
| 595 | |
| 596 | [handler_hand02] |
| 597 | class=FileHandler |
| 598 | level=DEBUG |
| 599 | formatter=form02 |
| 600 | args=('python.log', 'w') |
| 601 | |
| 602 | [handler_hand03] |
| 603 | class=handlers.SocketHandler |
| 604 | level=INFO |
| 605 | formatter=form03 |
| 606 | args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT) |
| 607 | |
| 608 | [handler_hand04] |
| 609 | class=handlers.DatagramHandler |
| 610 | level=WARN |
| 611 | formatter=form04 |
| 612 | args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT) |
| 613 | |
| 614 | [handler_hand05] |
| 615 | class=handlers.SysLogHandler |
| 616 | level=ERROR |
| 617 | formatter=form05 |
| 618 | args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER) |
| 619 | |
| 620 | [handler_hand06] |
| 621 | class=handlers.NTEventLogHandler |
| 622 | level=CRITICAL |
| 623 | formatter=form06 |
| 624 | args=('Python Application', '', 'Application') |
| 625 | |
| 626 | [handler_hand07] |
| 627 | class=handlers.SMTPHandler |
| 628 | level=WARN |
| 629 | formatter=form07 |
| 630 | args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject') |
| 631 | |
| 632 | [handler_hand08] |
| 633 | class=handlers.MemoryHandler |
| 634 | level=NOTSET |
| 635 | formatter=form08 |
| 636 | target= |
| 637 | args=(10, ERROR) |
| 638 | |
| 639 | [handler_hand09] |
| 640 | class=handlers.HTTPHandler |
| 641 | level=NOTSET |
| 642 | formatter=form09 |
| 643 | args=('localhost:9022', '/log', 'GET') |
| 644 | |
| 645 | Sections which specify formatter configuration are typified by the following. :: |
| 646 | |
| 647 | [formatter_form01] |
| 648 | format=F1 %(asctime)s %(levelname)s %(message)s |
| 649 | datefmt= |
| 650 | class=logging.Formatter |
| 651 | |
| 652 | The ``format`` entry is the overall format string, and the ``datefmt`` entry is |
| 653 | the :func:`strftime`\ -compatible date/time format string. If empty, the |
| 654 | package substitutes ISO8601 format date/times, which is almost equivalent to |
| 655 | specifying the date format string ``'%Y-%m-%d %H:%M:%S'``. The ISO8601 format |
| 656 | also specifies milliseconds, which are appended to the result of using the above |
| 657 | format string, with a comma separator. An example time in ISO8601 format is |
| 658 | ``2003-01-23 00:29:50,411``. |
| 659 | |
| 660 | The ``class`` entry is optional. It indicates the name of the formatter's class |
| 661 | (as a dotted module and class name.) This option is useful for instantiating a |
| 662 | :class:`Formatter` subclass. Subclasses of :class:`Formatter` can present |
| 663 | exception tracebacks in an expanded or condensed format. |
| 664 | |
| 665 | .. seealso:: |
| 666 | |
| 667 | Module :mod:`logging` |
| 668 | API reference for the logging module. |
| 669 | |
| 670 | Module :mod:`logging.handlers` |
| 671 | Useful handlers included with the logging module. |
| 672 | |
| 673 | |