diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-04-11 07:42:07 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-04-11 07:42:07 (GMT) |
commit | 4a0a31df5c6ab79dd7dc8ee828379dca1d1f632f (patch) | |
tree | 0246d7ef13a693a1194cbed38f1df9697f720735 /Lib/logging/__init__.py | |
parent | 98707c2cede1aa697a1338ea8f7c9a59da42dfe9 (diff) | |
download | cpython-4a0a31df5c6ab79dd7dc8ee828379dca1d1f632f.zip cpython-4a0a31df5c6ab79dd7dc8ee828379dca1d1f632f.tar.gz cpython-4a0a31df5c6ab79dd7dc8ee828379dca1d1f632f.tar.bz2 |
Added 'handlers' argument to logging.basicConfig.
Diffstat (limited to 'Lib/logging/__init__.py')
-rw-r--r-- | Lib/logging/__init__.py | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 7757a82..ef88d0a 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1650,6 +1650,10 @@ def basicConfig(**kwargs): 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 handler. Any handler + in the list which does not have a formatter assigned will be + assigned the formatter created in this function. Note that you could specify a stream created using open(filename, mode) rather than passing the filename and mode in. However, it should be @@ -1657,27 +1661,47 @@ def basicConfig(**kwargs): using sys.stdout or sys.stderr), whereas FileHandler closes its stream when the handler is closed. - .. versionchanged: 3.2 + .. 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``. """ # Add thread safety in case someone mistakenly calls # basicConfig() from multiple threads _acquireLock() try: if len(root.handlers) == 0: - filename = kwargs.get("filename") - if filename: - mode = kwargs.get("filemode", 'a') - hdlr = FileHandler(filename, mode) + handlers = kwargs.get("handlers") + if handlers is None: + if "stream" in kwargs and "filename" in kwargs: + raise ValueError("'stream' and 'filename' should not be " + "specified together") else: - stream = kwargs.get("stream") - hdlr = StreamHandler(stream) + if "stream" in kwargs or "filename" in kwargs: + raise ValueError("'stream' or 'filename' should not be " + "specified together with 'handlers'") + if handlers is None: + filename = kwargs.get("filename") + if filename: + mode = kwargs.get("filemode", 'a') + h = FileHandler(filename, mode) + else: + stream = kwargs.get("stream") + h = StreamHandler(stream) + handlers = [h] fs = kwargs.get("format", BASIC_FORMAT) dfs = kwargs.get("datefmt", None) style = kwargs.get("style", '%') fmt = Formatter(fs, dfs, style) - hdlr.setFormatter(fmt) - root.addHandler(hdlr) + for h in handlers: + if h.formatter is None: + h.setFormatter(fmt) + root.addHandler(h) level = kwargs.get("level") if level is not None: root.setLevel(level) |