Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ea56d2

Browse files
Preston-Landersvsajip
authored andcommitted
bpo-31080: Allowed logging.config.fileConfig() to accept both args and kwargs. (GH-2979)
1 parent de34cbe commit 6ea56d2

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

Doc/library/logging.config.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,12 @@ a corresponding section in the configuration file.
715715
The ``args`` entry, when :func:`eval`\ uated in the context of the ``logging``
716716
package's namespace, is the list of arguments to the constructor for the handler
717717
class. Refer to the constructors for the relevant handlers, or to the examples
718-
below, to see how typical entries are constructed.
718+
below, to see how typical entries are constructed. If not provided, it defaults
719+
to ``()``.
720+
721+
The optional ``kwargs`` entry, when :func:`eval`\ uated in the context of the
722+
``logging`` package's namespace, is the keyword argument dict to the constructor
723+
for the handler class. If not provided, it defaults to ``{}``.
719724

720725
.. code-block:: ini
721726
@@ -754,6 +759,7 @@ below, to see how typical entries are constructed.
754759
level=WARN
755760
formatter=form07
756761
args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
762+
kwargs={'timeout': 10.0}
757763
758764
[handler_hand08]
759765
class=handlers.MemoryHandler
@@ -767,6 +773,7 @@ below, to see how typical entries are constructed.
767773
level=NOTSET
768774
formatter=form09
769775
args=('localhost:9022', '/log', 'GET')
776+
kwargs={'secure': True}
770777
771778
Sections which specify formatter configuration are typified by the following.
772779

Lib/logging/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ def _install_handlers(cp, formatters):
143143
klass = eval(klass, vars(logging))
144144
except (AttributeError, NameError):
145145
klass = _resolve(klass)
146-
args = section["args"]
146+
args = section.get("args", '()')
147147
args = eval(args, vars(logging))
148-
h = klass(*args)
148+
kwargs = section.get("kwargs", '{}')
149+
kwargs = eval(kwargs, vars(logging))
150+
h = klass(*args, **kwargs)
149151
if "level" in section:
150152
level = section["level"]
151153
h.setLevel(level)

Lib/test/test_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ class ConfigFileTest(BaseTest):
12731273
datefmt=
12741274
"""
12751275

1276-
# config7 adds a compiler logger.
1276+
# config7 adds a compiler logger, and uses kwargs instead of args.
12771277
config7 = """
12781278
[loggers]
12791279
keys=root,parser,compiler
@@ -1304,7 +1304,7 @@ class ConfigFileTest(BaseTest):
13041304
class=StreamHandler
13051305
level=NOTSET
13061306
formatter=form1
1307-
args=(sys.stdout,)
1307+
kwargs={'stream': sys.stdout,}
13081308
13091309
[formatter_form1]
13101310
format=%(levelname)s ++ %(message)s
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow `logging.config.fileConfig` to accept kwargs and/or args.

0 commit comments

Comments
 (0)