[Python-Dev] Some changes to logging

Vinay Sajip vinay_sajip at red-dove.com
Fri Mar 19 10:54:02 EST 2004


I've had feedback from numerous sources that the logging package is harder
to configure than it needs to be, for the common use case of a simple script
which needs to log to a file. I propose to change the convenience function
basicConfig(), which is currently the one-shot convenience function for
simple scripts to use. The function signature change is backward-compatible.
The proposed new interface is:

def basicConfig(**kwargs):
    """
    Do basic configuration for the logging system.

    This function does nothing if the root logger already has handlers
    configured. 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.
    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.

    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.
    """

If any of you have comments/suggestions about this proposed change,
please let me know, otherwise I'll go ahead with this change.

Regards,

Vinay





More information about the Python-Dev mailing list