@@ -507,15 +507,79 @@ \subsection{Basic example \label{minimal-example}}
507507 datefmt='%a, %d %b %Y %H:%M:%S',
508508 filename='/temp/myapp.log',
509509 filemode='w')
510- logging.error('Pack my box with %d dozen %s', 12 , 'liquor jugs')
510+ logging.error('Pack my box with %d dozen %s', 5 , 'liquor jugs')
511511\end {verbatim }
512512
513513which would result in
514514
515515\begin {verbatim }
516- Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 12 dozen liquor jugs
516+ Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs
517517\end {verbatim }
518518
519+ \subsection {Logging to multiple destinations \label {multiple-destinations } }
520+
521+ Let's say you want to log to console and file with different message formats
522+ and in differing circumstances. Say you want to log messages with levels
523+ of DEBUG and higher to file, and those messages at level INFO and higher to
524+ the console. Let's also assume that the file should contain timestamps, but
525+ the console messages should not. Here's how you can achieve this:
526+
527+ \begin {verbatim }
528+ import logging
529+
530+ #set up logging to file - see previous section for more details
531+ logging.basicConfig(level=logging.DEBUG,
532+ format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
533+ datefmt='%m-%d %H:%M',
534+ filename='/temp/myapp.log',
535+ filemode='w')
536+ #define a Handler which writes INFO messages or higher to the sys.stderr
537+ console = logging.StreamHandler()
538+ console.setLevel(logging.INFO)
539+ #set a format which is simpler for console use
540+ formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
541+ #tell the handler to use this format
542+ console.setFormatter(formatter)
543+ #add the handler to the root logger
544+ logging.getLogger('').addHandler(console)
545+
546+ #Now, we can log to the root logger, or any other logger. First the root...
547+ logging.info('Jackdaws love my big sphinx of quartz.')
548+
549+ #Now, define a couple of other loggers which might represent areas in your
550+ #application:
551+
552+ logger1 = logging.getLogger('myapp.area1')
553+ logger2 = logging.getLogger('myapp.area2')
554+
555+ logger1.debug('Quick zephyrs blow, vexing daft Jim.')
556+ logger1.info('How quickly daft jumping zebras vex.')
557+ logger2.warning('Jail zesty vixen who grabbed pay from quack.')
558+ logger2.error('The five boxing wizards jump quickly.')
559+ \end {verbatim }
560+
561+ When you run this, on the console you will see
562+
563+ \begin {verbatim }
564+ root : INFO Jackdaws love my big sphinx of quartz.
565+ myapp.area1 : INFO How quickly daft jumping zebras vex.
566+ myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.
567+ myapp.area2 : ERROR The five boxing wizards jump quickly.
568+ \end {verbatim }
569+
570+ and in the file you will see something like
571+
572+ \begin {verbatim }
573+ 10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
574+ 10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
575+ 10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
576+ 10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
577+ 10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
578+ \end {verbatim }
579+
580+ As you can see, the DEBUG message only shows up in the file. The other
581+ messages are sent to both destinations.
582+
519583\subsection {Handler Objects }
520584
521585Handlers have the following attributes and methods. Note that
0 commit comments