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

Skip to content

Commit 39b83ac

Browse files
committed
Improved logging cookbook example.
1 parent e998386 commit 39b83ac

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Doc/howto/logging-cookbook.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -972,12 +972,13 @@ Use of alternative formatting styles
972972
When logging was added to the Python standard library, the only way of
973973
formatting messages with variable content was to use the %-formatting
974974
method. Since then, Python has gained two new formatting approaches:
975-
string.Template (added in Python 2.4) and str.format (added in Python 2.6).
975+
:class:`string.Template` (added in Python 2.4) and :meth:`str.format`
976+
(added in Python 2.6).
976977

977-
Logging now (as of 3.2) provides improved support for these two additional
978-
formatting styles. The :class:`Formatter` class been enhanced for Python 3.2 to
979-
take an additional, optional keyword parameter named ``style``. This defaults
980-
to ``'%'``, but other possible values are ``'{'`` and ``'$'``, which correspond
978+
Logging (as of 3.2) provides improved support for these two additional
979+
formatting styles. The :class:`Formatter` class been enhanced to take an
980+
additional, optional keyword parameter named ``style``. This defaults to
981+
``'%'``, but other possible values are ``'{'`` and ``'$'``, which correspond
981982
to the other two formatting styles. Backwards compatibility is maintained by
982983
default (as you would expect), but by explicitly specifying a style parameter,
983984
you get the ability to specify format strings which work with
@@ -1068,7 +1069,7 @@ they're declared in a module called ``wherever``):
10681069
.. code-block:: pycon
10691070
10701071
>>> from wherever import BraceMessage as __
1071-
>>> print(__('Message with {0} {1}', 2, 'placeholders'))
1072+
>>> print(__('Message with {0} {name}', 2, name='placeholders'))
10721073
Message with 2 placeholders
10731074
>>> class Point: pass
10741075
...
@@ -1083,6 +1084,10 @@ they're declared in a module called ``wherever``):
10831084
Message with 2 placeholders
10841085
>>>
10851086
1087+
While the above examples use ``print()`` to show how the formatting works, you
1088+
would of course use ``logger.debug()`` or similar to actually log using this
1089+
approach.
1090+
10861091
One thing to note is that you pay no significant performance penalty with this
10871092
approach: the actual formatting happens not when you make the logging call, but
10881093
when (and if) the logged message is actually about to be output to a log by a

0 commit comments

Comments
 (0)