Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31080: allow logging.config.fileConfig to accept kwargs and/or args. #2979

Merged
merged 2 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Doc/library/logging.config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,12 @@ a corresponding section in the configuration file.
The ``args`` entry, when :func:`eval`\ uated in the context of the ``logging``
package's namespace, is the list of arguments to the constructor for the handler
class. Refer to the constructors for the relevant handlers, or to the examples
below, to see how typical entries are constructed.
below, to see how typical entries are constructed. If not provided, it defaults
to ``()``.

The optional ``kwargs`` entry, when :func:`eval`\ uated in the context of the
``logging`` package's namespace, is the keyword argument dict to the constructor
for the handler class. If not provided, it defaults to ``{}``.

.. code-block:: ini

Expand Down Expand Up @@ -754,6 +759,7 @@ below, to see how typical entries are constructed.
level=WARN
formatter=form07
args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
kwargs={'timeout': 10.0}

[handler_hand08]
class=handlers.MemoryHandler
Expand All @@ -767,6 +773,7 @@ below, to see how typical entries are constructed.
level=NOTSET
formatter=form09
args=('localhost:9022', '/log', 'GET')
kwargs={'secure': True}

Sections which specify formatter configuration are typified by the following.

Expand Down
6 changes: 4 additions & 2 deletions Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ def _install_handlers(cp, formatters):
klass = eval(klass, vars(logging))
except (AttributeError, NameError):
klass = _resolve(klass)
args = section["args"]
args = section.get("args", '()')
args = eval(args, vars(logging))
h = klass(*args)
kwargs = section.get("kwargs", '{}')
kwargs = eval(kwargs, vars(logging))
h = klass(*args, **kwargs)
if "level" in section:
level = section["level"]
h.setLevel(level)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ class ConfigFileTest(BaseTest):
datefmt=
"""

# config7 adds a compiler logger.
# config7 adds a compiler logger, and uses kwargs instead of args.
config7 = """
[loggers]
keys=root,parser,compiler
Expand Down Expand Up @@ -1304,7 +1304,7 @@ class ConfigFileTest(BaseTest):
class=StreamHandler
level=NOTSET
formatter=form1
args=(sys.stdout,)
kwargs={'stream': sys.stdout,}

[formatter_form1]
format=%(levelname)s ++ %(message)s
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow `logging.config.fileConfig` to accept kwargs and/or args.