@@ -414,6 +414,110 @@ \section{PEP 279: The \function{enumerate()} Built-in Function\label{section-enu
414414\end {seealso }
415415
416416
417+ % ======================================================================
418+ \section {PEP 282: The \module {logging} Package }
419+
420+ A standard package for writing logs, the \module {logging} package, was
421+ added. It provides a powerful and flexible way for components to
422+ generate logging output which can then be filtered and processed in
423+ various ways. The logging system can parse a configuration file to
424+ control its behaviour. Logs can be written to standard error, a file
425+ or a socket, sent to the system log, e-mailed to a particular address,
426+ or buffered in memory. It's also possible to write your own handler
427+ classes, of course.
428+
429+ You can have multiple \class {Logger} objects, each one used by a
430+ particular subsystem of your code. Each \class {Logger} is identified
431+ by a name, and names are organized into a hierarchy using \samp {.} as
432+ the component separator. For example, you might have \class {Logger}
433+ instances named \samp {server}, \samp {server.auth} and
434+ \samp {server.network}. The latter two instances fall under the
435+ \samp {server} \class {Logger} in the hierarchy. This means that if you
436+ turn up the verbosity for \samp {server}, or direct
437+ \samp {server} messages to a different handler,
438+ the changes will also apply to \samp {server.auth} and
439+ \samp {server.network}.
440+ There's also a root \class {Logger} with the name \samp {root},
441+ parent of all other instances.
442+
443+ The \module {logging} package contains some convenience functions
444+ that always use the root log:
445+
446+ \begin {verbatim }
447+ import logging
448+
449+ logging.debug('Debugging information')
450+ logging.info('Informational message')
451+ logging.warn('Warning: config file %s not found', 'server.conf')
452+ logging.error('Error occurred')
453+ logging.critical('Critical error -- shutting down')
454+ \end {verbatim }
455+
456+ This produces the following output:
457+
458+ \begin {verbatim }
459+ WARN:root:Warning: config file not found
460+ ERROR:root:Error occurred
461+ CRITICAL:root:Critical error -- shutting down
462+ \end {verbatim }
463+
464+ In the default configuration, informational and debugging messages are
465+ suppressed and the output is sent to standard error. Note the
466+ \function {warn()} call's use of string formatting operators; all of
467+ the functions for logging messages take the arguments
468+ \code {(\var {msg}, \var {arg1}, \var {arg2}, ...)} and log the string resulting from
469+ \code {\var {msg} \% (\var {arg1}, \var {arg2}, ...)}.
470+
471+ There's also an \function {exception()} function that records the most
472+ recent traceback. Any of the other functions will also record the
473+ traceback by specifying the keyword argument \code {exc_info} as
474+ \code {True}.
475+
476+ \begin {verbatim }
477+ def f():
478+ try: 1/0
479+ except: logging.exception('Problem recorded')
480+
481+ f()
482+ \end {verbatim }
483+
484+ This produces the following output:
485+
486+ \begin {verbatim }
487+ ERROR:root:Problem recorded
488+ Traceback (most recent call last):
489+ File "t.py", line 6, in f
490+ 1/0
491+ ZeroDivisionError: integer division or modulo by zero
492+ \end {verbatim }
493+
494+ The \function {getLogger(\var {name})} is used to get a particular log.
495+
496+ \begin {verbatim }
497+ log = logging.getLogger('server')
498+ ...
499+ log.info('Listening on port %i', port)
500+ ...
501+ log.critical('Disk full')
502+ ...
503+ \end {verbatim }
504+
505+ XXX finish this section
506+
507+ This is only a partial overview of the \module {logging} package's
508+ features; see the
509+ \citetitle [http://www.python.org/dev/doc/devel/lib/module-logging.html]{\module {logging }
510+ package's reference documentation} for all of the details.
511+
512+
513+ \begin {seealso }
514+
515+ \seepep {282}{A Logging System}{Written by Vinay Sajip and Trent Mick;
516+ implemented by Vinay Sajip.}
517+
518+ \end {seealso }
519+
520+
417521% ======================================================================
418522\section {PEP 285: The \class {bool} Type\label {section-bool } }
419523
@@ -684,13 +788,13 @@ \section{Other Language Changes}
684788{1: 2}
685789>>> d.pop(4)
686790Traceback (most recent call last):
687- File `` stdin'' , line 1, in ?
791+ File " stdin" , line 1, in ?
688792KeyError: 4
689793>>> d.pop(1)
6907942
691795>>> d.pop(1)
692796Traceback (most recent call last):
693- File `` stdin'' , line 1, in ?
797+ File " stdin" , line 1, in ?
694798KeyError: pop(): dictionary is empty
695799>>> d
696800{}
@@ -1019,9 +1123,9 @@ \section{New and Improved Modules}
10191123[4, 2, 3, 0, 5, 1]
10201124>>> random.sample(pop, 7) # Can't choose more than six
10211125Traceback (most recent call last):
1022- File `` <stdin>'' , line 1, in ?
1023- File ``/home/amk/src/sf/python/dist/src/Lib/ random.py'' , line 396, in sample
1024- raise ValueError, `` sample larger than population''
1126+ File " <stdin>" , line 1, in ?
1127+ File " random.py" , line 396, in sample
1128+ raise ValueError, " sample larger than population"
10251129ValueError: sample larger than population
10261130>>>
10271131\end {verbatim }
0 commit comments