summaryrefslogtreecommitdiffstats
path: root/Doc/howto
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2014-01-17 18:36:41 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2014-01-17 18:36:41 (GMT)
commit5714e815c965b9a66bb6f9a3808b5ef25f7f25d8 (patch)
tree53f564dee5ca0296203bf0cc566d48ee2e84d0b5 /Doc/howto
parent24f60b46168d6ebee74cd145cca16926e63506de (diff)
parenteb14deca15673b77deb6f5d6dc54d3dd6a4cfb99 (diff)
downloadcpython-5714e815c965b9a66bb6f9a3808b5ef25f7f25d8.zip
cpython-5714e815c965b9a66bb6f9a3808b5ef25f7f25d8.tar.gz
cpython-5714e815c965b9a66bb6f9a3808b5ef25f7f25d8.tar.bz2
Merged documentation update from 3.3.
Diffstat (limited to 'Doc/howto')
-rw-r--r--Doc/howto/logging-cookbook.rst28
1 files changed, 28 insertions, 0 deletions
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index b37222d..93b1b8a 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -1918,3 +1918,31 @@ something, you can make it more palatable if you use an alias such as ``M`` or
``_`` for the message (or perhaps ``__``, if you are using ``_`` for
localization).
+Examples of this approach are given below. Firstly, formatting with
+:meth:`str.format`::
+
+ >>> __ = BraceMessage
+ >>> print(__('Message with {0} {1}', 2, 'placeholders'))
+ Message with 2 placeholders
+ >>> class Point: pass
+ ...
+ >>> p = Point()
+ >>> p.x = 0.5
+ >>> p.y = 0.5
+ >>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})', point=p))
+ Message with coordinates: (0.50, 0.50)
+
+Secondly, formatting with :class:`string.Template`::
+
+ >>> __ = DollarMessage
+ >>> print(__('Message with $num $what', num=2, what='placeholders'))
+ Message with 2 placeholders
+ >>>
+
+One thing to note is that you pay no significant performance penalty with this
+approach: the actual formatting happens not when you make the logging call, but
+when (and if) the logged message is actually about to be output to a log by a
+handler. So the only slightly unusual thing which might trip you up is that the
+parentheses go around the format string and the arguments, not just the format
+string. That’s because the __ notation is just syntax sugar for a constructor
+call to one of the ``XXXMessage`` classes shown above.