Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="network-logging.html" /> | |
13 | <link rel="prev" href="minimal-example.html" /> | |
14 | <link rel="parent" href="module-logging.html" /> | |
15 | <link rel="next" href="network-logging.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.29.3 Logging to multiple destinations </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.29.2 Basic example" | |
25 | href="minimal-example.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.29 logging " | |
28 | href="module-logging.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.29.4 Sending and receiving" | |
31 | href="network-logging.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="minimal-example.html">6.29.2 Basic example</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="network-logging.html">6.29.4 Sending and receiving</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0082930000000000000000"></A><A NAME="multiple-destinations"></A> | |
56 | <BR> | |
57 | 6.29.3 Logging to multiple destinations | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | Let's say you want to log to console and file with different message formats | |
62 | and in differing circumstances. Say you want to log messages with levels | |
63 | of DEBUG and higher to file, and those messages at level INFO and higher to | |
64 | the console. Let's also assume that the file should contain timestamps, but | |
65 | the console messages should not. Here's how you can achieve this: | |
66 | ||
67 | <P> | |
68 | <div class="verbatim"><pre> | |
69 | import logging | |
70 | ||
71 | # set up logging to file - see previous section for more details | |
72 | logging.basicConfig(level=logging.DEBUG, | |
73 | format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', | |
74 | datefmt='%m-%d %H:%M', | |
75 | filename='/temp/myapp.log', | |
76 | filemode='w') | |
77 | # define a Handler which writes INFO messages or higher to the sys.stderr | |
78 | console = logging.StreamHandler() | |
79 | console.setLevel(logging.INFO) | |
80 | # set a format which is simpler for console use | |
81 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') | |
82 | # tell the handler to use this format | |
83 | console.setFormatter(formatter) | |
84 | # add the handler to the root logger | |
85 | logging.getLogger('').addHandler(console) | |
86 | ||
87 | # Now, we can log to the root logger, or any other logger. First the root... | |
88 | logging.info('Jackdaws love my big sphinx of quartz.') | |
89 | ||
90 | # Now, define a couple of other loggers which might represent areas in your | |
91 | # application: | |
92 | ||
93 | logger1 = logging.getLogger('myapp.area1') | |
94 | logger2 = logging.getLogger('myapp.area2') | |
95 | ||
96 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') | |
97 | logger1.info('How quickly daft jumping zebras vex.') | |
98 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') | |
99 | logger2.error('The five boxing wizards jump quickly.') | |
100 | </pre></div> | |
101 | ||
102 | <P> | |
103 | When you run this, on the console you will see | |
104 | ||
105 | <P> | |
106 | <div class="verbatim"><pre> | |
107 | root : INFO Jackdaws love my big sphinx of quartz. | |
108 | myapp.area1 : INFO How quickly daft jumping zebras vex. | |
109 | myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack. | |
110 | myapp.area2 : ERROR The five boxing wizards jump quickly. | |
111 | </pre></div> | |
112 | ||
113 | <P> | |
114 | and in the file you will see something like | |
115 | ||
116 | <P> | |
117 | <div class="verbatim"><pre> | |
118 | 10-22 22:19 root INFO Jackdaws love my big sphinx of quartz. | |
119 | 10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. | |
120 | 10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex. | |
121 | 10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. | |
122 | 10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly. | |
123 | </pre></div> | |
124 | ||
125 | <P> | |
126 | As you can see, the DEBUG message only shows up in the file. The other | |
127 | messages are sent to both destinations. | |
128 | ||
129 | <P> | |
130 | This example uses console and file handlers, but you can use any number and | |
131 | combination of handlers you choose. | |
132 | ||
133 | <P> | |
134 | ||
135 | <DIV CLASS="navigation"> | |
136 | <div class='online-navigation'> | |
137 | <p></p><hr /> | |
138 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
139 | <tr> | |
140 | <td class='online-navigation'><a rel="prev" title="6.29.2 Basic example" | |
141 | href="minimal-example.html"><img src='../icons/previous.png' | |
142 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
143 | <td class='online-navigation'><a rel="parent" title="6.29 logging " | |
144 | href="module-logging.html"><img src='../icons/up.png' | |
145 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
146 | <td class='online-navigation'><a rel="next" title="6.29.4 Sending and receiving" | |
147 | href="network-logging.html"><img src='../icons/next.png' | |
148 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
149 | <td align="center" width="100%">Python Library Reference</td> | |
150 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
151 | href="contents.html"><img src='../icons/contents.png' | |
152 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
153 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
154 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
155 | <td class='online-navigation'><a rel="index" title="Index" | |
156 | href="genindex.html"><img src='../icons/index.png' | |
157 | border='0' height='32' alt='Index' width='32' /></A></td> | |
158 | </tr></table> | |
159 | <div class='online-navigation'> | |
160 | <b class="navlabel">Previous:</b> | |
161 | <a class="sectref" rel="prev" href="minimal-example.html">6.29.2 Basic example</A> | |
162 | <b class="navlabel">Up:</b> | |
163 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> | |
164 | <b class="navlabel">Next:</b> | |
165 | <a class="sectref" rel="next" href="network-logging.html">6.29.4 Sending and receiving</A> | |
166 | </div> | |
167 | </div> | |
168 | <hr /> | |
169 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
170 | </DIV> | |
171 | <!--End of Navigation Panel--> | |
172 | <ADDRESS> | |
173 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
174 | </ADDRESS> | |
175 | </BODY> | |
176 | </HTML> |