PyDocs

Nav

Generic Operating System Services

basicConfig

logging

function (source)
  • basicConfig(**kwargs)
Do basic configuration for the logging system.

This function does nothing if the root logger already has handlers
configured, unless the keyword argument *force* is set to ``True``.
It is a convenience method intended for use by simple scripts
to do one-shot configuration of the logging package.

The default behaviour is to create a StreamHandler which writes to
sys.stderr, set a formatter using the BASIC_FORMAT format string, and
add the handler to the root logger.

A number of optional keyword arguments may be specified, which can alter
the default behaviour.

filename  Specifies that a FileHandler be created, using the specified
          filename, rather than a StreamHandler.
filemode  Specifies the mode to open the file, if filename is specified
          (if filemode is unspecified, it defaults to 'a').
format    Use the specified format string for the handler.
datefmt   Use the specified date/time format.
style     If a format string is specified, use this to specify the
          type of format string (possible values '%', '{', '$', for
          %-formatting, :meth:`str.format` and :class:`string.Template`
          - defaults to '%').
level     Set the root logger level to the specified level.
stream    Use the specified stream to initialize the StreamHandler. Note
          that this argument is incompatible with 'filename' - if both
          are present, 'stream' is ignored.
handlers  If specified, this should be an iterable of already created
          handlers, which will be added to the root logger. Any handler
          in the list which does not have a formatter assigned will be
          assigned the formatter created in this function.
force     If this keyword  is specified as true, any existing handlers
          attached to the root logger are removed and closed, before
          carrying out the configuration as specified by the other
          arguments.
encoding  If specified together with a filename, this encoding is passed to
          the created FileHandler, causing it to be used when the file is
          opened.
errors    If specified together with a filename, this value is passed to the
          created FileHandler, causing it to be used when the file is
          opened in text mode. If not specified, the default value is
          `backslashreplace`.
formatter If specified, set this formatter instance for all involved
          handlers.
          If not specified, the default is to create and use an instance of
          `logging.Formatter` based on arguments 'format', 'datefmt' and
          'style'.
          When 'formatter' is specified together with any of the three
          arguments 'format', 'datefmt' and 'style', a `ValueError`
          is raised to signal that these arguments would lose meaning
          otherwise.

Note that you could specify a stream created using open(filename, mode)
rather than passing the filename and mode in. However, it should be
remembered that StreamHandler does not close its stream (since it may be
using sys.stdout or sys.stderr), whereas FileHandler closes its stream
when the handler is closed.

.. versionchanged:: 3.2
   Added the ``style`` parameter.

.. versionchanged:: 3.3
   Added the ``handlers`` parameter. A ``ValueError`` is now thrown for
   incompatible arguments (e.g. ``handlers`` specified together with
   ``filename``/``filemode``, or ``filename``/``filemode`` specified
   together with ``stream``, or ``handlers`` specified together with
   ``stream``.

.. versionchanged:: 3.8
   Added the ``force`` parameter.

.. versionchanged:: 3.9
   Added the ``encoding`` and ``errors`` parameters.

.. versionchanged:: 3.15
   Added the ``formatter`` parameter.
Changed in 3.2, 3.3, 3.8, 3.9, 3.15
  • 3.2: The style argument was added.
  • 3.3: The handlers argument was added. Additional checks were added to catch situations where incompatible arguments are specified (e.g. handlers together with stream or filename, or stream together with filename).
  • 3.8: The force argument was added.
  • 3.9: The encoding and errors arguments were added.
  • 3.15: The formatter argument was added.
0 Examples
No examples for logging/basicConfig.
See Also
No see-alsos for logging/basicConfig
0 Notes
No notes for basicConfig