aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/logging/config.py
diff options
context:
space:
mode:
authorBar Harel <bharel@barharel.com>2023-04-12 08:35:56 +0100
committerGitHub <noreply@github.com>2023-04-12 08:35:56 +0100
commit8f54302ab49a07e857843f1a551db5ddb536ce56 (patch)
treece22dc0d943450b642a69290303c956385b8740a /Lib/logging/config.py
parent449bf2a76b23b97a38158d506bc30d3ebe006321 (diff)
downloadcpython-8f54302ab49a07e857843f1a551db5ddb536ce56.tar.gz
cpython-8f54302ab49a07e857843f1a551db5ddb536ce56.zip
gh-103357: Add logging.Formatter defaults support to logging.config fileConfig and dictConfig (GH-103359)
Diffstat (limited to 'Lib/logging/config.py')
-rw-r--r--Lib/logging/config.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/Lib/logging/config.py b/Lib/logging/config.py
index 7cd16c643e9..16c54a6a4f7 100644
--- a/Lib/logging/config.py
+++ b/Lib/logging/config.py
@@ -114,11 +114,18 @@ def _create_formatters(cp):
fs = cp.get(sectname, "format", raw=True, fallback=None)
dfs = cp.get(sectname, "datefmt", raw=True, fallback=None)
stl = cp.get(sectname, "style", raw=True, fallback='%')
+ defaults = cp.get(sectname, "defaults", raw=True, fallback=None)
+
c = logging.Formatter
class_name = cp[sectname].get("class")
if class_name:
c = _resolve(class_name)
- f = c(fs, dfs, stl)
+
+ if defaults is not None:
+ defaults = eval(defaults, vars(logging))
+ f = c(fs, dfs, stl, defaults=defaults)
+ else:
+ f = c(fs, dfs, stl)
formatters[form] = f
return formatters
@@ -668,18 +675,27 @@ class DictConfigurator(BaseConfigurator):
dfmt = config.get('datefmt', None)
style = config.get('style', '%')
cname = config.get('class', None)
+ defaults = config.get('defaults', None)
if not cname:
c = logging.Formatter
else:
c = _resolve(cname)
+ kwargs = {}
+
+ # Add defaults only if it exists.
+ # Prevents TypeError in custom formatter callables that do not
+ # accept it.
+ if defaults is not None:
+ kwargs['defaults'] = defaults
+
# A TypeError would be raised if "validate" key is passed in with a formatter callable
# that does not accept "validate" as a parameter
if 'validate' in config: # if user hasn't mentioned it, the default will be fine
- result = c(fmt, dfmt, style, config['validate'])
+ result = c(fmt, dfmt, style, config['validate'], **kwargs)
else:
- result = c(fmt, dfmt, style)
+ result = c(fmt, dfmt, style, **kwargs)
return result