Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a5f29c9

Browse filesBrowse files
authored
gh-110875: Handle '.' properties in logging formatter configuration c… (GH-110943)
1 parent 7d21e3d commit a5f29c9
Copy full SHA for a5f29c9

File tree

Expand file treeCollapse file tree

2 files changed

+41
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-5
lines changed

‎Lib/logging/config.py

Copy file name to clipboardExpand all lines: Lib/logging/config.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,10 @@ def configure_custom(self, config):
482482
c = config.pop('()')
483483
if not callable(c):
484484
c = self.resolve(c)
485-
props = config.pop('.', None)
486485
# Check for valid identifiers
487-
kwargs = {k: config[k] for k in config if valid_ident(k)}
486+
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
488487
result = c(**kwargs)
488+
props = config.pop('.', None)
489489
if props:
490490
for name, value in props.items():
491491
setattr(result, name, value)
@@ -835,8 +835,7 @@ def configure_handler(self, config):
835835
factory = functools.partial(self._configure_queue_handler, klass)
836836
else:
837837
factory = klass
838-
props = config.pop('.', None)
839-
kwargs = {k: config[k] for k in config if valid_ident(k)}
838+
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
840839
try:
841840
result = factory(**kwargs)
842841
except TypeError as te:
@@ -854,6 +853,7 @@ def configure_handler(self, config):
854853
result.setLevel(logging._checkLevel(level))
855854
if filters:
856855
self.add_filters(result, filters)
856+
props = config.pop('.', None)
857857
if props:
858858
for name, value in props.items():
859859
setattr(result, name, value)

‎Lib/test/test_logging.py

Copy file name to clipboardExpand all lines: Lib/test/test_logging.py
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2998,6 +2998,39 @@ class ConfigDictTest(BaseTest):
29982998
},
29992999
}
30003000

3001+
class CustomFormatter(logging.Formatter):
3002+
custom_property = "."
3003+
3004+
def format(self, record):
3005+
return super().format(record)
3006+
3007+
config17 = {
3008+
'version': 1,
3009+
'formatters': {
3010+
"custom": {
3011+
"()": CustomFormatter,
3012+
"style": "{",
3013+
"datefmt": "%Y-%m-%d %H:%M:%S",
3014+
"format": "{message}", # <-- to force an exception when configuring
3015+
".": {
3016+
"custom_property": "value"
3017+
}
3018+
}
3019+
},
3020+
'handlers' : {
3021+
'hand1' : {
3022+
'class' : 'logging.StreamHandler',
3023+
'formatter' : 'custom',
3024+
'level' : 'NOTSET',
3025+
'stream' : 'ext://sys.stdout',
3026+
},
3027+
},
3028+
'root' : {
3029+
'level' : 'WARNING',
3030+
'handlers' : ['hand1'],
3031+
},
3032+
}
3033+
30013034
bad_format = {
30023035
"version": 1,
30033036
"formatters": {
@@ -3479,7 +3512,10 @@ def test_config16_ok(self):
34793512
{'msg': 'Hello'}))
34803513
self.assertEqual(result, 'Hello ++ defaultvalue')
34813514

3482-
3515+
def test_config17_ok(self):
3516+
self.apply_config(self.config17)
3517+
h = logging._handlers['hand1']
3518+
self.assertEqual(h.formatter.custom_property, 'value')
34833519

34843520
def setup_via_listener(self, text, verify=None):
34853521
text = text.encode("utf-8")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.