Skip to content

bad logging documentation {}-formatting placeholder #109451

Closed
@AlberLC

Description

@AlberLC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc direasy

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions