blob: 1fcd231c54ca3b53957739b041523688bb26400e [file] [log] [blame]
Skip Montanaro649698f2002-11-14 03:57:19 +00001\section{\module{logging} ---
2 Logging facility for Python}
3
Fred Drake9a5b6a62003-07-08 15:38:40 +00004\declaremodule{standard}{logging}
Skip Montanaro649698f2002-11-14 03:57:19 +00005
6% These apply to all modules, and may be given more than once:
7
8\moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00009\sectionauthor{Vinay Sajip}{vinay_sajip@red-dove.com}
Skip Montanaro649698f2002-11-14 03:57:19 +000010
Fred Drake68e6d572003-01-28 22:02:35 +000011\modulesynopsis{Logging module for Python based on \pep{282}.}
Skip Montanaro649698f2002-11-14 03:57:19 +000012
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000013\indexii{Errors}{logging}
Skip Montanaro649698f2002-11-14 03:57:19 +000014
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000015\versionadded{2.3}
16This module defines functions and classes which implement a flexible
17error logging system for applications.
Skip Montanaro649698f2002-11-14 03:57:19 +000018
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000019Logging is performed by calling methods on instances of the
20\class{Logger} class (hereafter called \dfn{loggers}). Each instance has a
21name, and they are conceptually arranged in a name space hierarchy
22using dots (periods) as separators. For example, a logger named
23"scan" is the parent of loggers "scan.text", "scan.html" and "scan.pdf".
24Logger names can be anything you want, and indicate the area of an
25application in which a logged message originates.
Skip Montanaro649698f2002-11-14 03:57:19 +000026
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000027Logged messages also have levels of importance associated with them.
28The default levels provided are \constant{DEBUG}, \constant{INFO},
29\constant{WARNING}, \constant{ERROR} and \constant{CRITICAL}. As a
30convenience, you indicate the importance of a logged message by calling
31an appropriate method of \class{Logger}. The methods are
Fred Drakec23e0192003-01-28 22:09:16 +000032\method{debug()}, \method{info()}, \method{warning()}, \method{error()} and
33\method{critical()}, which mirror the default levels. You are not
34constrained to use these levels: you can specify your own and use a
35more general \class{Logger} method, \method{log()}, which takes an
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000036explicit level argument.
Skip Montanaro649698f2002-11-14 03:57:19 +000037
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000038Levels can also be associated with loggers, being set either by the
39developer or through loading a saved logging configuration. When a
40logging method is called on a logger, the logger compares its own
41level with the level associated with the method call. If the logger's
42level is higher than the method call's, no logging message is actually
43generated. This is the basic mechanism controlling the verbosity of
44logging output.
Skip Montanaro649698f2002-11-14 03:57:19 +000045
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000046Logging messages are encoded as instances of the \class{LogRecord} class.
47When a logger decides to actually log an event, an \class{LogRecord}
48instance is created from the logging message.
Skip Montanaro649698f2002-11-14 03:57:19 +000049
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000050Logging messages are subjected to a dispatch mechanism through the
51use of \dfn{handlers}, which are instances of subclasses of the
52\class{Handler} class. Handlers are responsible for ensuring that a logged
53message (in the form of a \class{LogRecord}) ends up in a particular
54location (or set of locations) which is useful for the target audience for
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +000055that message (such as end users, support desk staff, system administrators,
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000056developers). Handlers are passed \class{LogRecord} instances intended for
57particular destinations. Each logger can have zero, one or more handlers
Fred Drake6b3b0462004-04-09 18:26:40 +000058associated with it (via the \method{addHandler()} method of \class{Logger}).
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000059In addition to any handlers directly associated with a logger,
Fred Drakec23e0192003-01-28 22:09:16 +000060\emph{all handlers associated with all ancestors of the logger} are
61called to dispatch the message.
Skip Montanaro649698f2002-11-14 03:57:19 +000062
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000063Just as for loggers, handlers can have levels associated with them.
64A handler's level acts as a filter in the same way as a logger's level does.
Fred Drakec23e0192003-01-28 22:09:16 +000065If a handler decides to actually dispatch an event, the \method{emit()} method
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000066is used to send the message to its destination. Most user-defined subclasses
Fred Drakec23e0192003-01-28 22:09:16 +000067of \class{Handler} will need to override this \method{emit()}.
Skip Montanaro649698f2002-11-14 03:57:19 +000068
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000069In addition to the base \class{Handler} class, many useful subclasses
70are provided:
Skip Montanaro649698f2002-11-14 03:57:19 +000071
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000072\begin{enumerate}
Skip Montanaro649698f2002-11-14 03:57:19 +000073
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000074\item \class{StreamHandler} instances send error messages to
75streams (file-like objects).
Skip Montanaro649698f2002-11-14 03:57:19 +000076
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000077\item \class{FileHandler} instances send error messages to disk
78files.
79
80\item \class{RotatingFileHandler} instances send error messages to disk
81files, with support for maximum log file sizes and log file rotation.
82
83\item \class{SocketHandler} instances send error messages to
84TCP/IP sockets.
85
86\item \class{DatagramHandler} instances send error messages to UDP
87sockets.
88
89\item \class{SMTPHandler} instances send error messages to a
90designated email address.
91
92\item \class{SysLogHandler} instances send error messages to a
Fred Drake68e6d572003-01-28 22:02:35 +000093\UNIX{} syslog daemon, possibly on a remote machine.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000094
95\item \class{NTEventLogHandler} instances send error messages to a
96Windows NT/2000/XP event log.
97
98\item \class{MemoryHandler} instances send error messages to a
99buffer in memory, which is flushed whenever specific criteria are
100met.
101
102\item \class{HTTPHandler} instances send error messages to an
Fred Drake68e6d572003-01-28 22:02:35 +0000103HTTP server using either \samp{GET} or \samp{POST} semantics.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000104
105\end{enumerate}
106
107The \class{StreamHandler} and \class{FileHandler} classes are defined
108in the core logging package. The other handlers are defined in a sub-
109module, \module{logging.handlers}. (There is also another sub-module,
110\module{logging.config}, for configuration functionality.)
111
112Logged messages are formatted for presentation through instances of the
113\class{Formatter} class. They are initialized with a format string
114suitable for use with the \% operator and a dictionary.
115
116For formatting multiple messages in a batch, instances of
117\class{BufferingFormatter} can be used. In addition to the format string
118(which is applied to each message in the batch), there is provision for
119header and trailer format strings.
120
121When filtering based on logger level and/or handler level is not enough,
122instances of \class{Filter} can be added to both \class{Logger} and
Fred Drakec23e0192003-01-28 22:09:16 +0000123\class{Handler} instances (through their \method{addFilter()} method).
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000124Before deciding to process a message further, both loggers and handlers
125consult all their filters for permission. If any filter returns a false
126value, the message is not processed further.
127
128The basic \class{Filter} functionality allows filtering by specific logger
129name. If this feature is used, messages sent to the named logger and its
130children are allowed through the filter, and all others dropped.
131
132In addition to the classes described above, there are a number of module-
133level functions.
134
135\begin{funcdesc}{getLogger}{\optional{name}}
136Return a logger with the specified name or, if no name is specified, return
Vinay Sajip17952b72004-08-31 10:21:51 +0000137a logger which is the root logger of the hierarchy. If specified, the name
138is typically a dot-separated hierarchical name like \var{"a"}, \var{"a.b"}
139or \var{"a.b.c.d"}. Choice of these names is entirely up to the developer
140who is using logging.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000141
142All calls to this function with a given name return the same logger instance.
143This means that logger instances never need to be passed between different
144parts of an application.
Skip Montanaro649698f2002-11-14 03:57:19 +0000145\end{funcdesc}
146
Vinay Sajipc6646c02004-09-22 12:55:16 +0000147\begin{funcdesc}{getLoggerClass}{}
148Return either the standard \class{Logger} class, or the last class passed to
149\function{setLoggerClass()}. This function may be called from within a new
150class definition, to ensure that installing a customised \class{Logger} class
151will not undo customisations already applied by other code. For example:
152
153\begin{verbatim}
154 class MyLogger(logging.getLoggerClass()):
155 # ... override behaviour here
156\end{verbatim}
157
158\end{funcdesc}
159
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000160\begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
161Logs a message with level \constant{DEBUG} on the root logger.
162The \var{msg} is the message format string, and the \var{args} are the
163arguments which are merged into \var{msg}. The only keyword argument in
164\var{kwargs} which is inspected is \var{exc_info} which, if it does not
Vinay Sajip1dc5b1e2004-10-03 19:10:05 +0000165evaluate as false, causes exception information to be added to the logging
166message. If an exception tuple (in the format returned by
167\function{sys.exc_info()}) is provided, it is used; otherwise,
168\function{sys.exc_info()} is called to get the exception information.
Skip Montanaro649698f2002-11-14 03:57:19 +0000169\end{funcdesc}
170
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000171\begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
172Logs a message with level \constant{INFO} on the root logger.
173The arguments are interpreted as for \function{debug()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000174\end{funcdesc}
175
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000176\begin{funcdesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
177Logs a message with level \constant{WARNING} on the root logger.
178The arguments are interpreted as for \function{debug()}.
179\end{funcdesc}
180
181\begin{funcdesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
182Logs a message with level \constant{ERROR} on the root logger.
183The arguments are interpreted as for \function{debug()}.
184\end{funcdesc}
185
186\begin{funcdesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
187Logs a message with level \constant{CRITICAL} on the root logger.
188The arguments are interpreted as for \function{debug()}.
189\end{funcdesc}
190
191\begin{funcdesc}{exception}{msg\optional{, *args}}
192Logs a message with level \constant{ERROR} on the root logger.
193The arguments are interpreted as for \function{debug()}. Exception info
194is added to the logging message. This function should only be called
195from an exception handler.
196\end{funcdesc}
197
Vinay Sajip739d49e2004-09-24 11:46:44 +0000198\begin{funcdesc}{log}{level, msg\optional{, *args\optional{, **kwargs}}}
199Logs a message with level \var{level} on the root logger.
200The other arguments are interpreted as for \function{debug()}.
201\end{funcdesc}
202
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000203\begin{funcdesc}{disable}{lvl}
204Provides an overriding level \var{lvl} for all loggers which takes
205precedence over the logger's own level. When the need arises to
206temporarily throttle logging output down across the whole application,
207this function can be useful.
208\end{funcdesc}
209
210\begin{funcdesc}{addLevelName}{lvl, levelName}
211Associates level \var{lvl} with text \var{levelName} in an internal
212dictionary, which is used to map numeric levels to a textual
213representation, for example when a \class{Formatter} formats a message.
214This function can also be used to define your own levels. The only
215constraints are that all levels used must be registered using this
216function, levels should be positive integers and they should increase
217in increasing order of severity.
218\end{funcdesc}
219
220\begin{funcdesc}{getLevelName}{lvl}
221Returns the textual representation of logging level \var{lvl}. If the
222level is one of the predefined levels \constant{CRITICAL},
223\constant{ERROR}, \constant{WARNING}, \constant{INFO} or \constant{DEBUG}
224then you get the corresponding string. If you have associated levels
225with names using \function{addLevelName()} then the name you have associated
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000226with \var{lvl} is returned. If a numeric value corresponding to one of the
227defined levels is passed in, the corresponding string representation is
228returned. Otherwise, the string "Level \%s" \% lvl is returned.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000229\end{funcdesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000230
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000231\begin{funcdesc}{makeLogRecord}{attrdict}
232Creates and returns a new \class{LogRecord} instance whose attributes are
233defined by \var{attrdict}. This function is useful for taking a pickled
234\class{LogRecord} attribute dictionary, sent over a socket, and reconstituting
235it as a \class{LogRecord} instance at the receiving end.
236\end{funcdesc}
237
Skip Montanaro649698f2002-11-14 03:57:19 +0000238\begin{funcdesc}{basicConfig}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000239Does basic configuration for the logging system by creating a
240\class{StreamHandler} with a default \class{Formatter} and adding it to
241the root logger. The functions \function{debug()}, \function{info()},
242\function{warning()}, \function{error()} and \function{critical()} will call
243\function{basicConfig()} automatically if no handlers are defined for the
244root logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000245\end{funcdesc}
246
Skip Montanaro649698f2002-11-14 03:57:19 +0000247\begin{funcdesc}{shutdown}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000248Informs the logging system to perform an orderly shutdown by flushing and
249closing all handlers.
Skip Montanaro649698f2002-11-14 03:57:19 +0000250\end{funcdesc}
251
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000252\begin{funcdesc}{setLoggerClass}{klass}
253Tells the logging system to use the class \var{klass} when instantiating a
254logger. The class should define \method{__init__()} such that only a name
255argument is required, and the \method{__init__()} should call
256\method{Logger.__init__()}. This function is typically called before any
257loggers are instantiated by applications which need to use custom logger
258behavior.
259\end{funcdesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000260
Fred Drake68e6d572003-01-28 22:02:35 +0000261
262\begin{seealso}
263 \seepep{282}{A Logging System}
264 {The proposal which described this feature for inclusion in
265 the Python standard library.}
Fred Drake11514792004-01-08 14:59:02 +0000266 \seelink{http://www.red-dove.com/python_logging.html}
267 {Original Python \module{logging} package}
268 {This is the original source for the \module{logging}
269 package. The version of the package available from this
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000270 site is suitable for use with Python 1.5.2, 2.1.x and 2.2.x,
271 which do not include the \module{logging} package in the standard
Fred Drake11514792004-01-08 14:59:02 +0000272 library.}
Fred Drake68e6d572003-01-28 22:02:35 +0000273\end{seealso}
274
275
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000276\subsection{Logger Objects}
Skip Montanaro649698f2002-11-14 03:57:19 +0000277
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000278Loggers have the following attributes and methods. Note that Loggers are
279never instantiated directly, but always through the module-level function
280\function{logging.getLogger(name)}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000281
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000282\begin{datadesc}{propagate}
283If this evaluates to false, logging messages are not passed by this
284logger or by child loggers to higher level (ancestor) loggers. The
285constructor sets this attribute to 1.
Skip Montanaro649698f2002-11-14 03:57:19 +0000286\end{datadesc}
287
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000288\begin{methoddesc}{setLevel}{lvl}
289Sets the threshold for this logger to \var{lvl}. Logging messages
290which are less severe than \var{lvl} will be ignored. When a logger is
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000291created, the level is set to \constant{NOTSET} (which causes all messages
292to be processed in the root logger, or delegation to the parent in non-root
293loggers).
Skip Montanaro649698f2002-11-14 03:57:19 +0000294\end{methoddesc}
295
296\begin{methoddesc}{isEnabledFor}{lvl}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000297Indicates if a message of severity \var{lvl} would be processed by
298this logger. This method checks first the module-level level set by
299\function{logging.disable(lvl)} and then the logger's effective level as
300determined by \method{getEffectiveLevel()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000301\end{methoddesc}
302
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000303\begin{methoddesc}{getEffectiveLevel}{}
304Indicates the effective level for this logger. If a value other than
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000305\constant{NOTSET} has been set using \method{setLevel()}, it is returned.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000306Otherwise, the hierarchy is traversed towards the root until a value
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000307other than \constant{NOTSET} is found, and that value is returned.
Skip Montanaro649698f2002-11-14 03:57:19 +0000308\end{methoddesc}
309
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000310\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
311Logs a message with level \constant{DEBUG} on this logger.
312The \var{msg} is the message format string, and the \var{args} are the
313arguments which are merged into \var{msg}. The only keyword argument in
314\var{kwargs} which is inspected is \var{exc_info} which, if it does not
Vinay Sajip1dc5b1e2004-10-03 19:10:05 +0000315evaluate as false, causes exception information to be added to the logging
316message. If an exception tuple (as provided by \function{sys.exc_info()})
317is provided, it is used; otherwise, \function{sys.exc_info()} is called
318to get the exception information.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000319\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000320
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000321\begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
322Logs a message with level \constant{INFO} on this logger.
323The arguments are interpreted as for \method{debug()}.
324\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000325
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000326\begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
327Logs a message with level \constant{WARNING} on this logger.
328The arguments are interpreted as for \method{debug()}.
329\end{methoddesc}
330
331\begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
332Logs a message with level \constant{ERROR} on this logger.
333The arguments are interpreted as for \method{debug()}.
334\end{methoddesc}
335
336\begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
337Logs a message with level \constant{CRITICAL} on this logger.
338The arguments are interpreted as for \method{debug()}.
339\end{methoddesc}
340
341\begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}}
Vinay Sajip1cf56d02004-08-04 08:36:44 +0000342Logs a message with integer level \var{lvl} on this logger.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000343The other arguments are interpreted as for \method{debug()}.
344\end{methoddesc}
345
346\begin{methoddesc}{exception}{msg\optional{, *args}}
347Logs a message with level \constant{ERROR} on this logger.
348The arguments are interpreted as for \method{debug()}. Exception info
349is added to the logging message. This method should only be called
350from an exception handler.
351\end{methoddesc}
352
353\begin{methoddesc}{addFilter}{filt}
354Adds the specified filter \var{filt} to this logger.
355\end{methoddesc}
356
357\begin{methoddesc}{removeFilter}{filt}
358Removes the specified filter \var{filt} from this logger.
359\end{methoddesc}
360
361\begin{methoddesc}{filter}{record}
362Applies this logger's filters to the record and returns a true value if
363the record is to be processed.
364\end{methoddesc}
365
366\begin{methoddesc}{addHandler}{hdlr}
367Adds the specified handler \var{hdlr} to this logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000368\end{methoddesc}
369
370\begin{methoddesc}{removeHandler}{hdlr}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000371Removes the specified handler \var{hdlr} from this logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000372\end{methoddesc}
373
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000374\begin{methoddesc}{findCaller}{}
375Finds the caller's source filename and line number. Returns the filename
376and line number as a 2-element tuple.
Skip Montanaro649698f2002-11-14 03:57:19 +0000377\end{methoddesc}
378
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000379\begin{methoddesc}{handle}{record}
380Handles a record by passing it to all handlers associated with this logger
381and its ancestors (until a false value of \var{propagate} is found).
382This method is used for unpickled records received from a socket, as well
383as those created locally. Logger-level filtering is applied using
384\method{filter()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000385\end{methoddesc}
386
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000387\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info}
388This is a factory method which can be overridden in subclasses to create
389specialized \class{LogRecord} instances.
Skip Montanaro649698f2002-11-14 03:57:19 +0000390\end{methoddesc}
391
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000392\subsection{Basic example \label{minimal-example}}
393
394The \module{logging} package provides a lot of flexibility, and its
395configuration can appear daunting. This section demonstrates that simple
396use of the logging package is possible.
397
398The simplest example shows logging to the console:
399
400\begin{verbatim}
401import logging
402
403logging.debug('A debug message')
404logging.info('Some information')
405logging.warning('A shot across the bows')
406\end{verbatim}
407
408If you run the above script, you'll see this:
409\begin{verbatim}
410WARNING:root:A shot across the bows
411\end{verbatim}
412
413Because no particular logger was specified, the system used the root logger.
414The debug and info messages didn't appear because by default, the root
415logger is configured to only handle messages with a severity of WARNING
416or above. The message format is also a configuration default, as is the output
417destination of the messages - \code{sys.stderr}. The severity level,
418the message format and destination can be easily changed, as shown in
419the example below:
420
421\begin{verbatim}
422import logging
423
424logging.basicConfig(level=logging.DEBUG,
Vinay Sajipe3c330b2004-07-07 15:59:49 +0000425 format='%(asctime)s %(levelname)s %(message)s',
426 filename='/tmp/myapp.log',
427 filemode='w')
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000428logging.debug('A debug message')
429logging.info('Some information')
430logging.warning('A shot across the bows')
431\end{verbatim}
432
433The \method{basicConfig()} method is used to change the configuration
434defaults, which results in output (written to \code{/tmp/myapp.log})
435which should look something like the following:
436
437\begin{verbatim}
4382004-07-02 13:00:08,743 DEBUG A debug message
4392004-07-02 13:00:08,743 INFO Some information
4402004-07-02 13:00:08,743 WARNING A shot across the bows
441\end{verbatim}
442
443This time, all messages with a severity of DEBUG or above were handled,
444and the format of the messages was also changed, and output went to the
445specified file rather than the console.
446
447Formatting uses standard Python string formatting - see section
448\ref{typesseq-strings}. The format string takes the following
449common specifiers. For a complete list of specifiers, consult the
450\class{Formatter} documentation.
451
452\begin{tableii}{l|l}{code}{Format}{Description}
453\lineii{\%(name)s} {Name of the logger (logging channel).}
454\lineii{\%(levelname)s}{Text logging level for the message
455 (\code{'DEBUG'}, \code{'INFO'},
456 \code{'WARNING'}, \code{'ERROR'},
457 \code{'CRITICAL'}).}
458\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
459 was created. By default this is of the form
460 ``2003-07-08 16:49:45,896'' (the numbers after the
461 comma are millisecond portion of the time).}
462\lineii{\%(message)s} {The logged message.}
463\end{tableii}
464
465To change the date/time format, you can pass an additional keyword parameter,
466\var{datefmt}, as in the following:
467
468\begin{verbatim}
469import logging
470
471logging.basicConfig(level=logging.DEBUG,
Vinay Sajipe3c330b2004-07-07 15:59:49 +0000472 format='%(asctime)s %(levelname)-8s %(message)s',
473 datefmt='%a, %d %b %Y %H:%M:%S',
474 filename='/temp/myapp.log',
475 filemode='w')
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000476logging.debug('A debug message')
477logging.info('Some information')
478logging.warning('A shot across the bows')
479\end{verbatim}
480
481which would result in output like
482
483\begin{verbatim}
484Fri, 02 Jul 2004 13:06:18 DEBUG A debug message
485Fri, 02 Jul 2004 13:06:18 INFO Some information
486Fri, 02 Jul 2004 13:06:18 WARNING A shot across the bows
487\end{verbatim}
488
489The date format string follows the requirements of \function{strftime()} -
490see the documentation for the \refmodule{time} module.
491
492If, instead of sending logging output to the console or a file, you'd rather
493use a file-like object which you have created separately, you can pass it
494to \function{basicConfig()} using the \var{stream} keyword argument. Note
495that if both \var{stream} and \var{filename} keyword arguments are passed,
496the \var{stream} argument is ignored.
497
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000498Of course, you can put variable information in your output. To do this,
499simply have the message be a format string and pass in additional arguments
500containing the variable information, as in the following example:
501
502\begin{verbatim}
503import logging
504
505logging.basicConfig(level=logging.DEBUG,
506 format='%(asctime)s %(levelname)-8s %(message)s',
507 datefmt='%a, %d %b %Y %H:%M:%S',
508 filename='/temp/myapp.log',
509 filemode='w')
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000510logging.error('Pack my box with %d dozen %s', 5, 'liquor jugs')
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000511\end{verbatim}
512
513which would result in
514
515\begin{verbatim}
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000516Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000517\end{verbatim}
518
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000519\subsection{Logging to multiple destinations \label{multiple-destinations}}
520
521Let's say you want to log to console and file with different message formats
522and in differing circumstances. Say you want to log messages with levels
523of DEBUG and higher to file, and those messages at level INFO and higher to
524the console. Let's also assume that the file should contain timestamps, but
525the console messages should not. Here's how you can achieve this:
526
527\begin{verbatim}
528import logging
529
530#set up logging to file - see previous section for more details
531logging.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
537console = logging.StreamHandler()
538console.setLevel(logging.INFO)
539#set a format which is simpler for console use
540formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
541#tell the handler to use this format
542console.setFormatter(formatter)
543#add the handler to the root logger
544logging.getLogger('').addHandler(console)
545
546#Now, we can log to the root logger, or any other logger. First the root...
547logging.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
552logger1 = logging.getLogger('myapp.area1')
553logger2 = logging.getLogger('myapp.area2')
554
555logger1.debug('Quick zephyrs blow, vexing daft Jim.')
556logger1.info('How quickly daft jumping zebras vex.')
557logger2.warning('Jail zesty vixen who grabbed pay from quack.')
558logger2.error('The five boxing wizards jump quickly.')
559\end{verbatim}
560
561When you run this, on the console you will see
562
563\begin{verbatim}
564root : INFO Jackdaws love my big sphinx of quartz.
565myapp.area1 : INFO How quickly daft jumping zebras vex.
566myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.
567myapp.area2 : ERROR The five boxing wizards jump quickly.
568\end{verbatim}
569
570and in the file you will see something like
571
572\begin{verbatim}
57310-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
57410-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
57510-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
57610-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
57710-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
578\end{verbatim}
579
580As you can see, the DEBUG message only shows up in the file. The other
581messages are sent to both destinations.
582
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000583\subsection{Handler Objects}
Skip Montanaro649698f2002-11-14 03:57:19 +0000584
Fred Drake68e6d572003-01-28 22:02:35 +0000585Handlers have the following attributes and methods. Note that
586\class{Handler} is never instantiated directly; this class acts as a
587base for more useful subclasses. However, the \method{__init__()}
588method in subclasses needs to call \method{Handler.__init__()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000589
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000590\begin{methoddesc}{__init__}{level=\constant{NOTSET}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000591Initializes the \class{Handler} instance by setting its level, setting
592the list of filters to the empty list and creating a lock (using
Raymond Hettingerc75c3e02003-09-01 22:50:52 +0000593\method{createLock()}) for serializing access to an I/O mechanism.
Skip Montanaro649698f2002-11-14 03:57:19 +0000594\end{methoddesc}
595
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000596\begin{methoddesc}{createLock}{}
597Initializes a thread lock which can be used to serialize access to
598underlying I/O functionality which may not be threadsafe.
Skip Montanaro649698f2002-11-14 03:57:19 +0000599\end{methoddesc}
600
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000601\begin{methoddesc}{acquire}{}
602Acquires the thread lock created with \method{createLock()}.
603\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000604
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000605\begin{methoddesc}{release}{}
606Releases the thread lock acquired with \method{acquire()}.
607\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000608
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000609\begin{methoddesc}{setLevel}{lvl}
610Sets the threshold for this handler to \var{lvl}. Logging messages which are
611less severe than \var{lvl} will be ignored. When a handler is created, the
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000612level is set to \constant{NOTSET} (which causes all messages to be processed).
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000613\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000614
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000615\begin{methoddesc}{setFormatter}{form}
616Sets the \class{Formatter} for this handler to \var{form}.
617\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000618
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000619\begin{methoddesc}{addFilter}{filt}
620Adds the specified filter \var{filt} to this handler.
621\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000622
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000623\begin{methoddesc}{removeFilter}{filt}
624Removes the specified filter \var{filt} from this handler.
625\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000626
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000627\begin{methoddesc}{filter}{record}
628Applies this handler's filters to the record and returns a true value if
629the record is to be processed.
Skip Montanaro649698f2002-11-14 03:57:19 +0000630\end{methoddesc}
631
632\begin{methoddesc}{flush}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000633Ensure all logging output has been flushed. This version does
634nothing and is intended to be implemented by subclasses.
Skip Montanaro649698f2002-11-14 03:57:19 +0000635\end{methoddesc}
636
Skip Montanaro649698f2002-11-14 03:57:19 +0000637\begin{methoddesc}{close}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000638Tidy up any resources used by the handler. This version does
639nothing and is intended to be implemented by subclasses.
Skip Montanaro649698f2002-11-14 03:57:19 +0000640\end{methoddesc}
641
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000642\begin{methoddesc}{handle}{record}
643Conditionally emits the specified logging record, depending on
644filters which may have been added to the handler. Wraps the actual
645emission of the record with acquisition/release of the I/O thread
646lock.
Skip Montanaro649698f2002-11-14 03:57:19 +0000647\end{methoddesc}
648
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000649\begin{methoddesc}{handleError}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000650This method should be called from handlers when an exception is
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000651encountered during an \method{emit()} call. By default it does nothing,
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000652which means that exceptions get silently ignored. This is what is
653mostly wanted for a logging system - most users will not care
654about errors in the logging system, they are more interested in
655application errors. You could, however, replace this with a custom
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000656handler if you wish. The specified record is the one which was being
657processed when the exception occurred.
Skip Montanaro649698f2002-11-14 03:57:19 +0000658\end{methoddesc}
659
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000660\begin{methoddesc}{format}{record}
661Do formatting for a record - if a formatter is set, use it.
662Otherwise, use the default formatter for the module.
Skip Montanaro649698f2002-11-14 03:57:19 +0000663\end{methoddesc}
664
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000665\begin{methoddesc}{emit}{record}
666Do whatever it takes to actually log the specified logging record.
667This version is intended to be implemented by subclasses and so
668raises a \exception{NotImplementedError}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000669\end{methoddesc}
670
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000671\subsubsection{StreamHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000672
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000673The \class{StreamHandler} class sends logging output to streams such as
674\var{sys.stdout}, \var{sys.stderr} or any file-like object (or, more
675precisely, any object which supports \method{write()} and \method{flush()}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000676methods).
Skip Montanaro649698f2002-11-14 03:57:19 +0000677
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000678\begin{classdesc}{StreamHandler}{\optional{strm}}
679Returns a new instance of the \class{StreamHandler} class. If \var{strm} is
680specified, the instance will use it for logging output; otherwise,
681\var{sys.stderr} will be used.
Skip Montanaro649698f2002-11-14 03:57:19 +0000682\end{classdesc}
683
684\begin{methoddesc}{emit}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000685If a formatter is specified, it is used to format the record.
686The record is then written to the stream with a trailing newline.
687If exception information is present, it is formatted using
688\function{traceback.print_exception()} and appended to the stream.
Skip Montanaro649698f2002-11-14 03:57:19 +0000689\end{methoddesc}
690
691\begin{methoddesc}{flush}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000692Flushes the stream by calling its \method{flush()} method. Note that
693the \method{close()} method is inherited from \class{Handler} and
694so does nothing, so an explicit \method{flush()} call may be needed
695at times.
Skip Montanaro649698f2002-11-14 03:57:19 +0000696\end{methoddesc}
697
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000698\subsubsection{FileHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000699
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000700The \class{FileHandler} class sends logging output to a disk file.
Fred Drake68e6d572003-01-28 22:02:35 +0000701It inherits the output functionality from \class{StreamHandler}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000702
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000703\begin{classdesc}{FileHandler}{filename\optional{, mode}}
704Returns a new instance of the \class{FileHandler} class. The specified
705file is opened and used as the stream for logging. If \var{mode} is
Fred Drake9a5b6a62003-07-08 15:38:40 +0000706not specified, \constant{'a'} is used. By default, the file grows
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000707indefinitely.
Skip Montanaro649698f2002-11-14 03:57:19 +0000708\end{classdesc}
709
710\begin{methoddesc}{close}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000711Closes the file.
Skip Montanaro649698f2002-11-14 03:57:19 +0000712\end{methoddesc}
713
714\begin{methoddesc}{emit}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000715Outputs the record to the file.
716\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000717
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000718\subsubsection{RotatingFileHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000719
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000720The \class{RotatingFileHandler} class supports rotation of disk log files.
721
Fred Drake9a5b6a62003-07-08 15:38:40 +0000722\begin{classdesc}{RotatingFileHandler}{filename\optional{, mode\optional{,
723 maxBytes\optional{, backupCount}}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000724Returns a new instance of the \class{RotatingFileHandler} class. The
725specified file is opened and used as the stream for logging. If
Fred Drake68e6d572003-01-28 22:02:35 +0000726\var{mode} is not specified, \code{'a'} is used. By default, the
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000727file grows indefinitely.
Andrew M. Kuchling7cf4d9b2003-09-26 13:45:18 +0000728
729You can use the \var{maxBytes} and
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000730\var{backupCount} values to allow the file to \dfn{rollover} at a
731predetermined size. When the size is about to be exceeded, the file is
Andrew M. Kuchling7cf4d9b2003-09-26 13:45:18 +0000732closed and a new file is silently opened for output. Rollover occurs
733whenever the current log file is nearly \var{maxBytes} in length; if
734\var{maxBytes} is zero, rollover never occurs. If \var{backupCount}
735is non-zero, the system will save old log files by appending the
736extensions ".1", ".2" etc., to the filename. For example, with
737a \var{backupCount} of 5 and a base file name of
738\file{app.log}, you would get \file{app.log},
739\file{app.log.1}, \file{app.log.2}, up to \file{app.log.5}. The file being
740written to is always \file{app.log}. When this file is filled, it is
741closed and renamed to \file{app.log.1}, and if files \file{app.log.1},
742\file{app.log.2}, etc. exist, then they are renamed to \file{app.log.2},
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000743\file{app.log.3} etc. respectively.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000744\end{classdesc}
745
746\begin{methoddesc}{doRollover}{}
747Does a rollover, as described above.
748\end{methoddesc}
749
750\begin{methoddesc}{emit}{record}
751Outputs the record to the file, catering for rollover as described
752in \method{setRollover()}.
753\end{methoddesc}
754
755\subsubsection{SocketHandler}
756
757The \class{SocketHandler} class sends logging output to a network
758socket. The base class uses a TCP socket.
759
760\begin{classdesc}{SocketHandler}{host, port}
761Returns a new instance of the \class{SocketHandler} class intended to
762communicate with a remote machine whose address is given by \var{host}
763and \var{port}.
764\end{classdesc}
765
766\begin{methoddesc}{close}{}
767Closes the socket.
768\end{methoddesc}
769
770\begin{methoddesc}{handleError}{}
771\end{methoddesc}
772
773\begin{methoddesc}{emit}{}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000774Pickles the record's attribute dictionary and writes it to the socket in
775binary format. If there is an error with the socket, silently drops the
776packet. If the connection was previously lost, re-establishes the connection.
Fred Drake6b3b0462004-04-09 18:26:40 +0000777To unpickle the record at the receiving end into a \class{LogRecord}, use the
778\function{makeLogRecord()} function.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000779\end{methoddesc}
780
781\begin{methoddesc}{handleError}{}
782Handles an error which has occurred during \method{emit()}. The
783most likely cause is a lost connection. Closes the socket so that
784we can retry on the next event.
785\end{methoddesc}
786
787\begin{methoddesc}{makeSocket}{}
788This is a factory method which allows subclasses to define the precise
789type of socket they want. The default implementation creates a TCP
790socket (\constant{socket.SOCK_STREAM}).
791\end{methoddesc}
792
793\begin{methoddesc}{makePickle}{record}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000794Pickles the record's attribute dictionary in binary format with a length
795prefix, and returns it ready for transmission across the socket.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000796\end{methoddesc}
797
798\begin{methoddesc}{send}{packet}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000799Send a pickled string \var{packet} to the socket. This function allows
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000800for partial sends which can happen when the network is busy.
801\end{methoddesc}
802
803\subsubsection{DatagramHandler}
804
805The \class{DatagramHandler} class inherits from \class{SocketHandler}
806to support sending logging messages over UDP sockets.
807
808\begin{classdesc}{DatagramHandler}{host, port}
809Returns a new instance of the \class{DatagramHandler} class intended to
810communicate with a remote machine whose address is given by \var{host}
811and \var{port}.
812\end{classdesc}
813
814\begin{methoddesc}{emit}{}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000815Pickles the record's attribute dictionary and writes it to the socket in
816binary format. If there is an error with the socket, silently drops the
817packet.
Fred Drake6b3b0462004-04-09 18:26:40 +0000818To unpickle the record at the receiving end into a \class{LogRecord}, use the
819\function{makeLogRecord()} function.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000820\end{methoddesc}
821
822\begin{methoddesc}{makeSocket}{}
823The factory method of \class{SocketHandler} is here overridden to create
824a UDP socket (\constant{socket.SOCK_DGRAM}).
825\end{methoddesc}
826
827\begin{methoddesc}{send}{s}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000828Send a pickled string to a socket.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000829\end{methoddesc}
830
831\subsubsection{SysLogHandler}
832
833The \class{SysLogHandler} class supports sending logging messages to a
Fred Drake68e6d572003-01-28 22:02:35 +0000834remote or local \UNIX{} syslog.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000835
836\begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}}
837Returns a new instance of the \class{SysLogHandler} class intended to
Fred Drake68e6d572003-01-28 22:02:35 +0000838communicate with a remote \UNIX{} machine whose address is given by
839\var{address} in the form of a \code{(\var{host}, \var{port})}
840tuple. If \var{address} is not specified, \code{('localhost', 514)} is
841used. The address is used to open a UDP socket. If \var{facility} is
842not specified, \constant{LOG_USER} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000843\end{classdesc}
844
845\begin{methoddesc}{close}{}
846Closes the socket to the remote host.
847\end{methoddesc}
848
849\begin{methoddesc}{emit}{record}
850The record is formatted, and then sent to the syslog server. If
851exception information is present, it is \emph{not} sent to the server.
Skip Montanaro649698f2002-11-14 03:57:19 +0000852\end{methoddesc}
853
854\begin{methoddesc}{encodePriority}{facility, priority}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000855Encodes the facility and priority into an integer. You can pass in strings
856or integers - if strings are passed, internal mapping dictionaries are used
857to convert them to integers.
Skip Montanaro649698f2002-11-14 03:57:19 +0000858\end{methoddesc}
859
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000860\subsubsection{NTEventLogHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000861
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000862The \class{NTEventLogHandler} class supports sending logging messages
863to a local Windows NT, Windows 2000 or Windows XP event log. Before
864you can use it, you need Mark Hammond's Win32 extensions for Python
865installed.
Skip Montanaro649698f2002-11-14 03:57:19 +0000866
Fred Drake9a5b6a62003-07-08 15:38:40 +0000867\begin{classdesc}{NTEventLogHandler}{appname\optional{,
868 dllname\optional{, logtype}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000869Returns a new instance of the \class{NTEventLogHandler} class. The
870\var{appname} is used to define the application name as it appears in the
871event log. An appropriate registry entry is created using this name.
872The \var{dllname} should give the fully qualified pathname of a .dll or .exe
873which contains message definitions to hold in the log (if not specified,
Fred Drake9a5b6a62003-07-08 15:38:40 +0000874\code{'win32service.pyd'} is used - this is installed with the Win32
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000875extensions and contains some basic placeholder message definitions.
876Note that use of these placeholders will make your event logs big, as the
877entire message source is held in the log. If you want slimmer logs, you have
878to pass in the name of your own .dll or .exe which contains the message
879definitions you want to use in the event log). The \var{logtype} is one of
Fred Drake9a5b6a62003-07-08 15:38:40 +0000880\code{'Application'}, \code{'System'} or \code{'Security'}, and
881defaults to \code{'Application'}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000882\end{classdesc}
883
884\begin{methoddesc}{close}{}
885At this point, you can remove the application name from the registry as a
886source of event log entries. However, if you do this, you will not be able
887to see the events as you intended in the Event Log Viewer - it needs to be
888able to access the registry to get the .dll name. The current version does
889not do this (in fact it doesn't do anything).
890\end{methoddesc}
891
892\begin{methoddesc}{emit}{record}
893Determines the message ID, event category and event type, and then logs the
894message in the NT event log.
895\end{methoddesc}
896
897\begin{methoddesc}{getEventCategory}{record}
898Returns the event category for the record. Override this if you
899want to specify your own categories. This version returns 0.
900\end{methoddesc}
901
902\begin{methoddesc}{getEventType}{record}
903Returns the event type for the record. Override this if you want
904to specify your own types. This version does a mapping using the
905handler's typemap attribute, which is set up in \method{__init__()}
906to a dictionary which contains mappings for \constant{DEBUG},
907\constant{INFO}, \constant{WARNING}, \constant{ERROR} and
908\constant{CRITICAL}. If you are using your own levels, you will either need
909to override this method or place a suitable dictionary in the
910handler's \var{typemap} attribute.
911\end{methoddesc}
912
913\begin{methoddesc}{getMessageID}{record}
914Returns the message ID for the record. If you are using your
915own messages, you could do this by having the \var{msg} passed to the
916logger being an ID rather than a format string. Then, in here,
917you could use a dictionary lookup to get the message ID. This
918version returns 1, which is the base message ID in
Fred Drake9a5b6a62003-07-08 15:38:40 +0000919\file{win32service.pyd}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000920\end{methoddesc}
921
922\subsubsection{SMTPHandler}
923
924The \class{SMTPHandler} class supports sending logging messages to an email
925address via SMTP.
926
927\begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject}
928Returns a new instance of the \class{SMTPHandler} class. The
929instance is initialized with the from and to addresses and subject
930line of the email. The \var{toaddrs} should be a list of strings without
931domain names (That's what the \var{mailhost} is for). To specify a
932non-standard SMTP port, use the (host, port) tuple format for the
933\var{mailhost} argument. If you use a string, the standard SMTP port
934is used.
935\end{classdesc}
936
937\begin{methoddesc}{emit}{record}
938Formats the record and sends it to the specified addressees.
939\end{methoddesc}
940
941\begin{methoddesc}{getSubject}{record}
942If you want to specify a subject line which is record-dependent,
943override this method.
944\end{methoddesc}
945
946\subsubsection{MemoryHandler}
947
948The \class{MemoryHandler} supports buffering of logging records in memory,
949periodically flushing them to a \dfn{target} handler. Flushing occurs
950whenever the buffer is full, or when an event of a certain severity or
951greater is seen.
952
953\class{MemoryHandler} is a subclass of the more general
954\class{BufferingHandler}, which is an abstract class. This buffers logging
955records in memory. Whenever each record is added to the buffer, a
956check is made by calling \method{shouldFlush()} to see if the buffer
957should be flushed. If it should, then \method{flush()} is expected to
958do the needful.
959
960\begin{classdesc}{BufferingHandler}{capacity}
961Initializes the handler with a buffer of the specified capacity.
962\end{classdesc}
963
964\begin{methoddesc}{emit}{record}
965Appends the record to the buffer. If \method{shouldFlush()} returns true,
966calls \method{flush()} to process the buffer.
967\end{methoddesc}
968
969\begin{methoddesc}{flush}{}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000970You can override this to implement custom flushing behavior. This version
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000971just zaps the buffer to empty.
972\end{methoddesc}
973
974\begin{methoddesc}{shouldFlush}{record}
975Returns true if the buffer is up to capacity. This method can be
976overridden to implement custom flushing strategies.
977\end{methoddesc}
978
979\begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000980\optional{, target}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000981Returns a new instance of the \class{MemoryHandler} class. The
982instance is initialized with a buffer size of \var{capacity}. If
983\var{flushLevel} is not specified, \constant{ERROR} is used. If no
984\var{target} is specified, the target will need to be set using
985\method{setTarget()} before this handler does anything useful.
986\end{classdesc}
987
988\begin{methoddesc}{close}{}
989Calls \method{flush()}, sets the target to \constant{None} and
990clears the buffer.
991\end{methoddesc}
992
993\begin{methoddesc}{flush}{}
994For a \class{MemoryHandler}, flushing means just sending the buffered
995records to the target, if there is one. Override if you want
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000996different behavior.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000997\end{methoddesc}
998
999\begin{methoddesc}{setTarget}{target}
1000Sets the target handler for this handler.
1001\end{methoddesc}
1002
1003\begin{methoddesc}{shouldFlush}{record}
1004Checks for buffer full or a record at the \var{flushLevel} or higher.
1005\end{methoddesc}
1006
1007\subsubsection{HTTPHandler}
1008
1009The \class{HTTPHandler} class supports sending logging messages to a
Fred Drake68e6d572003-01-28 22:02:35 +00001010Web server, using either \samp{GET} or \samp{POST} semantics.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001011
1012\begin{classdesc}{HTTPHandler}{host, url\optional{, method}}
1013Returns a new instance of the \class{HTTPHandler} class. The
1014instance is initialized with a host address, url and HTTP method.
Fred Drake68e6d572003-01-28 22:02:35 +00001015If no \var{method} is specified, \samp{GET} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001016\end{classdesc}
1017
1018\begin{methoddesc}{emit}{record}
1019Sends the record to the Web server as an URL-encoded dictionary.
1020\end{methoddesc}
1021
1022\subsection{Formatter Objects}
1023
1024\class{Formatter}s have the following attributes and methods. They are
1025responsible for converting a \class{LogRecord} to (usually) a string
1026which can be interpreted by either a human or an external system. The
1027base
1028\class{Formatter} allows a formatting string to be specified. If none is
Fred Drake8efc74d2004-04-15 06:18:48 +00001029supplied, the default value of \code{'\%(message)s'} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001030
1031A Formatter can be initialized with a format string which makes use of
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001032knowledge of the \class{LogRecord} attributes - such as the default value
1033mentioned above making use of the fact that the user's message and
Fred Drake6b3b0462004-04-09 18:26:40 +00001034arguments are pre-formatted into a \class{LogRecord}'s \var{message}
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001035attribute. This format string contains standard python \%-style
1036mapping keys. See section \ref{typesseq-strings}, ``String Formatting
1037Operations,'' for more information on string formatting.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001038
Fred Drake6b3b0462004-04-09 18:26:40 +00001039Currently, the useful mapping keys in a \class{LogRecord} are:
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001040
Fred Drake9a5b6a62003-07-08 15:38:40 +00001041\begin{tableii}{l|l}{code}{Format}{Description}
1042\lineii{\%(name)s} {Name of the logger (logging channel).}
1043\lineii{\%(levelno)s} {Numeric logging level for the message
1044 (\constant{DEBUG}, \constant{INFO},
1045 \constant{WARNING}, \constant{ERROR},
1046 \constant{CRITICAL}).}
1047\lineii{\%(levelname)s}{Text logging level for the message
1048 (\code{'DEBUG'}, \code{'INFO'},
1049 \code{'WARNING'}, \code{'ERROR'},
1050 \code{'CRITICAL'}).}
1051\lineii{\%(pathname)s} {Full pathname of the source file where the logging
1052 call was issued (if available).}
1053\lineii{\%(filename)s} {Filename portion of pathname.}
1054\lineii{\%(module)s} {Module (name portion of filename).}
1055\lineii{\%(lineno)d} {Source line number where the logging call was issued
1056 (if available).}
Fred Drake6b3b0462004-04-09 18:26:40 +00001057\lineii{\%(created)f} {Time when the \class{LogRecord} was created (as
Fred Drake9a5b6a62003-07-08 15:38:40 +00001058 returned by \function{time.time()}).}
Fred Drake6b3b0462004-04-09 18:26:40 +00001059\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
1060 was created. By default this is of the form
Fred Drake9a5b6a62003-07-08 15:38:40 +00001061 ``2003-07-08 16:49:45,896'' (the numbers after the
1062 comma are millisecond portion of the time).}
1063\lineii{\%(msecs)d} {Millisecond portion of the time when the
1064 \class{LogRecord} was created.}
1065\lineii{\%(thread)d} {Thread ID (if available).}
1066\lineii{\%(process)d} {Process ID (if available).}
1067\lineii{\%(message)s} {The logged message, computed as \code{msg \% args}.}
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001068\end{tableii}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001069
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001070\begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}}
1071Returns a new instance of the \class{Formatter} class. The
1072instance is initialized with a format string for the message as a whole,
1073as well as a format string for the date/time portion of a message. If
Neal Norwitzdd3afa72003-07-08 16:26:34 +00001074no \var{fmt} is specified, \code{'\%(message)s'} is used. If no \var{datefmt}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001075is specified, the ISO8601 date format is used.
1076\end{classdesc}
1077
1078\begin{methoddesc}{format}{record}
1079The record's attribute dictionary is used as the operand to a
1080string formatting operation. Returns the resulting string.
1081Before formatting the dictionary, a couple of preparatory steps
1082are carried out. The \var{message} attribute of the record is computed
1083using \var{msg} \% \var{args}. If the formatting string contains
Fred Drake9a5b6a62003-07-08 15:38:40 +00001084\code{'(asctime)'}, \method{formatTime()} is called to format the
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001085event time. If there is exception information, it is formatted using
1086\method{formatException()} and appended to the message.
1087\end{methoddesc}
1088
1089\begin{methoddesc}{formatTime}{record\optional{, datefmt}}
1090This method should be called from \method{format()} by a formatter which
1091wants to make use of a formatted time. This method can be overridden
1092in formatters to provide for any specific requirement, but the
Raymond Hettinger2ef85a72003-01-25 21:46:53 +00001093basic behavior is as follows: if \var{datefmt} (a string) is specified,
Fred Drakec23e0192003-01-28 22:09:16 +00001094it is used with \function{time.strftime()} to format the creation time of the
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001095record. Otherwise, the ISO8601 format is used. The resulting
1096string is returned.
1097\end{methoddesc}
1098
1099\begin{methoddesc}{formatException}{exc_info}
1100Formats the specified exception information (a standard exception tuple
Fred Drakec23e0192003-01-28 22:09:16 +00001101as returned by \function{sys.exc_info()}) as a string. This default
1102implementation just uses \function{traceback.print_exception()}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001103The resulting string is returned.
1104\end{methoddesc}
1105
1106\subsection{Filter Objects}
1107
1108\class{Filter}s can be used by \class{Handler}s and \class{Logger}s for
1109more sophisticated filtering than is provided by levels. The base filter
1110class only allows events which are below a certain point in the logger
1111hierarchy. For example, a filter initialized with "A.B" will allow events
1112logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB",
1113"B.A.B" etc. If initialized with the empty string, all events are passed.
1114
1115\begin{classdesc}{Filter}{\optional{name}}
1116Returns an instance of the \class{Filter} class. If \var{name} is specified,
1117it names a logger which, together with its children, will have its events
1118allowed through the filter. If no name is specified, allows every event.
1119\end{classdesc}
1120
1121\begin{methoddesc}{filter}{record}
1122Is the specified record to be logged? Returns zero for no, nonzero for
1123yes. If deemed appropriate, the record may be modified in-place by this
1124method.
1125\end{methoddesc}
1126
1127\subsection{LogRecord Objects}
1128
Fred Drake6b3b0462004-04-09 18:26:40 +00001129\class{LogRecord} instances are created every time something is logged. They
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001130contain all the information pertinent to the event being logged. The
1131main information passed in is in msg and args, which are combined
1132using msg \% args to create the message field of the record. The record
1133also includes information such as when the record was created, the
1134source line where the logging call was made, and any exception
1135information to be logged.
1136
Fred Drake6b3b0462004-04-09 18:26:40 +00001137\class{LogRecord} has no methods; it's just a repository for
1138information about the logging event. The only reason it's a class
1139rather than a dictionary is to facilitate extension.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001140
1141\begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args,
Fred Drake9a5b6a62003-07-08 15:38:40 +00001142 exc_info}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001143Returns an instance of \class{LogRecord} initialized with interesting
1144information. The \var{name} is the logger name; \var{lvl} is the
1145numeric level; \var{pathname} is the absolute pathname of the source
1146file in which the logging call was made; \var{lineno} is the line
1147number in that file where the logging call is found; \var{msg} is the
1148user-supplied message (a format string); \var{args} is the tuple
1149which, together with \var{msg}, makes up the user message; and
1150\var{exc_info} is the exception tuple obtained by calling
1151\function{sys.exc_info() }(or \constant{None}, if no exception information
1152is available).
1153\end{classdesc}
1154
1155\subsection{Thread Safety}
1156
1157The logging module is intended to be thread-safe without any special work
1158needing to be done by its clients. It achieves this though using threading
1159locks; there is one lock to serialize access to the module's shared data,
1160and each handler also creates a lock to serialize access to its underlying
1161I/O.
1162
1163\subsection{Configuration}
1164
1165
Fred Drake94ffbb72004-04-08 19:44:31 +00001166\subsubsection{Configuration functions%
1167 \label{logging-config-api}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001168
Fred Drake9a5b6a62003-07-08 15:38:40 +00001169The following functions allow the logging module to be
1170configured. Before they can be used, you must import
1171\module{logging.config}. Their use is optional --- you can configure
1172the logging module entirely by making calls to the main API (defined
1173in \module{logging} itself) and defining handlers which are declared
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001174either in \module{logging} or \module{logging.handlers}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001175
1176\begin{funcdesc}{fileConfig}{fname\optional{, defaults}}
1177Reads the logging configuration from a ConfigParser-format file named
1178\var{fname}. This function can be called several times from an application,
1179allowing an end user the ability to select from various pre-canned
1180configurations (if the developer provides a mechanism to present the
1181choices and load the chosen configuration). Defaults to be passed to
1182ConfigParser can be specified in the \var{defaults} argument.
1183\end{funcdesc}
1184
1185\begin{funcdesc}{listen}{\optional{port}}
1186Starts up a socket server on the specified port, and listens for new
1187configurations. If no port is specified, the module's default
1188\constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations
1189will be sent as a file suitable for processing by \function{fileConfig()}.
1190Returns a \class{Thread} instance on which you can call \method{start()}
1191to start the server, and which you can \method{join()} when appropriate.
1192To stop the server, call \function{stopListening()}.
1193\end{funcdesc}
1194
1195\begin{funcdesc}{stopListening}{}
1196Stops the listening server which was created with a call to
1197\function{listen()}. This is typically called before calling \method{join()}
1198on the return value from \function{listen()}.
1199\end{funcdesc}
1200
Fred Drake94ffbb72004-04-08 19:44:31 +00001201\subsubsection{Configuration file format%
1202 \label{logging-config-fileformat}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001203
Fred Drake6b3b0462004-04-09 18:26:40 +00001204The configuration file format understood by \function{fileConfig()} is
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001205based on ConfigParser functionality. The file must contain sections
1206called \code{[loggers]}, \code{[handlers]} and \code{[formatters]}
1207which identify by name the entities of each type which are defined in
1208the file. For each such entity, there is a separate section which
1209identified how that entity is configured. Thus, for a logger named
1210\code{log01} in the \code{[loggers]} section, the relevant
1211configuration details are held in a section
1212\code{[logger_log01]}. Similarly, a handler called \code{hand01} in
1213the \code{[handlers]} section will have its configuration held in a
1214section called \code{[handler_hand01]}, while a formatter called
1215\code{form01} in the \code{[formatters]} section will have its
1216configuration specified in a section called
1217\code{[formatter_form01]}. The root logger configuration must be
1218specified in a section called \code{[logger_root]}.
1219
1220Examples of these sections in the file are given below.
Skip Montanaro649698f2002-11-14 03:57:19 +00001221
1222\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001223[loggers]
1224keys=root,log02,log03,log04,log05,log06,log07
Skip Montanaro649698f2002-11-14 03:57:19 +00001225
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001226[handlers]
1227keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
1228
1229[formatters]
1230keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
Skip Montanaro649698f2002-11-14 03:57:19 +00001231\end{verbatim}
1232
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001233The root logger must specify a level and a list of handlers. An
1234example of a root logger section is given below.
Skip Montanaro649698f2002-11-14 03:57:19 +00001235
1236\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001237[logger_root]
1238level=NOTSET
1239handlers=hand01
Skip Montanaro649698f2002-11-14 03:57:19 +00001240\end{verbatim}
1241
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001242The \code{level} entry can be one of \code{DEBUG, INFO, WARNING,
1243ERROR, CRITICAL} or \code{NOTSET}. For the root logger only,
1244\code{NOTSET} means that all messages will be logged. Level values are
1245\function{eval()}uated in the context of the \code{logging} package's
1246namespace.
Skip Montanaro649698f2002-11-14 03:57:19 +00001247
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001248The \code{handlers} entry is a comma-separated list of handler names,
1249which must appear in the \code{[handlers]} section. These names must
1250appear in the \code{[handlers]} section and have corresponding
1251sections in the configuration file.
Skip Montanaro649698f2002-11-14 03:57:19 +00001252
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001253For loggers other than the root logger, some additional information is
1254required. This is illustrated by the following example.
Skip Montanaro649698f2002-11-14 03:57:19 +00001255
1256\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001257[logger_parser]
1258level=DEBUG
1259handlers=hand01
1260propagate=1
1261qualname=compiler.parser
Skip Montanaro649698f2002-11-14 03:57:19 +00001262\end{verbatim}
1263
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001264The \code{level} and \code{handlers} entries are interpreted as for
1265the root logger, except that if a non-root logger's level is specified
1266as \code{NOTSET}, the system consults loggers higher up the hierarchy
1267to determine the effective level of the logger. The \code{propagate}
1268entry is set to 1 to indicate that messages must propagate to handlers
1269higher up the logger hierarchy from this logger, or 0 to indicate that
1270messages are \strong{not} propagated to handlers up the hierarchy. The
1271\code{qualname} entry is the hierarchical channel name of the logger,
Vinay Sajipa13c60b2004-07-03 11:45:53 +00001272that is to say the name used by the application to get the logger.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001273
1274Sections which specify handler configuration are exemplified by the
1275following.
1276
Skip Montanaro649698f2002-11-14 03:57:19 +00001277\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001278[handler_hand01]
1279class=StreamHandler
1280level=NOTSET
1281formatter=form01
1282args=(sys.stdout,)
Skip Montanaro649698f2002-11-14 03:57:19 +00001283\end{verbatim}
1284
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001285The \code{class} entry indicates the handler's class (as determined by
1286\function{eval()} in the \code{logging} package's namespace). The
1287\code{level} is interpreted as for loggers, and \code{NOTSET} is taken
1288to mean "log everything".
1289
1290The \code{formatter} entry indicates the key name of the formatter for
1291this handler. If blank, a default formatter
1292(\code{logging._defaultFormatter}) is used. If a name is specified, it
1293must appear in the \code{[formatters]} section and have a
1294corresponding section in the configuration file.
1295
1296The \code{args} entry, when \function{eval()}uated in the context of
1297the \code{logging} package's namespace, is the list of arguments to
1298the constructor for the handler class. Refer to the constructors for
1299the relevant handlers, or to the examples below, to see how typical
1300entries are constructed.
Skip Montanaro649698f2002-11-14 03:57:19 +00001301
1302\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001303[handler_hand02]
1304class=FileHandler
1305level=DEBUG
1306formatter=form02
1307args=('python.log', 'w')
1308
1309[handler_hand03]
1310class=handlers.SocketHandler
1311level=INFO
1312formatter=form03
1313args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
1314
1315[handler_hand04]
1316class=handlers.DatagramHandler
1317level=WARN
1318formatter=form04
1319args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)
1320
1321[handler_hand05]
1322class=handlers.SysLogHandler
1323level=ERROR
1324formatter=form05
1325args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
1326
1327[handler_hand06]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001328class=handlers.NTEventLogHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001329level=CRITICAL
1330formatter=form06
1331args=('Python Application', '', 'Application')
1332
1333[handler_hand07]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001334class=handlers.SMTPHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001335level=WARN
1336formatter=form07
1337args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
1338
1339[handler_hand08]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001340class=handlers.MemoryHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001341level=NOTSET
1342formatter=form08
1343target=
1344args=(10, ERROR)
1345
1346[handler_hand09]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001347class=handlers.HTTPHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001348level=NOTSET
1349formatter=form09
1350args=('localhost:9022', '/log', 'GET')
Skip Montanaro649698f2002-11-14 03:57:19 +00001351\end{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001352
1353Sections which specify formatter configuration are typified by the following.
1354
1355\begin{verbatim}
1356[formatter_form01]
1357format=F1 %(asctime)s %(levelname)s %(message)s
1358datefmt=
1359\end{verbatim}
1360
1361The \code{format} entry is the overall format string, and the
1362\code{datefmt} entry is the \function{strftime()}-compatible date/time format
1363string. If empty, the package substitutes ISO8601 format date/times, which
1364is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S".
1365The ISO8601 format also specifies milliseconds, which are appended to the
1366result of using the above format string, with a comma separator. An example
1367time in ISO8601 format is \code{2003-01-23 00:29:50,411}.