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

Skip to content

Commit 1fd1202

Browse files
committed
Issue #20242: Fixed basicConfig() format strings for the alternative formatting styles.
1 parent 66c9350 commit 1fd1202

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

Lib/logging/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,12 @@ def usesTime(self):
388388
def format(self, record):
389389
return self._tpl.substitute(**record.__dict__)
390390

391+
BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
392+
391393
_STYLES = {
392-
'%': PercentStyle,
393-
'{': StrFormatStyle,
394-
'$': StringTemplateStyle
394+
'%': (PercentStyle, BASIC_FORMAT),
395+
'{': (StrFormatStyle, '{levelname}:{name}:{message}'),
396+
'$': (StringTemplateStyle, '${levelname}:${name}:${message}'),
395397
}
396398

397399
class Formatter(object):
@@ -456,7 +458,7 @@ def __init__(self, fmt=None, datefmt=None, style='%'):
456458
if style not in _STYLES:
457459
raise ValueError('Style must be one of: %s' % ','.join(
458460
_STYLES.keys()))
459-
self._style = _STYLES[style](fmt)
461+
self._style = _STYLES[style][0](fmt)
460462
self._fmt = self._style._fmt
461463
self.datefmt = datefmt
462464

@@ -1629,8 +1631,6 @@ def hasHandlers(self):
16291631
# Configuration classes and functions
16301632
#---------------------------------------------------------------------------
16311633

1632-
BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
1633-
16341634
def basicConfig(**kwargs):
16351635
"""
16361636
Do basic configuration for the logging system.
@@ -1704,9 +1704,12 @@ def basicConfig(**kwargs):
17041704
stream = kwargs.get("stream")
17051705
h = StreamHandler(stream)
17061706
handlers = [h]
1707-
fs = kwargs.get("format", BASIC_FORMAT)
17081707
dfs = kwargs.get("datefmt", None)
17091708
style = kwargs.get("style", '%')
1709+
if style not in _STYLES:
1710+
raise ValueError('Style must be one of: %s' % ','.join(
1711+
_STYLES.keys()))
1712+
fs = kwargs.get("format", _STYLES[style][1])
17101713
fmt = Formatter(fs, dfs, style)
17111714
for h in handlers:
17121715
if h.formatter is None:

Lib/test/test_logging.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,22 @@ def test_no_kwargs(self):
33373337
# level is not explicitly set
33383338
self.assertEqual(logging.root.level, self.original_logging_level)
33393339

3340+
def test_strformatstyle(self):
3341+
with captured_stdout() as output:
3342+
logging.basicConfig(stream=sys.stdout, style="{")
3343+
logging.error("Log an error")
3344+
sys.stdout.seek(0)
3345+
self.assertEqual(output.getvalue().strip(),
3346+
"ERROR:root:Log an error")
3347+
3348+
def test_stringtemplatestyle(self):
3349+
with captured_stdout() as output:
3350+
logging.basicConfig(stream=sys.stdout, style="$")
3351+
logging.error("Log an error")
3352+
sys.stdout.seek(0)
3353+
self.assertEqual(output.getvalue().strip(),
3354+
"ERROR:root:Log an error")
3355+
33403356
def test_filename(self):
33413357
logging.basicConfig(filename='test.log')
33423358

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #20242: Fixed basicConfig() format strings for the alternative
47+
formatting styles. Thanks to kespindler for the bug report and patch.
48+
4649
- Issues #20206 and #5803: Fix edge case in email.quoprimime.encode where it
4750
truncated lines ending in a character needing encoding but no newline by
4851
using a more efficient algorithm that doesn't have the bug.

0 commit comments

Comments
 (0)