Repository navigation
bpo-34844: logging.Formatter enhancement - Ensure styles and fmt matches in logging.Formatter #9703
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
Changes from 15 commits
66bbddf
0eaaf25
f4f6a60
d51e186
cc2cfbd
7c24e46
abd868d
c1359bb
921ba33
8484f29
4c35216
6eee298
aab0c46
59879e9
43c0b34
908c3ec
d73ab3a
ff05742
2664f85
74ca6a2
de1243f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,11 @@ otherwise, the context is used to determine what to instantiate. | |
| (with defaults of ``None``) and these are used to construct a | ||
| :class:`~logging.Formatter` instance. | ||
|
|
||
| .. versionchanged:: 3.8 | ||
| a ``validate`` key (with defaults of ``True``) can be added into | ||
| the ``formatters`` section of the configuring dict, this is to | ||
| validate the correct format. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| * *filters* - the corresponding value will be a dict in which each key | ||
| is a filter id and each value is a dict describing how to configure | ||
| the corresponding Filter instance. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,6 +544,9 @@ The useful mapping keys in a :class:`LogRecord` are given in the section on | |
| .. versionchanged:: 3.2 | ||
| The *style* parameter was added. | ||
|
|
||
| .. versionchanged:: 3.8 | ||
| Incorrect or mismatch style and fmt will raise a ``ValueError``. For | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| example: ``logging.Formatter('%(asctime)s - %(message)s', style='{')``. | ||
|
|
||
| .. method:: format(record) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import sys | ||
| import threading | ||
| import traceback | ||
| from inspect import signature | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC importing |
||
|
|
||
| from socketserver import ThreadingTCPServer, StreamRequestHandler | ||
|
|
||
|
|
@@ -666,13 +667,29 @@ def configure_formatter(self, config): | |
| dfmt = config.get('datefmt', None) | ||
| style = config.get('style', '%') | ||
| cname = config.get('class', None) | ||
| validate = config.get('validate', True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be overthinking this, but the logic in this area should perhaps be: |
||
| if not cname: | ||
| c = logging.Formatter | ||
| result = c(fmt, dfmt, style, validate) | ||
| else: | ||
| c = _resolve(cname) | ||
| result = c(fmt, dfmt, style) | ||
| if self._has_validate_param(config, c): | ||
| result = c(fmt, dfmt, style, validate) | ||
| else: | ||
| result = c(fmt, dfmt, style) | ||
| return result | ||
|
|
||
| def _has_validate_param(self, config, formatter_callable): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may not be needed, see above. |
||
| """Check if formatter callable has validate parameter""" | ||
| params = signature(formatter_callable).parameters | ||
| if not isinstance(formatter_callable, logging.Formatter) or \ | ||
| not params.get('validate') or params.get("kwargs"): | ||
| if config.get('validate'): | ||
| raise TypeError('%r has not validate in parameters' % formatter_callable) | ||
|
|
||
| return False | ||
| return True | ||
|
|
||
| def configure_filter(self, config): | ||
| """Configure a filter from a dictionary.""" | ||
| if '()' in config: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults->a default