Skip to content

Commit 761eeb6

Browse files
authored
pythongh-94700: Rewrite the logging.Formatter API ref in structured form (pythonGH-94701)
1 parent 6442a9d commit 761eeb6

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

Doc/library/logging.rst

+41-43
Original file line numberDiff line numberDiff line change
@@ -531,55 +531,53 @@ Formatter Objects
531531

532532
.. currentmodule:: logging
533533

534-
:class:`Formatter` objects have the following attributes and methods. They are
535-
responsible for converting a :class:`LogRecord` to (usually) a string which can
536-
be interpreted by either a human or an external system. The base
537-
:class:`Formatter` allows a formatting string to be specified. If none is
538-
supplied, the default value of ``'%(message)s'`` is used, which just includes
539-
the message in the logging call. To have additional items of information in the
540-
formatted output (such as a timestamp), keep reading.
541-
542-
A Formatter can be initialized with a format string which makes use of knowledge
543-
of the :class:`LogRecord` attributes - such as the default value mentioned above
544-
making use of the fact that the user's message and arguments are pre-formatted
545-
into a :class:`LogRecord`'s *message* attribute. This format string contains
546-
standard Python %-style mapping keys. See section :ref:`old-string-formatting`
547-
for more information on string formatting.
548-
549-
The useful mapping keys in a :class:`LogRecord` are given in the section on
550-
:ref:`logrecord-attributes`.
551-
552-
553534
.. class:: Formatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)
554535

555-
Returns a new instance of the :class:`Formatter` class. The instance is
556-
initialized with a format string for the message as a whole, as well as a
557-
format string for the date/time portion of a message. If no *fmt* is
558-
specified, ``'%(message)s'`` is used. If no *datefmt* is specified, a format
559-
is used which is described in the :meth:`formatTime` documentation.
536+
Responsible for converting a :class:`LogRecord` to an output string
537+
to be interpreted by a human or external system.
538+
539+
:param fmt: A format string in the given *style* for
540+
the logged output as a whole.
541+
The possible mapping keys are drawn from the :class:`LogRecord` object's
542+
:ref:`logrecord-attributes`.
543+
If not specified, ``'%(message)s'`` is used,
544+
which is just the logged message.
545+
:type fmt: str
546+
547+
:param datefmt: A format string in the given *style* for
548+
the date/time portion of the logged output.
549+
If not specified, the default described in :meth:`formatTime` is used.
550+
:type datefmt: str
551+
552+
:param style: Can be one of ``'%'``, ``'{'`` or ``'$'`` and determines
553+
how the format string will be merged with its data: using one of
554+
:ref:`old-string-formatting` (``%``), :meth:`str.format` (``{``)
555+
or :class:`string.Template` (``$``). This only applies to
556+
*fmt* and *datefmt* (e.g. ``'%(message)s'`` versus ``'{message}'``),
557+
not to the actual log messages passed to the logging methods.
558+
However, there are :ref:`other ways <formatting-styles>`
559+
to use ``{``- and ``$``-formatting for log messages.
560+
:type style: str
561+
562+
:param validate: If ``True`` (the default), incorrect or mismatched
563+
*fmt* and *style* will raise a :exc:`ValueError`; for example,
564+
``logging.Formatter('%(asctime)s - %(message)s', style='{')``.
565+
:type validate: bool
566+
567+
:param defaults: A dictionary with default values to use in custom fields.
568+
For example,
569+
``logging.Formatter('%(ip)s %(message)s', defaults={"ip": None})``
570+
:type defaults: dict[str, Any]
560571

561-
The *style* parameter can be one of '%', '{' or '$' and determines how
562-
the format string will be merged with its data: using one of %-formatting,
563-
:meth:`str.format` or :class:`string.Template`. This only applies to the
564-
format string *fmt* (e.g. ``'%(message)s'`` or ``{message}``), not to the
565-
actual log messages passed to ``Logger.debug`` etc; see
566-
:ref:`formatting-styles` for more information on using {- and $-formatting
567-
for log messages.
568-
569-
The *defaults* parameter can be a dictionary with default values to use in
570-
custom fields. For example:
571-
``logging.Formatter('%(ip)s %(message)s', defaults={"ip": None})``
572+
.. versionadded:: 3.2
573+
The *style* parameter.
572574

573-
.. versionchanged:: 3.2
574-
The *style* parameter was added.
575+
.. versionadded:: 3.8
576+
The *validate* parameter.
575577

576-
.. versionchanged:: 3.8
577-
The *validate* parameter was added. Incorrect or mismatched style and fmt
578-
will raise a ``ValueError``.
579-
For example: ``logging.Formatter('%(asctime)s - %(message)s', style='{')``.
578+
.. versionadded:: 3.10
579+
The *defaults* parameter.
580580

581-
.. versionchanged:: 3.10
582-
The *defaults* parameter was added.
583581

584582
.. method:: format(record)
585583

0 commit comments

Comments
 (0)