| 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="node358.html" /> |
| 13 | <link rel="prev" href="node345.html" /> |
| 14 | <link rel="parent" href="module-logging.html" /> |
| 15 | <link rel="next" href="node358.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.29.6 Formatter Objects</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.5.11 HTTPHandler" |
| 25 | href="node356.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.7 Filter Objects" |
| 31 | href="node358.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="node356.html">6.29.5.11 HTTPHandler</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="node358.html">6.29.7 Filter Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0082960000000000000000"> |
| 56 | 6.29.6 Formatter Objects</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | <tt class="class">Formatter</tt>s have the following attributes and methods. They are |
| 61 | responsible for converting a <tt class="class">LogRecord</tt> to (usually) a string |
| 62 | which can be interpreted by either a human or an external system. The |
| 63 | base |
| 64 | <tt class="class">Formatter</tt> allows a formatting string to be specified. If none is |
| 65 | supplied, the default value of <code>'%(message)s'</code> is used. |
| 66 | |
| 67 | <P> |
| 68 | A Formatter can be initialized with a format string which makes use of |
| 69 | knowledge of the <tt class="class">LogRecord</tt> attributes - such as the default value |
| 70 | mentioned above making use of the fact that the user's message and |
| 71 | arguments are pre-formatted into a <tt class="class">LogRecord</tt>'s <var>message</var> |
| 72 | attribute. This format string contains standard python %-style |
| 73 | mapping keys. See section <A href="typesseq-strings.html#typesseq-strings">2.3.6</A>, ``String Formatting |
| 74 | Operations,'' for more information on string formatting. |
| 75 | |
| 76 | <P> |
| 77 | Currently, the useful mapping keys in a <tt class="class">LogRecord</tt> are: |
| 78 | |
| 79 | <P> |
| 80 | <div class="center"><table class="realtable"> |
| 81 | <thead> |
| 82 | <tr> |
| 83 | <th class="left" >Format</th> |
| 84 | <th class="left" >Description</th> |
| 85 | </tr> |
| 86 | </thead> |
| 87 | <tbody> |
| 88 | <tr><td class="left" valign="baseline"><code>%(name)s</code></td> |
| 89 | <td class="left" >Name of the logger (logging channel).</td></tr> |
| 90 | <tr><td class="left" valign="baseline"><code>%(levelno)s</code></td> |
| 91 | <td class="left" >Numeric logging level for the message |
| 92 | (<tt class="constant">DEBUG</tt>, <tt class="constant">INFO</tt>, |
| 93 | <tt class="constant">WARNING</tt>, <tt class="constant">ERROR</tt>, |
| 94 | <tt class="constant">CRITICAL</tt>).</td></tr> |
| 95 | <tr><td class="left" valign="baseline"><code>%(levelname)s</code></td> |
| 96 | <td class="left" >Text logging level for the message |
| 97 | (<code>'DEBUG'</code>, <code>'INFO'</code>, |
| 98 | <code>'WARNING'</code>, <code>'ERROR'</code>, |
| 99 | <code>'CRITICAL'</code>).</td></tr> |
| 100 | <tr><td class="left" valign="baseline"><code>%(pathname)s</code></td> |
| 101 | <td class="left" >Full pathname of the source file where the logging |
| 102 | call was issued (if available).</td></tr> |
| 103 | <tr><td class="left" valign="baseline"><code>%(filename)s</code></td> |
| 104 | <td class="left" >Filename portion of pathname.</td></tr> |
| 105 | <tr><td class="left" valign="baseline"><code>%(module)s</code></td> |
| 106 | <td class="left" >Module (name portion of filename).</td></tr> |
| 107 | <tr><td class="left" valign="baseline"><code>%(lineno)d</code></td> |
| 108 | <td class="left" >Source line number where the logging call was issued |
| 109 | (if available).</td></tr> |
| 110 | <tr><td class="left" valign="baseline"><code>%(created)f</code></td> |
| 111 | <td class="left" >Time when the <tt class="class">LogRecord</tt> was created (as |
| 112 | returned by <tt class="function">time.time()</tt>).</td></tr> |
| 113 | <tr><td class="left" valign="baseline"><code>%(asctime)s</code></td> |
| 114 | <td class="left" >Human-readable time when the <tt class="class">LogRecord</tt> |
| 115 | was created. By default this is of the form |
| 116 | ``2003-07-08 16:49:45,896'' (the numbers after the |
| 117 | comma are millisecond portion of the time).</td></tr> |
| 118 | <tr><td class="left" valign="baseline"><code>%(msecs)d</code></td> |
| 119 | <td class="left" >Millisecond portion of the time when the |
| 120 | <tt class="class">LogRecord</tt> was created.</td></tr> |
| 121 | <tr><td class="left" valign="baseline"><code>%(thread)d</code></td> |
| 122 | <td class="left" >Thread ID (if available).</td></tr> |
| 123 | <tr><td class="left" valign="baseline"><code>%(threadName)s</code></td> |
| 124 | <td class="left" >Thread name (if available).</td></tr> |
| 125 | <tr><td class="left" valign="baseline"><code>%(process)d</code></td> |
| 126 | <td class="left" >Process ID (if available).</td></tr> |
| 127 | <tr><td class="left" valign="baseline"><code>%(message)s</code></td> |
| 128 | <td class="left" >The logged message, computed as <code>msg % args</code>.</td></tr></tbody> |
| 129 | </table></div> |
| 130 | |
| 131 | <P> |
| 132 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 133 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-2555' xml:id='l2h-2555' class="class">Formatter</tt></b>(</nobr></td> |
| 134 | <td><var></var><big>[</big><var>fmt</var><big>[</big><var>, datefmt</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 135 | <dd> |
| 136 | Returns a new instance of the <tt class="class">Formatter</tt> class. The |
| 137 | instance is initialized with a format string for the message as a whole, |
| 138 | as well as a format string for the date/time portion of a message. If |
| 139 | no <var>fmt</var> is specified, <code>'%(message)s'</code> is used. If no <var>datefmt</var> |
| 140 | is specified, the ISO8601 date format is used. |
| 141 | </dl> |
| 142 | |
| 143 | <P> |
| 144 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 145 | <td><nobr><b><tt id='l2h-2556' xml:id='l2h-2556' class="method">format</tt></b>(</nobr></td> |
| 146 | <td><var>record</var>)</td></tr></table></dt> |
| 147 | <dd> |
| 148 | The record's attribute dictionary is used as the operand to a |
| 149 | string formatting operation. Returns the resulting string. |
| 150 | Before formatting the dictionary, a couple of preparatory steps |
| 151 | are carried out. The <var>message</var> attribute of the record is computed |
| 152 | using <var>msg</var> % <var>args</var>. If the formatting string contains |
| 153 | <code>'(asctime)'</code>, <tt class="method">formatTime()</tt> is called to format the |
| 154 | event time. If there is exception information, it is formatted using |
| 155 | <tt class="method">formatException()</tt> and appended to the message. |
| 156 | </dl> |
| 157 | |
| 158 | <P> |
| 159 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 160 | <td><nobr><b><tt id='l2h-2557' xml:id='l2h-2557' class="method">formatTime</tt></b>(</nobr></td> |
| 161 | <td><var>record</var><big>[</big><var>, datefmt</var><big>]</big><var></var>)</td></tr></table></dt> |
| 162 | <dd> |
| 163 | This method should be called from <tt class="method">format()</tt> by a formatter which |
| 164 | wants to make use of a formatted time. This method can be overridden |
| 165 | in formatters to provide for any specific requirement, but the |
| 166 | basic behavior is as follows: if <var>datefmt</var> (a string) is specified, |
| 167 | it is used with <tt class="function">time.strftime()</tt> to format the creation time of the |
| 168 | record. Otherwise, the ISO8601 format is used. The resulting |
| 169 | string is returned. |
| 170 | </dl> |
| 171 | |
| 172 | <P> |
| 173 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 174 | <td><nobr><b><tt id='l2h-2558' xml:id='l2h-2558' class="method">formatException</tt></b>(</nobr></td> |
| 175 | <td><var>exc_info</var>)</td></tr></table></dt> |
| 176 | <dd> |
| 177 | Formats the specified exception information (a standard exception tuple |
| 178 | as returned by <tt class="function">sys.exc_info()</tt>) as a string. This default |
| 179 | implementation just uses <tt class="function">traceback.print_exception()</tt>. |
| 180 | The resulting string is returned. |
| 181 | </dl> |
| 182 | |
| 183 | <P> |
| 184 | |
| 185 | <DIV CLASS="navigation"> |
| 186 | <div class='online-navigation'> |
| 187 | <p></p><hr /> |
| 188 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 189 | <tr> |
| 190 | <td class='online-navigation'><a rel="prev" title="6.29.5.11 HTTPHandler" |
| 191 | href="node356.html"><img src='../icons/previous.png' |
| 192 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 193 | <td class='online-navigation'><a rel="parent" title="6.29 logging " |
| 194 | href="module-logging.html"><img src='../icons/up.png' |
| 195 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 196 | <td class='online-navigation'><a rel="next" title="6.29.7 Filter Objects" |
| 197 | href="node358.html"><img src='../icons/next.png' |
| 198 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 199 | <td align="center" width="100%">Python Library Reference</td> |
| 200 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 201 | href="contents.html"><img src='../icons/contents.png' |
| 202 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 203 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 204 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 205 | <td class='online-navigation'><a rel="index" title="Index" |
| 206 | href="genindex.html"><img src='../icons/index.png' |
| 207 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 208 | </tr></table> |
| 209 | <div class='online-navigation'> |
| 210 | <b class="navlabel">Previous:</b> |
| 211 | <a class="sectref" rel="prev" href="node356.html">6.29.5.11 HTTPHandler</A> |
| 212 | <b class="navlabel">Up:</b> |
| 213 | <a class="sectref" rel="parent" href="module-logging.html">6.29 logging </A> |
| 214 | <b class="navlabel">Next:</b> |
| 215 | <a class="sectref" rel="next" href="node358.html">6.29.7 Filter Objects</A> |
| 216 | </div> |
| 217 | </div> |
| 218 | <hr /> |
| 219 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 220 | </DIV> |
| 221 | <!--End of Navigation Panel--> |
| 222 | <ADDRESS> |
| 223 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 224 | </ADDRESS> |
| 225 | </BODY> |
| 226 | </HTML> |