<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"node358.html" />
<link rel=
"prev" href=
"node345.html" />
<link rel=
"parent" href=
"module-logging.html" />
<link rel=
"next" href=
"node358.html" />
<meta name='aesop' content='information'
/>
<title>6.29.6 Formatter Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.29.5.11 HTTPHandler"
href=
"node356.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.29 logging "
href=
"module-logging.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.29.7 Filter Objects"
href=
"node358.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node356.html">6.29.5.11 HTTPHandler
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-logging.html">6.29 logging
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node358.html">6.29.7 Filter Objects
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0082960000000000000000">
6.29.6 Formatter Objects
</A>
<tt class=
"class">Formatter
</tt>s have the following attributes and methods. They are
responsible for converting a
<tt class=
"class">LogRecord
</tt> to (usually) a string
which can be interpreted by either a human or an external system. The
<tt class=
"class">Formatter
</tt> allows a formatting string to be specified. If none is
supplied, the default value of
<code>'%(message)s'
</code> is used.
A Formatter can be initialized with a format string which makes use of
knowledge of the
<tt class=
"class">LogRecord
</tt> attributes - such as the default value
mentioned above making use of the fact that the user's message and
arguments are pre-formatted into a
<tt class=
"class">LogRecord
</tt>'s
<var>message
</var>
attribute. This format string contains standard python %-style
mapping keys. See section
<A href=
"typesseq-strings.html#typesseq-strings">2.3.6</A>, ``String Formatting
Operations,'' for more information on string formatting.
Currently, the useful mapping keys in a
<tt class=
"class">LogRecord
</tt> are:
<div class=
"center"><table class=
"realtable">
<th class=
"left" >Format
</th>
<th class=
"left" >Description
</th>
<tr><td class=
"left" valign=
"baseline"><code>%(name)s
</code></td>
<td class=
"left" >Name of the logger (logging channel).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(levelno)s
</code></td>
<td class=
"left" >Numeric logging level for the message
(
<tt class=
"constant">DEBUG
</tt>,
<tt class=
"constant">INFO
</tt>,
<tt class=
"constant">WARNING
</tt>,
<tt class=
"constant">ERROR
</tt>,
<tt class=
"constant">CRITICAL
</tt>).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(levelname)s
</code></td>
<td class=
"left" >Text logging level for the message
(
<code>'DEBUG'
</code>,
<code>'INFO'
</code>,
<code>'WARNING'
</code>,
<code>'ERROR'
</code>,
<code>'CRITICAL'
</code>).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(pathname)s
</code></td>
<td class=
"left" >Full pathname of the source file where the logging
call was issued (if available).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(filename)s
</code></td>
<td class=
"left" >Filename portion of pathname.
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(module)s
</code></td>
<td class=
"left" >Module (name portion of filename).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(lineno)d
</code></td>
<td class=
"left" >Source line number where the logging call was issued
(if available).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(created)f
</code></td>
<td class=
"left" >Time when the
<tt class=
"class">LogRecord
</tt> was created (as
returned by
<tt class=
"function">time.time()
</tt>).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(asctime)s
</code></td>
<td class=
"left" >Human-readable time when the
<tt class=
"class">LogRecord
</tt>
was created. By default this is of the form
``
2003-
07-
08 16:
49:
45,
896'' (the numbers after the
comma are millisecond portion of the time).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(msecs)d
</code></td>
<td class=
"left" >Millisecond portion of the time when the
<tt class=
"class">LogRecord
</tt> was created.
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(thread)d
</code></td>
<td class=
"left" >Thread ID (if available).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(threadName)s
</code></td>
<td class=
"left" >Thread name (if available).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(process)d
</code></td>
<td class=
"left" >Process ID (if available).
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>%(message)s
</code></td>
<td class=
"left" >The logged message, computed as
<code>msg % args
</code>.
</td></tr></tbody>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
2555' xml:id='l2h-
2555'
class=
"class">Formatter
</tt></b>(
</nobr></td>
<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>
Returns a new instance of the
<tt class=
"class">Formatter
</tt> class. The
instance is initialized with a format string for the message as a whole,
as well as a format string for the date/time portion of a message. If
no
<var>fmt
</var> is specified,
<code>'%(message)s'
</code> is used. If no
<var>datefmt
</var>
is specified, the ISO8601 date format is used.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2556' xml:id='l2h-
2556'
class=
"method">format
</tt></b>(
</nobr></td>
<td><var>record
</var>)
</td></tr></table></dt>
The record's attribute dictionary is used as the operand to a
string formatting operation. Returns the resulting string.
Before formatting the dictionary, a couple of preparatory steps
are carried out. The
<var>message
</var> attribute of the record is computed
using
<var>msg
</var> %
<var>args
</var>. If the formatting string contains
<code>'(asctime)'
</code>,
<tt class=
"method">formatTime()
</tt> is called to format the
event time. If there is exception information, it is formatted using
<tt class=
"method">formatException()
</tt> and appended to the message.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2557' xml:id='l2h-
2557'
class=
"method">formatTime
</tt></b>(
</nobr></td>
<td><var>record
</var><big>[
</big><var>, datefmt
</var><big>]
</big><var></var>)
</td></tr></table></dt>
This method should be called from
<tt class=
"method">format()
</tt> by a formatter which
wants to make use of a formatted time. This method can be overridden
in formatters to provide for any specific requirement, but the
basic behavior is as follows: if
<var>datefmt
</var> (a string) is specified,
it is used with
<tt class=
"function">time.strftime()
</tt> to format the creation time of the
record. Otherwise, the ISO8601 format is used. The resulting
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2558' xml:id='l2h-
2558'
class=
"method">formatException
</tt></b>(
</nobr></td>
<td><var>exc_info
</var>)
</td></tr></table></dt>
Formats the specified exception information (a standard exception tuple
as returned by
<tt class=
"function">sys.exc_info()
</tt>) as a string. This default
implementation just uses
<tt class=
"function">traceback.print_exception()
</tt>.
The resulting string is returned.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.29.5.11 HTTPHandler"
href=
"node356.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.29 logging "
href=
"module-logging.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.29.7 Filter Objects"
href=
"node358.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node356.html">6.29.5.11 HTTPHandler
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-logging.html">6.29 logging
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node358.html">6.29.7 Filter Objects
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.