Skip to content

bad logging documentation {}-formatting placeholder #109451

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

Closed
AlberLC opened this issue Sep 15, 2023 · 1 comment
Closed

bad logging documentation {}-formatting placeholder #109451

AlberLC opened this issue Sep 15, 2023 · 1 comment
Labels
docs Documentation in the Doc dir easy

Comments

@AlberLC
Copy link
Contributor

AlberLC commented Sep 15, 2023

Documentation

%-formatting (It works)

import logging
logging.basicConfig(filename='example.log', format='%(msecs)03d - %(message)s')
logging.warning('foo')

example.log:

273 - foo

Now using the the modern {} format syntax, as the documentation says (doesn't work)

image

import logging
logging.basicConfig(filename='example.log', format='{msecs:03d} - {message}', style='{')
logging.warning('foo')

Exception:

--- Logging error ---
Traceback (most recent call last):
  File "...\Python\Python310\lib\logging\__init__.py", line 1100, in emit
    msg = self.format(record)
  File "...\Python\Python310\lib\logging\__init__.py", line 943, in format
    return fmt.format(record)
  File "...\Python\Python310\lib\logging\__init__.py", line 681, in format
    s = self.formatMessage(record)
  File "...\Python\Python310\lib\logging\__init__.py", line 650, in formatMessage
    return self._style.format(record)
  File "...\Python\Python310\lib\logging\__init__.py", line 440, in format
    return self._format(record)
  File "...\Python\Python310\lib\logging\__init__.py", line 458, in _format
    return self._fmt.format(**values)
ValueError: Unknown format code 'd' for object of type 'float'
Call stack:
  File "...\main.py", line 26, in <module>
    logging.warning('foo')
Message: 'foo'
Arguments: ()

Proposed fix

Use {msecs:03.0f} placeholder.

import logging
logging.basicConfig(filename='example.log', format='{msecs:03.0f} - {message}', style='{')
logging.warning('foo')

example.log:

273 - foo

Linked PRs

@AlberLC AlberLC added the docs Documentation in the Doc dir label Sep 15, 2023
@sobolevn
Copy link
Member

Please, send a PR :)

@sobolevn sobolevn added the easy label Sep 15, 2023
AlberLC added a commit to AlberLC/cpython that referenced this issue Sep 15, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 16, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 16, 2023
@AlberLC AlberLC closed this as completed Sep 16, 2023
Yhg1s pushed a commit that referenced this issue Sep 17, 2023
…GH-109465) (#109483)

gh-109451: Fix wrong format specifier in logging documentation (GH-109465)
(cherry picked from commit 929cc4e)

Co-authored-by: AlberLC <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants