[Python-checkins] cpython (merge 3.3 -> default): Merged documentation update from 3.3.
vinay.sajip
python-checkins at python.org
Sat Mar 30 12:57:24 CET 2013
http://hg.python.org/cpython/rev/efb44458afcd
changeset: 83014:efb44458afcd
parent: 83011:d5d6209745ab
parent: 83013:7fd5d9fe9f71
user: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: Sat Mar 30 11:57:09 2013 +0000
summary:
Merged documentation update from 3.3.
files:
Doc/howto/logging-cookbook.rst | 34 ++++++++++++++++++++++
1 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -1094,6 +1094,40 @@
string. That's because the __ notation is just syntax sugar for a constructor
call to one of the XXXMessage classes.
+If you prefer, you can use a :class:`LoggerAdapter` to achieve a similar effect
+to the above, as in the following example::
+
+ import logging
+
+ class Message(object):
+ def __init__(self, fmt, args):
+ self.fmt = fmt
+ self.args = args
+
+ def __str__(self):
+ return self.fmt.format(*self.args)
+
+ class StyleAdapter(logging.LoggerAdapter):
+ def __init__(self, logger, extra=None):
+ super(StyleAdapter, self).__init__(logger, extra or {})
+
+ def log(self, level, msg, *args, **kwargs):
+ if self.isEnabledFor(level):
+ msg, kwargs = self.process(msg, kwargs)
+ self.logger._log(level, Message(msg, args), (), **kwargs)
+
+ logger = StyleAdapter(logging.getLogger(__name__))
+
+ def main():
+ logger.debug('Hello, {}', 'world!')
+
+ if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ main()
+
+The above script should log the message ``Hello, world!`` when run with
+Python 3.2 or later.
+
.. currentmodule:: logging
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list