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="multiple-destinations.html" /> | |
13 | <link rel="prev" href="node341.html" /> | |
14 | <link rel="parent" href="module-logging.html" /> | |
15 | <link rel="next" href="multiple-destinations.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.29.2 Basic example </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.1 Logger Objects" | |
25 | href="node341.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.3 Logging to multiple" | |
31 | href="multiple-destinations.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="node341.html">6.29.1 Logger Objects</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="multiple-destinations.html">6.29.3 Logging to multiple</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0082920000000000000000"></A><A NAME="minimal-example"></A> | |
56 | <BR> | |
57 | 6.29.2 Basic example | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | ||
62 | <span class="versionnote">Changed in version 2.4: | |
63 | formerly <tt class="function">basicConfig</tt> did not take any keyword | |
64 | arguments.</span> | |
65 | ||
66 | <P> | |
67 | The <tt class="module">logging</tt> package provides a lot of flexibility, and its | |
68 | configuration can appear daunting. This section demonstrates that simple | |
69 | use of the logging package is possible. | |
70 | ||
71 | <P> | |
72 | The simplest example shows logging to the console: | |
73 | ||
74 | <P> | |
75 | <div class="verbatim"><pre> | |
76 | import logging | |
77 | ||
78 | logging.debug('A debug message') | |
79 | logging.info('Some information') | |
80 | logging.warning('A shot across the bows') | |
81 | </pre></div> | |
82 | ||
83 | <P> | |
84 | If you run the above script, you'll see this: | |
85 | <div class="verbatim"><pre> | |
86 | WARNING:root:A shot across the bows | |
87 | </pre></div> | |
88 | ||
89 | <P> | |
90 | Because no particular logger was specified, the system used the root logger. | |
91 | The debug and info messages didn't appear because by default, the root | |
92 | logger is configured to only handle messages with a severity of WARNING | |
93 | or above. The message format is also a configuration default, as is the output | |
94 | destination of the messages - <code>sys.stderr</code>. The severity level, | |
95 | the message format and destination can be easily changed, as shown in | |
96 | the example below: | |
97 | ||
98 | <P> | |
99 | <div class="verbatim"><pre> | |
100 | import logging | |
101 | ||
102 | logging.basicConfig(level=logging.DEBUG, | |
103 | format='%(asctime)s %(levelname)s %(message)s', | |
104 | filename='/tmp/myapp.log', | |
105 | filemode='w') | |
106 | logging.debug('A debug message') | |
107 | logging.info('Some information') | |
108 | logging.warning('A shot across the bows') | |
109 | </pre></div> | |
110 | ||
111 | <P> | |
112 | The <tt class="method">basicConfig()</tt> method is used to change the configuration | |
113 | defaults, which results in output (written to <code>/tmp/myapp.log</code>) | |
114 | which should look something like the following: | |
115 | ||
116 | <P> | |
117 | <div class="verbatim"><pre> | |
118 | 2004-07-02 13:00:08,743 DEBUG A debug message | |
119 | 2004-07-02 13:00:08,743 INFO Some information | |
120 | 2004-07-02 13:00:08,743 WARNING A shot across the bows | |
121 | </pre></div> | |
122 | ||
123 | <P> | |
124 | This time, all messages with a severity of DEBUG or above were handled, | |
125 | and the format of the messages was also changed, and output went to the | |
126 | specified file rather than the console. | |
127 | ||
128 | <P> | |
129 | Formatting uses standard Python string formatting - see section | |
130 | <A href="typesseq-strings.html#typesseq-strings">2.3.6</A>. The format string takes the following | |
131 | common specifiers. For a complete list of specifiers, consult the | |
132 | <tt class="class">Formatter</tt> documentation. | |
133 | ||
134 | <P> | |
135 | <div class="center"><table class="realtable"> | |
136 | <thead> | |
137 | <tr> | |
138 | <th class="left" >Format</th> | |
139 | <th class="left" >Description</th> | |
140 | </tr> | |
141 | </thead> | |
142 | <tbody> | |
143 | <tr><td class="left" valign="baseline"><code>%(name)s</code></td> | |
144 | <td class="left" >Name of the logger (logging channel).</td></tr> | |
145 | <tr><td class="left" valign="baseline"><code>%(levelname)s</code></td> | |
146 | <td class="left" >Text logging level for the message | |
147 | (<code>'DEBUG'</code>, <code>'INFO'</code>, | |
148 | <code>'WARNING'</code>, <code>'ERROR'</code>, | |
149 | <code>'CRITICAL'</code>).</td></tr> | |
150 | <tr><td class="left" valign="baseline"><code>%(asctime)s</code></td> | |
151 | <td class="left" >Human-readable time when the <tt class="class">LogRecord</tt> | |
152 | was created. By default this is of the form | |
153 | ``2003-07-08 16:49:45,896'' (the numbers after the | |
154 | comma are millisecond portion of the time).</td></tr> | |
155 | <tr><td class="left" valign="baseline"><code>%(message)s</code></td> | |
156 | <td class="left" >The logged message.</td></tr></tbody> | |
157 | </table></div> | |
158 | ||
159 | <P> | |
160 | To change the date/time format, you can pass an additional keyword parameter, | |
161 | <var>datefmt</var>, as in the following: | |
162 | ||
163 | <P> | |
164 | <div class="verbatim"><pre> | |
165 | import logging | |
166 | ||
167 | logging.basicConfig(level=logging.DEBUG, | |
168 | format='%(asctime)s %(levelname)-8s %(message)s', | |
169 | datefmt='%a, %d %b %Y %H:%M:%S', | |
170 | filename='/temp/myapp.log', | |
171 | filemode='w') | |
172 | logging.debug('A debug message') | |
173 | logging.info('Some information') | |
174 | logging.warning('A shot across the bows') | |
175 | </pre></div> | |
176 | ||
177 | <P> | |
178 | which would result in output like | |
179 | ||
180 | <P> | |
181 | <div class="verbatim"><pre> | |
182 | Fri, 02 Jul 2004 13:06:18 DEBUG A debug message | |
183 | Fri, 02 Jul 2004 13:06:18 INFO Some information | |
184 | Fri, 02 Jul 2004 13:06:18 WARNING A shot across the bows | |
185 | </pre></div> | |
186 | ||
187 | <P> | |
188 | The date format string follows the requirements of <tt class="function">strftime()</tt> - | |
189 | see the documentation for the <tt class="module"><a href="module-time.html">time</a></tt> module. | |
190 | ||
191 | <P> | |
192 | If, instead of sending logging output to the console or a file, you'd rather | |
193 | use a file-like object which you have created separately, you can pass it | |
194 | to <tt class="function">basicConfig()</tt> using the <var>stream</var> keyword argument. Note | |
195 | that if both <var>stream</var> and <var>filename</var> keyword arguments are passed, | |
196 | the <var>stream</var> argument is ignored. | |
197 | ||
198 | <P> | |
199 | Of course, you can put variable information in your output. To do this, | |
200 | simply have the message be a format string and pass in additional arguments | |
201 | containing the variable information, as in the following example: | |
202 | ||
203 | <P> | |
204 | <div class="verbatim"><pre> | |
205 | import logging | |
206 | ||
207 | logging.basicConfig(level=logging.DEBUG, | |
208 | format='%(asctime)s %(levelname)-8s %(message)s', | |
209 | datefmt='%a, %d %b %Y %H:%M:%S', | |
210 | filename='/temp/myapp.log', | |
211 | filemode='w') | |
212 | logging.error('Pack my box with %d dozen %s', 5, 'liquor jugs') | |
213 | </pre></div> | |
214 | ||
215 | <P> | |
216 | which would result in | |
217 | ||
218 | <P> | |
219 | <div class="verbatim"><pre> | |
220 | Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs | |
221 | </pre></div> | |
222 | ||
223 | <P> | |
224 | ||
225 | <DIV CLASS="navigation"> | |
226 | <div class='online-navigation'> | |
227 | <p></p><hr /> | |
228 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
229 | <tr> | |
230 | <td class='online-navigation'><a rel="prev" title="6.29.1 Logger Objects" | |
231 | href="node341.html"><img src='../icons/previous.png' | |
232 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
233 | <td class='online-navigation'><a rel="parent" title="6.29 logging " | |
234 | href="module-logging.html"><img src='../icons/up.png' | |
235 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
236 | <td class='online-navigation'><a rel="next" title="6.29.3 Logging to multiple" | |
237 | href="multiple-destinations.html"><img src='../icons/next.png' | |
238 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
239 | <td align="center" width="100%">Python Library Reference</td> | |
240 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
241 | href="contents.html"><img src='../icons/contents.png' | |
242 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
243 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
244 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
245 | <td class='online-navigation'><a rel="index" title="Index" | |
246 | href="genindex.html"><img src='../icons/index.png' | |
247 | border='0' height='32' alt='Index' width='32' /></A></td> | |
248 | </tr></table> | |
249 | <div class='online-navigation'> | |
250 | <b class="navlabel">Previous:</b> | |
251 | <a class="sectref" rel="prev" href="node341.html">6.29.1 Logger Objects</A> | |
252 | <b class="navlabel">Up:</b> | |
253 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> | |
254 | <b class="navlabel">Next:</b> | |
255 | <a class="sectref" rel="next" href="multiple-destinations.html">6.29.3 Logging to multiple</A> | |
256 | </div> | |
257 | </div> | |
258 | <hr /> | |
259 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
260 | </DIV> | |
261 | <!--End of Navigation Panel--> | |
262 | <ADDRESS> | |
263 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
264 | </ADDRESS> | |
265 | </BODY> | |
266 | </HTML> |