blob: 4725d58e32ad8a5a56e8c4ea5e59b23a2920d5b8 [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
Johannes Gijsbers4f802ac2004-11-07 14:14:27 +000083\item \class{TimedRotatingFileHandler} instances send error messages to
84disk files rotating the log file at certain timed intervals.
85
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000086\item \class{SocketHandler} instances send error messages to
87TCP/IP sockets.
88
89\item \class{DatagramHandler} instances send error messages to UDP
90sockets.
91
92\item \class{SMTPHandler} instances send error messages to a
93designated email address.
94
95\item \class{SysLogHandler} instances send error messages to a
Fred Drake68e6d572003-01-28 22:02:35 +000096\UNIX{} syslog daemon, possibly on a remote machine.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +000097
98\item \class{NTEventLogHandler} instances send error messages to a
99Windows NT/2000/XP event log.
100
101\item \class{MemoryHandler} instances send error messages to a
102buffer in memory, which is flushed whenever specific criteria are
103met.
104
105\item \class{HTTPHandler} instances send error messages to an
Fred Drake68e6d572003-01-28 22:02:35 +0000106HTTP server using either \samp{GET} or \samp{POST} semantics.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000107
108\end{enumerate}
109
110The \class{StreamHandler} and \class{FileHandler} classes are defined
111in the core logging package. The other handlers are defined in a sub-
112module, \module{logging.handlers}. (There is also another sub-module,
113\module{logging.config}, for configuration functionality.)
114
115Logged messages are formatted for presentation through instances of the
116\class{Formatter} class. They are initialized with a format string
117suitable for use with the \% operator and a dictionary.
118
119For formatting multiple messages in a batch, instances of
120\class{BufferingFormatter} can be used. In addition to the format string
121(which is applied to each message in the batch), there is provision for
122header and trailer format strings.
123
124When filtering based on logger level and/or handler level is not enough,
125instances of \class{Filter} can be added to both \class{Logger} and
Fred Drakec23e0192003-01-28 22:09:16 +0000126\class{Handler} instances (through their \method{addFilter()} method).
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000127Before deciding to process a message further, both loggers and handlers
128consult all their filters for permission. If any filter returns a false
129value, the message is not processed further.
130
131The basic \class{Filter} functionality allows filtering by specific logger
132name. If this feature is used, messages sent to the named logger and its
133children are allowed through the filter, and all others dropped.
134
135In addition to the classes described above, there are a number of module-
136level functions.
137
138\begin{funcdesc}{getLogger}{\optional{name}}
139Return a logger with the specified name or, if no name is specified, return
Vinay Sajip17952b72004-08-31 10:21:51 +0000140a logger which is the root logger of the hierarchy. If specified, the name
141is typically a dot-separated hierarchical name like \var{"a"}, \var{"a.b"}
142or \var{"a.b.c.d"}. Choice of these names is entirely up to the developer
143who is using logging.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000144
145All calls to this function with a given name return the same logger instance.
146This means that logger instances never need to be passed between different
147parts of an application.
Skip Montanaro649698f2002-11-14 03:57:19 +0000148\end{funcdesc}
149
Vinay Sajipc6646c02004-09-22 12:55:16 +0000150\begin{funcdesc}{getLoggerClass}{}
151Return either the standard \class{Logger} class, or the last class passed to
152\function{setLoggerClass()}. This function may be called from within a new
153class definition, to ensure that installing a customised \class{Logger} class
154will not undo customisations already applied by other code. For example:
155
156\begin{verbatim}
157 class MyLogger(logging.getLoggerClass()):
158 # ... override behaviour here
159\end{verbatim}
160
161\end{funcdesc}
162
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000163\begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
164Logs a message with level \constant{DEBUG} on the root logger.
165The \var{msg} is the message format string, and the \var{args} are the
166arguments which are merged into \var{msg}. The only keyword argument in
167\var{kwargs} which is inspected is \var{exc_info} which, if it does not
Vinay Sajip1dc5b1e2004-10-03 19:10:05 +0000168evaluate as false, causes exception information to be added to the logging
169message. If an exception tuple (in the format returned by
170\function{sys.exc_info()}) is provided, it is used; otherwise,
171\function{sys.exc_info()} is called to get the exception information.
Skip Montanaro649698f2002-11-14 03:57:19 +0000172\end{funcdesc}
173
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000174\begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
175Logs a message with level \constant{INFO} on the root logger.
176The arguments are interpreted as for \function{debug()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000177\end{funcdesc}
178
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000179\begin{funcdesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
180Logs a message with level \constant{WARNING} on the root logger.
181The arguments are interpreted as for \function{debug()}.
182\end{funcdesc}
183
184\begin{funcdesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
185Logs a message with level \constant{ERROR} on the root logger.
186The arguments are interpreted as for \function{debug()}.
187\end{funcdesc}
188
189\begin{funcdesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
190Logs a message with level \constant{CRITICAL} on the root logger.
191The arguments are interpreted as for \function{debug()}.
192\end{funcdesc}
193
194\begin{funcdesc}{exception}{msg\optional{, *args}}
195Logs a message with level \constant{ERROR} on the root logger.
196The arguments are interpreted as for \function{debug()}. Exception info
197is added to the logging message. This function should only be called
198from an exception handler.
199\end{funcdesc}
200
Vinay Sajip739d49e2004-09-24 11:46:44 +0000201\begin{funcdesc}{log}{level, msg\optional{, *args\optional{, **kwargs}}}
202Logs a message with level \var{level} on the root logger.
203The other arguments are interpreted as for \function{debug()}.
204\end{funcdesc}
205
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000206\begin{funcdesc}{disable}{lvl}
207Provides an overriding level \var{lvl} for all loggers which takes
208precedence over the logger's own level. When the need arises to
209temporarily throttle logging output down across the whole application,
210this function can be useful.
211\end{funcdesc}
212
213\begin{funcdesc}{addLevelName}{lvl, levelName}
214Associates level \var{lvl} with text \var{levelName} in an internal
215dictionary, which is used to map numeric levels to a textual
216representation, for example when a \class{Formatter} formats a message.
217This function can also be used to define your own levels. The only
218constraints are that all levels used must be registered using this
219function, levels should be positive integers and they should increase
220in increasing order of severity.
221\end{funcdesc}
222
223\begin{funcdesc}{getLevelName}{lvl}
224Returns the textual representation of logging level \var{lvl}. If the
225level is one of the predefined levels \constant{CRITICAL},
226\constant{ERROR}, \constant{WARNING}, \constant{INFO} or \constant{DEBUG}
227then you get the corresponding string. If you have associated levels
228with names using \function{addLevelName()} then the name you have associated
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000229with \var{lvl} is returned. If a numeric value corresponding to one of the
230defined levels is passed in, the corresponding string representation is
231returned. Otherwise, the string "Level \%s" \% lvl is returned.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000232\end{funcdesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000233
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000234\begin{funcdesc}{makeLogRecord}{attrdict}
235Creates and returns a new \class{LogRecord} instance whose attributes are
236defined by \var{attrdict}. This function is useful for taking a pickled
237\class{LogRecord} attribute dictionary, sent over a socket, and reconstituting
238it as a \class{LogRecord} instance at the receiving end.
239\end{funcdesc}
240
Skip Montanaro649698f2002-11-14 03:57:19 +0000241\begin{funcdesc}{basicConfig}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000242Does basic configuration for the logging system by creating a
243\class{StreamHandler} with a default \class{Formatter} and adding it to
244the root logger. The functions \function{debug()}, \function{info()},
245\function{warning()}, \function{error()} and \function{critical()} will call
246\function{basicConfig()} automatically if no handlers are defined for the
247root logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000248\end{funcdesc}
249
Skip Montanaro649698f2002-11-14 03:57:19 +0000250\begin{funcdesc}{shutdown}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000251Informs the logging system to perform an orderly shutdown by flushing and
252closing all handlers.
Skip Montanaro649698f2002-11-14 03:57:19 +0000253\end{funcdesc}
254
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000255\begin{funcdesc}{setLoggerClass}{klass}
256Tells the logging system to use the class \var{klass} when instantiating a
257logger. The class should define \method{__init__()} such that only a name
258argument is required, and the \method{__init__()} should call
259\method{Logger.__init__()}. This function is typically called before any
260loggers are instantiated by applications which need to use custom logger
261behavior.
262\end{funcdesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000263
Fred Drake68e6d572003-01-28 22:02:35 +0000264
265\begin{seealso}
266 \seepep{282}{A Logging System}
267 {The proposal which described this feature for inclusion in
268 the Python standard library.}
Fred Drake11514792004-01-08 14:59:02 +0000269 \seelink{http://www.red-dove.com/python_logging.html}
270 {Original Python \module{logging} package}
271 {This is the original source for the \module{logging}
272 package. The version of the package available from this
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000273 site is suitable for use with Python 1.5.2, 2.1.x and 2.2.x,
274 which do not include the \module{logging} package in the standard
Fred Drake11514792004-01-08 14:59:02 +0000275 library.}
Fred Drake68e6d572003-01-28 22:02:35 +0000276\end{seealso}
277
278
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000279\subsection{Logger Objects}
Skip Montanaro649698f2002-11-14 03:57:19 +0000280
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000281Loggers have the following attributes and methods. Note that Loggers are
282never instantiated directly, but always through the module-level function
283\function{logging.getLogger(name)}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000284
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000285\begin{datadesc}{propagate}
286If this evaluates to false, logging messages are not passed by this
287logger or by child loggers to higher level (ancestor) loggers. The
288constructor sets this attribute to 1.
Skip Montanaro649698f2002-11-14 03:57:19 +0000289\end{datadesc}
290
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000291\begin{methoddesc}{setLevel}{lvl}
292Sets the threshold for this logger to \var{lvl}. Logging messages
293which are less severe than \var{lvl} will be ignored. When a logger is
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000294created, the level is set to \constant{NOTSET} (which causes all messages
295to be processed in the root logger, or delegation to the parent in non-root
296loggers).
Skip Montanaro649698f2002-11-14 03:57:19 +0000297\end{methoddesc}
298
299\begin{methoddesc}{isEnabledFor}{lvl}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000300Indicates if a message of severity \var{lvl} would be processed by
301this logger. This method checks first the module-level level set by
302\function{logging.disable(lvl)} and then the logger's effective level as
303determined by \method{getEffectiveLevel()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000304\end{methoddesc}
305
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000306\begin{methoddesc}{getEffectiveLevel}{}
307Indicates the effective level for this logger. If a value other than
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000308\constant{NOTSET} has been set using \method{setLevel()}, it is returned.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000309Otherwise, the hierarchy is traversed towards the root until a value
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000310other than \constant{NOTSET} is found, and that value is returned.
Skip Montanaro649698f2002-11-14 03:57:19 +0000311\end{methoddesc}
312
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000313\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
314Logs a message with level \constant{DEBUG} on this logger.
315The \var{msg} is the message format string, and the \var{args} are the
316arguments which are merged into \var{msg}. The only keyword argument in
317\var{kwargs} which is inspected is \var{exc_info} which, if it does not
Vinay Sajip1dc5b1e2004-10-03 19:10:05 +0000318evaluate as false, causes exception information to be added to the logging
319message. If an exception tuple (as provided by \function{sys.exc_info()})
320is provided, it is used; otherwise, \function{sys.exc_info()} is called
321to get the exception information.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000322\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000323
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000324\begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
325Logs a message with level \constant{INFO} on this logger.
326The arguments are interpreted as for \method{debug()}.
327\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000328
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000329\begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
330Logs a message with level \constant{WARNING} on this logger.
331The arguments are interpreted as for \method{debug()}.
332\end{methoddesc}
333
334\begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
335Logs a message with level \constant{ERROR} on this logger.
336The arguments are interpreted as for \method{debug()}.
337\end{methoddesc}
338
339\begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
340Logs a message with level \constant{CRITICAL} on this logger.
341The arguments are interpreted as for \method{debug()}.
342\end{methoddesc}
343
344\begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}}
Vinay Sajip1cf56d02004-08-04 08:36:44 +0000345Logs a message with integer level \var{lvl} on this logger.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000346The other arguments are interpreted as for \method{debug()}.
347\end{methoddesc}
348
349\begin{methoddesc}{exception}{msg\optional{, *args}}
350Logs a message with level \constant{ERROR} on this logger.
351The arguments are interpreted as for \method{debug()}. Exception info
352is added to the logging message. This method should only be called
353from an exception handler.
354\end{methoddesc}
355
356\begin{methoddesc}{addFilter}{filt}
357Adds the specified filter \var{filt} to this logger.
358\end{methoddesc}
359
360\begin{methoddesc}{removeFilter}{filt}
361Removes the specified filter \var{filt} from this logger.
362\end{methoddesc}
363
364\begin{methoddesc}{filter}{record}
365Applies this logger's filters to the record and returns a true value if
366the record is to be processed.
367\end{methoddesc}
368
369\begin{methoddesc}{addHandler}{hdlr}
370Adds the specified handler \var{hdlr} to this logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000371\end{methoddesc}
372
373\begin{methoddesc}{removeHandler}{hdlr}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000374Removes the specified handler \var{hdlr} from this logger.
Skip Montanaro649698f2002-11-14 03:57:19 +0000375\end{methoddesc}
376
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000377\begin{methoddesc}{findCaller}{}
378Finds the caller's source filename and line number. Returns the filename
379and line number as a 2-element tuple.
Skip Montanaro649698f2002-11-14 03:57:19 +0000380\end{methoddesc}
381
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000382\begin{methoddesc}{handle}{record}
383Handles a record by passing it to all handlers associated with this logger
384and its ancestors (until a false value of \var{propagate} is found).
385This method is used for unpickled records received from a socket, as well
386as those created locally. Logger-level filtering is applied using
387\method{filter()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000388\end{methoddesc}
389
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000390\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info}
391This is a factory method which can be overridden in subclasses to create
392specialized \class{LogRecord} instances.
Skip Montanaro649698f2002-11-14 03:57:19 +0000393\end{methoddesc}
394
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000395\subsection{Basic example \label{minimal-example}}
396
397The \module{logging} package provides a lot of flexibility, and its
398configuration can appear daunting. This section demonstrates that simple
399use of the logging package is possible.
400
401The simplest example shows logging to the console:
402
403\begin{verbatim}
404import logging
405
406logging.debug('A debug message')
407logging.info('Some information')
408logging.warning('A shot across the bows')
409\end{verbatim}
410
411If you run the above script, you'll see this:
412\begin{verbatim}
413WARNING:root:A shot across the bows
414\end{verbatim}
415
416Because no particular logger was specified, the system used the root logger.
417The debug and info messages didn't appear because by default, the root
418logger is configured to only handle messages with a severity of WARNING
419or above. The message format is also a configuration default, as is the output
420destination of the messages - \code{sys.stderr}. The severity level,
421the message format and destination can be easily changed, as shown in
422the example below:
423
424\begin{verbatim}
425import logging
426
427logging.basicConfig(level=logging.DEBUG,
Vinay Sajipe3c330b2004-07-07 15:59:49 +0000428 format='%(asctime)s %(levelname)s %(message)s',
429 filename='/tmp/myapp.log',
430 filemode='w')
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000431logging.debug('A debug message')
432logging.info('Some information')
433logging.warning('A shot across the bows')
434\end{verbatim}
435
436The \method{basicConfig()} method is used to change the configuration
437defaults, which results in output (written to \code{/tmp/myapp.log})
438which should look something like the following:
439
440\begin{verbatim}
4412004-07-02 13:00:08,743 DEBUG A debug message
4422004-07-02 13:00:08,743 INFO Some information
4432004-07-02 13:00:08,743 WARNING A shot across the bows
444\end{verbatim}
445
446This time, all messages with a severity of DEBUG or above were handled,
447and the format of the messages was also changed, and output went to the
448specified file rather than the console.
449
450Formatting uses standard Python string formatting - see section
451\ref{typesseq-strings}. The format string takes the following
452common specifiers. For a complete list of specifiers, consult the
453\class{Formatter} documentation.
454
455\begin{tableii}{l|l}{code}{Format}{Description}
456\lineii{\%(name)s} {Name of the logger (logging channel).}
457\lineii{\%(levelname)s}{Text logging level for the message
458 (\code{'DEBUG'}, \code{'INFO'},
459 \code{'WARNING'}, \code{'ERROR'},
460 \code{'CRITICAL'}).}
461\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
462 was created. By default this is of the form
463 ``2003-07-08 16:49:45,896'' (the numbers after the
464 comma are millisecond portion of the time).}
465\lineii{\%(message)s} {The logged message.}
466\end{tableii}
467
468To change the date/time format, you can pass an additional keyword parameter,
469\var{datefmt}, as in the following:
470
471\begin{verbatim}
472import logging
473
474logging.basicConfig(level=logging.DEBUG,
Vinay Sajipe3c330b2004-07-07 15:59:49 +0000475 format='%(asctime)s %(levelname)-8s %(message)s',
476 datefmt='%a, %d %b %Y %H:%M:%S',
477 filename='/temp/myapp.log',
478 filemode='w')
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000479logging.debug('A debug message')
480logging.info('Some information')
481logging.warning('A shot across the bows')
482\end{verbatim}
483
484which would result in output like
485
486\begin{verbatim}
487Fri, 02 Jul 2004 13:06:18 DEBUG A debug message
488Fri, 02 Jul 2004 13:06:18 INFO Some information
489Fri, 02 Jul 2004 13:06:18 WARNING A shot across the bows
490\end{verbatim}
491
492The date format string follows the requirements of \function{strftime()} -
493see the documentation for the \refmodule{time} module.
494
495If, instead of sending logging output to the console or a file, you'd rather
496use a file-like object which you have created separately, you can pass it
497to \function{basicConfig()} using the \var{stream} keyword argument. Note
498that if both \var{stream} and \var{filename} keyword arguments are passed,
499the \var{stream} argument is ignored.
500
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000501Of course, you can put variable information in your output. To do this,
502simply have the message be a format string and pass in additional arguments
503containing the variable information, as in the following example:
504
505\begin{verbatim}
506import logging
507
508logging.basicConfig(level=logging.DEBUG,
509 format='%(asctime)s %(levelname)-8s %(message)s',
510 datefmt='%a, %d %b %Y %H:%M:%S',
511 filename='/temp/myapp.log',
512 filemode='w')
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000513logging.error('Pack my box with %d dozen %s', 5, 'liquor jugs')
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000514\end{verbatim}
515
516which would result in
517
518\begin{verbatim}
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000519Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs
Vinay Sajipb4bf62f2004-07-21 14:40:11 +0000520\end{verbatim}
521
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000522\subsection{Logging to multiple destinations \label{multiple-destinations}}
523
524Let's say you want to log to console and file with different message formats
525and in differing circumstances. Say you want to log messages with levels
526of DEBUG and higher to file, and those messages at level INFO and higher to
527the console. Let's also assume that the file should contain timestamps, but
528the console messages should not. Here's how you can achieve this:
529
530\begin{verbatim}
531import logging
532
Fred Drake048840c2004-10-29 14:35:42 +0000533# set up logging to file - see previous section for more details
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000534logging.basicConfig(level=logging.DEBUG,
535 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
536 datefmt='%m-%d %H:%M',
537 filename='/temp/myapp.log',
538 filemode='w')
Fred Drake048840c2004-10-29 14:35:42 +0000539# define a Handler which writes INFO messages or higher to the sys.stderr
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000540console = logging.StreamHandler()
541console.setLevel(logging.INFO)
Fred Drake048840c2004-10-29 14:35:42 +0000542# set a format which is simpler for console use
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000543formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
Fred Drake048840c2004-10-29 14:35:42 +0000544# tell the handler to use this format
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000545console.setFormatter(formatter)
Fred Drake048840c2004-10-29 14:35:42 +0000546# add the handler to the root logger
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000547logging.getLogger('').addHandler(console)
548
Fred Drake048840c2004-10-29 14:35:42 +0000549# Now, we can log to the root logger, or any other logger. First the root...
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000550logging.info('Jackdaws love my big sphinx of quartz.')
551
Fred Drake048840c2004-10-29 14:35:42 +0000552# Now, define a couple of other loggers which might represent areas in your
553# application:
Vinay Sajip93ae4c12004-10-22 21:43:15 +0000554
555logger1 = logging.getLogger('myapp.area1')
556logger2 = logging.getLogger('myapp.area2')
557
558logger1.debug('Quick zephyrs blow, vexing daft Jim.')
559logger1.info('How quickly daft jumping zebras vex.')
560logger2.warning('Jail zesty vixen who grabbed pay from quack.')
561logger2.error('The five boxing wizards jump quickly.')
562\end{verbatim}
563
564When you run this, on the console you will see
565
566\begin{verbatim}
567root : INFO Jackdaws love my big sphinx of quartz.
568myapp.area1 : INFO How quickly daft jumping zebras vex.
569myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.
570myapp.area2 : ERROR The five boxing wizards jump quickly.
571\end{verbatim}
572
573and in the file you will see something like
574
575\begin{verbatim}
57610-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
57710-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
57810-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
57910-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
58010-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
581\end{verbatim}
582
583As you can see, the DEBUG message only shows up in the file. The other
584messages are sent to both destinations.
585
Vinay Sajip006483b2004-10-29 12:30:28 +0000586This example uses console and file handlers, but you can use any number and
587combination of handlers you choose.
588
589\subsection{Sending and receiving logging events across a network
590\label{network-logging}}
591
592Let's say you want to send logging events across a network, and handle them
593at the receiving end. A simple way of doing this is attaching a
594\class{SocketHandler} instance to the root logger at the sending end:
595
596\begin{verbatim}
597import logging, logging.handlers
598
599rootLogger = logging.getLogger('')
600rootLogger.setLevel(logging.DEBUG)
601socketHandler = logging.handlers.SocketHandler('localhost',
602 logging.handlers.DEFAULT_TCP_LOGGING_PORT)
603# don't bother with a formatter, since a socket handler sends the event as
604# an unformatted pickle
605rootLogger.addHandler(socketHandler)
606
Fred Drake048840c2004-10-29 14:35:42 +0000607# Now, we can log to the root logger, or any other logger. First the root...
Vinay Sajip006483b2004-10-29 12:30:28 +0000608logging.info('Jackdaws love my big sphinx of quartz.')
609
Fred Drake048840c2004-10-29 14:35:42 +0000610# Now, define a couple of other loggers which might represent areas in your
611# application:
Vinay Sajip006483b2004-10-29 12:30:28 +0000612
613logger1 = logging.getLogger('myapp.area1')
614logger2 = logging.getLogger('myapp.area2')
615
616logger1.debug('Quick zephyrs blow, vexing daft Jim.')
617logger1.info('How quickly daft jumping zebras vex.')
618logger2.warning('Jail zesty vixen who grabbed pay from quack.')
619logger2.error('The five boxing wizards jump quickly.')
620\end{verbatim}
621
622At the receiving end, you can set up a receiver using the
623\module{SocketServer} module. Here is a basic working example:
624
625\begin{verbatim}
Fred Drake048840c2004-10-29 14:35:42 +0000626import cPickle
627import logging
628import logging.handlers
629import SocketServer
630import struct
Vinay Sajip006483b2004-10-29 12:30:28 +0000631
Vinay Sajip006483b2004-10-29 12:30:28 +0000632
Fred Drake048840c2004-10-29 14:35:42 +0000633class LogRecordStreamHandler(SocketServer.StreamRequestHandler):
634 """Handler for a streaming logging request.
635
636 This basically logs the record using whatever logging policy is
637 configured locally.
Vinay Sajip006483b2004-10-29 12:30:28 +0000638 """
639
640 def handle(self):
641 """
642 Handle multiple requests - each expected to be a 4-byte length,
643 followed by the LogRecord in pickle format. Logs the record
644 according to whatever policy is configured locally.
645 """
646 while 1:
647 chunk = self.connection.recv(4)
648 if len(chunk) < 4:
649 break
650 slen = struct.unpack(">L", chunk)[0]
651 chunk = self.connection.recv(slen)
652 while len(chunk) < slen:
653 chunk = chunk + self.connection.recv(slen - len(chunk))
654 obj = self.unPickle(chunk)
655 record = logging.makeLogRecord(obj)
656 self.handleLogRecord(record)
657
658 def unPickle(self, data):
659 return cPickle.loads(data)
660
661 def handleLogRecord(self, record):
Fred Drake048840c2004-10-29 14:35:42 +0000662 # if a name is specified, we use the named logger rather than the one
663 # implied by the record.
Vinay Sajip006483b2004-10-29 12:30:28 +0000664 if self.server.logname is not None:
665 name = self.server.logname
666 else:
667 name = record.name
668 logger = logging.getLogger(name)
Fred Drake048840c2004-10-29 14:35:42 +0000669 # N.B. EVERY record gets logged. This is because Logger.handle
670 # is normally called AFTER logger-level filtering. If you want
671 # to do filtering, do it at the client end to save wasting
672 # cycles and network bandwidth!
Vinay Sajip006483b2004-10-29 12:30:28 +0000673 logger.handle(record)
674
Fred Drake048840c2004-10-29 14:35:42 +0000675class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):
676 """simple TCP socket-based logging receiver suitable for testing.
Vinay Sajip006483b2004-10-29 12:30:28 +0000677 """
678
679 allow_reuse_address = 1
680
681 def __init__(self, host='localhost',
Fred Drake048840c2004-10-29 14:35:42 +0000682 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
683 handler=LogRecordStreamHandler):
684 SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
Vinay Sajip006483b2004-10-29 12:30:28 +0000685 self.abort = 0
686 self.timeout = 1
687 self.logname = None
688
689 def serve_until_stopped(self):
690 import select
691 abort = 0
692 while not abort:
693 rd, wr, ex = select.select([self.socket.fileno()],
694 [], [],
695 self.timeout)
696 if rd:
697 self.handle_request()
698 abort = self.abort
699
700def main():
701 logging.basicConfig(
702 format="%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s",
703 datefmt="%H:%M:%S")
704 tcpserver = LogRecordSocketReceiver()
705 print "About to start TCP server..."
706 tcpserver.serve_until_stopped()
707
708if __name__ == "__main__":
709 main()
710\end{verbatim}
711
712If you first run the server, and then the client. On the client side, nothing
713is printed on the client console; on the server side, you should see something
714like this:
715
716\begin{verbatim}
717About to start TCP server...
718 59 root INFO Jackdaws love my big sphinx of quartz.
719 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
720 69 myapp.area1 INFO How quickly daft jumping zebras vex.
721 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
722 69 myapp.area2 ERROR The five boxing wizards jump quickly.
723\end{verbatim}
724
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000725\subsection{Handler Objects}
Skip Montanaro649698f2002-11-14 03:57:19 +0000726
Fred Drake68e6d572003-01-28 22:02:35 +0000727Handlers have the following attributes and methods. Note that
728\class{Handler} is never instantiated directly; this class acts as a
729base for more useful subclasses. However, the \method{__init__()}
730method in subclasses needs to call \method{Handler.__init__()}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000731
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000732\begin{methoddesc}{__init__}{level=\constant{NOTSET}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000733Initializes the \class{Handler} instance by setting its level, setting
734the list of filters to the empty list and creating a lock (using
Raymond Hettingerc75c3e02003-09-01 22:50:52 +0000735\method{createLock()}) for serializing access to an I/O mechanism.
Skip Montanaro649698f2002-11-14 03:57:19 +0000736\end{methoddesc}
737
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000738\begin{methoddesc}{createLock}{}
739Initializes a thread lock which can be used to serialize access to
740underlying I/O functionality which may not be threadsafe.
Skip Montanaro649698f2002-11-14 03:57:19 +0000741\end{methoddesc}
742
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000743\begin{methoddesc}{acquire}{}
744Acquires the thread lock created with \method{createLock()}.
745\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000746
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000747\begin{methoddesc}{release}{}
748Releases the thread lock acquired with \method{acquire()}.
749\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000750
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000751\begin{methoddesc}{setLevel}{lvl}
752Sets the threshold for this handler to \var{lvl}. Logging messages which are
753less severe than \var{lvl} will be ignored. When a handler is created, the
Neal Norwitz6fa635d2003-02-18 14:20:07 +0000754level is set to \constant{NOTSET} (which causes all messages to be processed).
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000755\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000756
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000757\begin{methoddesc}{setFormatter}{form}
758Sets the \class{Formatter} for this handler to \var{form}.
759\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000760
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000761\begin{methoddesc}{addFilter}{filt}
762Adds the specified filter \var{filt} to this handler.
763\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000764
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000765\begin{methoddesc}{removeFilter}{filt}
766Removes the specified filter \var{filt} from this handler.
767\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000768
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000769\begin{methoddesc}{filter}{record}
770Applies this handler's filters to the record and returns a true value if
771the record is to be processed.
Skip Montanaro649698f2002-11-14 03:57:19 +0000772\end{methoddesc}
773
774\begin{methoddesc}{flush}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000775Ensure all logging output has been flushed. This version does
776nothing and is intended to be implemented by subclasses.
Skip Montanaro649698f2002-11-14 03:57:19 +0000777\end{methoddesc}
778
Skip Montanaro649698f2002-11-14 03:57:19 +0000779\begin{methoddesc}{close}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000780Tidy up any resources used by the handler. This version does
781nothing and is intended to be implemented by subclasses.
Skip Montanaro649698f2002-11-14 03:57:19 +0000782\end{methoddesc}
783
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000784\begin{methoddesc}{handle}{record}
785Conditionally emits the specified logging record, depending on
786filters which may have been added to the handler. Wraps the actual
787emission of the record with acquisition/release of the I/O thread
788lock.
Skip Montanaro649698f2002-11-14 03:57:19 +0000789\end{methoddesc}
790
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000791\begin{methoddesc}{handleError}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000792This method should be called from handlers when an exception is
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000793encountered during an \method{emit()} call. By default it does nothing,
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000794which means that exceptions get silently ignored. This is what is
795mostly wanted for a logging system - most users will not care
796about errors in the logging system, they are more interested in
797application errors. You could, however, replace this with a custom
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000798handler if you wish. The specified record is the one which was being
799processed when the exception occurred.
Skip Montanaro649698f2002-11-14 03:57:19 +0000800\end{methoddesc}
801
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000802\begin{methoddesc}{format}{record}
803Do formatting for a record - if a formatter is set, use it.
804Otherwise, use the default formatter for the module.
Skip Montanaro649698f2002-11-14 03:57:19 +0000805\end{methoddesc}
806
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000807\begin{methoddesc}{emit}{record}
808Do whatever it takes to actually log the specified logging record.
809This version is intended to be implemented by subclasses and so
810raises a \exception{NotImplementedError}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000811\end{methoddesc}
812
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000813\subsubsection{StreamHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000814
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000815The \class{StreamHandler} class sends logging output to streams such as
816\var{sys.stdout}, \var{sys.stderr} or any file-like object (or, more
817precisely, any object which supports \method{write()} and \method{flush()}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000818methods).
Skip Montanaro649698f2002-11-14 03:57:19 +0000819
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000820\begin{classdesc}{StreamHandler}{\optional{strm}}
821Returns a new instance of the \class{StreamHandler} class. If \var{strm} is
822specified, the instance will use it for logging output; otherwise,
823\var{sys.stderr} will be used.
Skip Montanaro649698f2002-11-14 03:57:19 +0000824\end{classdesc}
825
826\begin{methoddesc}{emit}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000827If a formatter is specified, it is used to format the record.
828The record is then written to the stream with a trailing newline.
829If exception information is present, it is formatted using
830\function{traceback.print_exception()} and appended to the stream.
Skip Montanaro649698f2002-11-14 03:57:19 +0000831\end{methoddesc}
832
833\begin{methoddesc}{flush}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000834Flushes the stream by calling its \method{flush()} method. Note that
835the \method{close()} method is inherited from \class{Handler} and
836so does nothing, so an explicit \method{flush()} call may be needed
837at times.
Skip Montanaro649698f2002-11-14 03:57:19 +0000838\end{methoddesc}
839
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000840\subsubsection{FileHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000841
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000842The \class{FileHandler} class sends logging output to a disk file.
Fred Drake68e6d572003-01-28 22:02:35 +0000843It inherits the output functionality from \class{StreamHandler}.
Skip Montanaro649698f2002-11-14 03:57:19 +0000844
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000845\begin{classdesc}{FileHandler}{filename\optional{, mode}}
846Returns a new instance of the \class{FileHandler} class. The specified
847file is opened and used as the stream for logging. If \var{mode} is
Fred Drake9a5b6a62003-07-08 15:38:40 +0000848not specified, \constant{'a'} is used. By default, the file grows
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000849indefinitely.
Skip Montanaro649698f2002-11-14 03:57:19 +0000850\end{classdesc}
851
852\begin{methoddesc}{close}{}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000853Closes the file.
Skip Montanaro649698f2002-11-14 03:57:19 +0000854\end{methoddesc}
855
856\begin{methoddesc}{emit}{record}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000857Outputs the record to the file.
858\end{methoddesc}
Skip Montanaro649698f2002-11-14 03:57:19 +0000859
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000860\subsubsection{RotatingFileHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +0000861
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000862The \class{RotatingFileHandler} class supports rotation of disk log files.
863
Fred Drake9a5b6a62003-07-08 15:38:40 +0000864\begin{classdesc}{RotatingFileHandler}{filename\optional{, mode\optional{,
865 maxBytes\optional{, backupCount}}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000866Returns a new instance of the \class{RotatingFileHandler} class. The
867specified file is opened and used as the stream for logging. If
Fred Drake68e6d572003-01-28 22:02:35 +0000868\var{mode} is not specified, \code{'a'} is used. By default, the
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000869file grows indefinitely.
Andrew M. Kuchling7cf4d9b2003-09-26 13:45:18 +0000870
871You can use the \var{maxBytes} and
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000872\var{backupCount} values to allow the file to \dfn{rollover} at a
873predetermined size. When the size is about to be exceeded, the file is
Andrew M. Kuchling7cf4d9b2003-09-26 13:45:18 +0000874closed and a new file is silently opened for output. Rollover occurs
875whenever the current log file is nearly \var{maxBytes} in length; if
876\var{maxBytes} is zero, rollover never occurs. If \var{backupCount}
877is non-zero, the system will save old log files by appending the
878extensions ".1", ".2" etc., to the filename. For example, with
879a \var{backupCount} of 5 and a base file name of
880\file{app.log}, you would get \file{app.log},
881\file{app.log.1}, \file{app.log.2}, up to \file{app.log.5}. The file being
882written to is always \file{app.log}. When this file is filled, it is
883closed and renamed to \file{app.log.1}, and if files \file{app.log.1},
884\file{app.log.2}, etc. exist, then they are renamed to \file{app.log.2},
Vinay Sajipa13c60b2004-07-03 11:45:53 +0000885\file{app.log.3} etc. respectively.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000886\end{classdesc}
887
888\begin{methoddesc}{doRollover}{}
889Does a rollover, as described above.
890\end{methoddesc}
891
892\begin{methoddesc}{emit}{record}
893Outputs the record to the file, catering for rollover as described
894in \method{setRollover()}.
895\end{methoddesc}
896
Johannes Gijsbers4f802ac2004-11-07 14:14:27 +0000897\subsubsection{TimedRotatingFileHandler}
898
899The \class{TimedRotatingFileHandler} class supports rotation of disk log files
900at certain timed intervals.
901
902\begin{classdesc}{TimedRotatingFileHandler}{filename
903 \optional{,when
904 \optional{,interval
905 \optional{,backupCount}}}}
906
907Returns a new instance of the \class{TimedRotatingFileHandler} class. The
908specified file is opened and used as the stream for logging. On rotating
909it also sets the filename suffix. Rotating happens based on the product
910of \var{when} and \var{interval}.
911
912You can use the \var{when} to specify the type of \var{interval}. The
913list of possible values is, note that they are not case sensitive:
914
915\begin{tableii}{l|l}{}{Value}{Type of interval}
916 \lineii{S}{Seconds}
917 \lineii{M}{Minutes}
918 \lineii{H}{Hours}
919 \lineii{D}{Days}
920 \lineii{W}{Week day (0=Monday)}
921 \lineii{midnight}{Roll over at midnight}
922\end{tableii}
923
924If \var{backupCount} is non-zero, the system will save old log files by
925appending the extensions ".1", ".2" etc., to the filename. For example,
926with a \var{backupCount} of 5 and a base file name of \file{app.log},
927you would get \file{app.log}, \file{app.log.1}, \file{app.log.2}, up to
928\file{app.log.5}. The file being written to is always \file{app.log}.
929When this file is filled, it is closed and renamed to \file{app.log.1},
930and if files \file{app.log.1}, \file{app.log.2}, etc. exist, then they
931are renamed to \file{app.log.2}, \file{app.log.3} etc. respectively.
932\end{classdesc}
933
934\begin{methoddesc}{doRollover}{}
935Does a rollover, as described above.
936\end{methoddesc}
937
938\begin{methoddesc}{emit}{record}
939Outputs the record to the file, catering for rollover as described
940above.
941\end{methoddesc}
942
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000943\subsubsection{SocketHandler}
944
945The \class{SocketHandler} class sends logging output to a network
946socket. The base class uses a TCP socket.
947
948\begin{classdesc}{SocketHandler}{host, port}
949Returns a new instance of the \class{SocketHandler} class intended to
950communicate with a remote machine whose address is given by \var{host}
951and \var{port}.
952\end{classdesc}
953
954\begin{methoddesc}{close}{}
955Closes the socket.
956\end{methoddesc}
957
958\begin{methoddesc}{handleError}{}
959\end{methoddesc}
960
961\begin{methoddesc}{emit}{}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000962Pickles the record's attribute dictionary and writes it to the socket in
963binary format. If there is an error with the socket, silently drops the
964packet. If the connection was previously lost, re-establishes the connection.
Fred Drake6b3b0462004-04-09 18:26:40 +0000965To unpickle the record at the receiving end into a \class{LogRecord}, use the
966\function{makeLogRecord()} function.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000967\end{methoddesc}
968
969\begin{methoddesc}{handleError}{}
970Handles an error which has occurred during \method{emit()}. The
971most likely cause is a lost connection. Closes the socket so that
972we can retry on the next event.
973\end{methoddesc}
974
975\begin{methoddesc}{makeSocket}{}
976This is a factory method which allows subclasses to define the precise
977type of socket they want. The default implementation creates a TCP
978socket (\constant{socket.SOCK_STREAM}).
979\end{methoddesc}
980
981\begin{methoddesc}{makePickle}{record}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +0000982Pickles the record's attribute dictionary in binary format with a length
983prefix, and returns it ready for transmission across the socket.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000984\end{methoddesc}
985
986\begin{methoddesc}{send}{packet}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +0000987Send a pickled string \var{packet} to the socket. This function allows
Neal Norwitzcd5c8c22003-01-25 21:29:41 +0000988for partial sends which can happen when the network is busy.
989\end{methoddesc}
990
991\subsubsection{DatagramHandler}
992
993The \class{DatagramHandler} class inherits from \class{SocketHandler}
994to support sending logging messages over UDP sockets.
995
996\begin{classdesc}{DatagramHandler}{host, port}
997Returns a new instance of the \class{DatagramHandler} class intended to
998communicate with a remote machine whose address is given by \var{host}
999and \var{port}.
1000\end{classdesc}
1001
1002\begin{methoddesc}{emit}{}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001003Pickles the record's attribute dictionary and writes it to the socket in
1004binary format. If there is an error with the socket, silently drops the
1005packet.
Fred Drake6b3b0462004-04-09 18:26:40 +00001006To unpickle the record at the receiving end into a \class{LogRecord}, use the
1007\function{makeLogRecord()} function.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001008\end{methoddesc}
1009
1010\begin{methoddesc}{makeSocket}{}
1011The factory method of \class{SocketHandler} is here overridden to create
1012a UDP socket (\constant{socket.SOCK_DGRAM}).
1013\end{methoddesc}
1014
1015\begin{methoddesc}{send}{s}
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001016Send a pickled string to a socket.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001017\end{methoddesc}
1018
1019\subsubsection{SysLogHandler}
1020
1021The \class{SysLogHandler} class supports sending logging messages to a
Fred Drake68e6d572003-01-28 22:02:35 +00001022remote or local \UNIX{} syslog.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001023
1024\begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}}
1025Returns a new instance of the \class{SysLogHandler} class intended to
Fred Drake68e6d572003-01-28 22:02:35 +00001026communicate with a remote \UNIX{} machine whose address is given by
1027\var{address} in the form of a \code{(\var{host}, \var{port})}
1028tuple. If \var{address} is not specified, \code{('localhost', 514)} is
1029used. The address is used to open a UDP socket. If \var{facility} is
1030not specified, \constant{LOG_USER} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001031\end{classdesc}
1032
1033\begin{methoddesc}{close}{}
1034Closes the socket to the remote host.
1035\end{methoddesc}
1036
1037\begin{methoddesc}{emit}{record}
1038The record is formatted, and then sent to the syslog server. If
1039exception information is present, it is \emph{not} sent to the server.
Skip Montanaro649698f2002-11-14 03:57:19 +00001040\end{methoddesc}
1041
1042\begin{methoddesc}{encodePriority}{facility, priority}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001043Encodes the facility and priority into an integer. You can pass in strings
1044or integers - if strings are passed, internal mapping dictionaries are used
1045to convert them to integers.
Skip Montanaro649698f2002-11-14 03:57:19 +00001046\end{methoddesc}
1047
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001048\subsubsection{NTEventLogHandler}
Skip Montanaro649698f2002-11-14 03:57:19 +00001049
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001050The \class{NTEventLogHandler} class supports sending logging messages
1051to a local Windows NT, Windows 2000 or Windows XP event log. Before
1052you can use it, you need Mark Hammond's Win32 extensions for Python
1053installed.
Skip Montanaro649698f2002-11-14 03:57:19 +00001054
Fred Drake9a5b6a62003-07-08 15:38:40 +00001055\begin{classdesc}{NTEventLogHandler}{appname\optional{,
1056 dllname\optional{, logtype}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001057Returns a new instance of the \class{NTEventLogHandler} class. The
1058\var{appname} is used to define the application name as it appears in the
1059event log. An appropriate registry entry is created using this name.
1060The \var{dllname} should give the fully qualified pathname of a .dll or .exe
1061which contains message definitions to hold in the log (if not specified,
Fred Drake9a5b6a62003-07-08 15:38:40 +00001062\code{'win32service.pyd'} is used - this is installed with the Win32
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001063extensions and contains some basic placeholder message definitions.
1064Note that use of these placeholders will make your event logs big, as the
1065entire message source is held in the log. If you want slimmer logs, you have
1066to pass in the name of your own .dll or .exe which contains the message
1067definitions you want to use in the event log). The \var{logtype} is one of
Fred Drake9a5b6a62003-07-08 15:38:40 +00001068\code{'Application'}, \code{'System'} or \code{'Security'}, and
1069defaults to \code{'Application'}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001070\end{classdesc}
1071
1072\begin{methoddesc}{close}{}
1073At this point, you can remove the application name from the registry as a
1074source of event log entries. However, if you do this, you will not be able
1075to see the events as you intended in the Event Log Viewer - it needs to be
1076able to access the registry to get the .dll name. The current version does
1077not do this (in fact it doesn't do anything).
1078\end{methoddesc}
1079
1080\begin{methoddesc}{emit}{record}
1081Determines the message ID, event category and event type, and then logs the
1082message in the NT event log.
1083\end{methoddesc}
1084
1085\begin{methoddesc}{getEventCategory}{record}
1086Returns the event category for the record. Override this if you
1087want to specify your own categories. This version returns 0.
1088\end{methoddesc}
1089
1090\begin{methoddesc}{getEventType}{record}
1091Returns the event type for the record. Override this if you want
1092to specify your own types. This version does a mapping using the
1093handler's typemap attribute, which is set up in \method{__init__()}
1094to a dictionary which contains mappings for \constant{DEBUG},
1095\constant{INFO}, \constant{WARNING}, \constant{ERROR} and
1096\constant{CRITICAL}. If you are using your own levels, you will either need
1097to override this method or place a suitable dictionary in the
1098handler's \var{typemap} attribute.
1099\end{methoddesc}
1100
1101\begin{methoddesc}{getMessageID}{record}
1102Returns the message ID for the record. If you are using your
1103own messages, you could do this by having the \var{msg} passed to the
1104logger being an ID rather than a format string. Then, in here,
1105you could use a dictionary lookup to get the message ID. This
1106version returns 1, which is the base message ID in
Fred Drake9a5b6a62003-07-08 15:38:40 +00001107\file{win32service.pyd}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001108\end{methoddesc}
1109
1110\subsubsection{SMTPHandler}
1111
1112The \class{SMTPHandler} class supports sending logging messages to an email
1113address via SMTP.
1114
1115\begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject}
1116Returns a new instance of the \class{SMTPHandler} class. The
1117instance is initialized with the from and to addresses and subject
1118line of the email. The \var{toaddrs} should be a list of strings without
1119domain names (That's what the \var{mailhost} is for). To specify a
1120non-standard SMTP port, use the (host, port) tuple format for the
1121\var{mailhost} argument. If you use a string, the standard SMTP port
1122is used.
1123\end{classdesc}
1124
1125\begin{methoddesc}{emit}{record}
1126Formats the record and sends it to the specified addressees.
1127\end{methoddesc}
1128
1129\begin{methoddesc}{getSubject}{record}
1130If you want to specify a subject line which is record-dependent,
1131override this method.
1132\end{methoddesc}
1133
1134\subsubsection{MemoryHandler}
1135
1136The \class{MemoryHandler} supports buffering of logging records in memory,
1137periodically flushing them to a \dfn{target} handler. Flushing occurs
1138whenever the buffer is full, or when an event of a certain severity or
1139greater is seen.
1140
1141\class{MemoryHandler} is a subclass of the more general
1142\class{BufferingHandler}, which is an abstract class. This buffers logging
1143records in memory. Whenever each record is added to the buffer, a
1144check is made by calling \method{shouldFlush()} to see if the buffer
1145should be flushed. If it should, then \method{flush()} is expected to
1146do the needful.
1147
1148\begin{classdesc}{BufferingHandler}{capacity}
1149Initializes the handler with a buffer of the specified capacity.
1150\end{classdesc}
1151
1152\begin{methoddesc}{emit}{record}
1153Appends the record to the buffer. If \method{shouldFlush()} returns true,
1154calls \method{flush()} to process the buffer.
1155\end{methoddesc}
1156
1157\begin{methoddesc}{flush}{}
Raymond Hettinger2ef85a72003-01-25 21:46:53 +00001158You can override this to implement custom flushing behavior. This version
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001159just zaps the buffer to empty.
1160\end{methoddesc}
1161
1162\begin{methoddesc}{shouldFlush}{record}
1163Returns true if the buffer is up to capacity. This method can be
1164overridden to implement custom flushing strategies.
1165\end{methoddesc}
1166
1167\begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel
Neal Norwitz6fa635d2003-02-18 14:20:07 +00001168\optional{, target}}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001169Returns a new instance of the \class{MemoryHandler} class. The
1170instance is initialized with a buffer size of \var{capacity}. If
1171\var{flushLevel} is not specified, \constant{ERROR} is used. If no
1172\var{target} is specified, the target will need to be set using
1173\method{setTarget()} before this handler does anything useful.
1174\end{classdesc}
1175
1176\begin{methoddesc}{close}{}
1177Calls \method{flush()}, sets the target to \constant{None} and
1178clears the buffer.
1179\end{methoddesc}
1180
1181\begin{methoddesc}{flush}{}
1182For a \class{MemoryHandler}, flushing means just sending the buffered
1183records to the target, if there is one. Override if you want
Raymond Hettinger2ef85a72003-01-25 21:46:53 +00001184different behavior.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001185\end{methoddesc}
1186
1187\begin{methoddesc}{setTarget}{target}
1188Sets the target handler for this handler.
1189\end{methoddesc}
1190
1191\begin{methoddesc}{shouldFlush}{record}
1192Checks for buffer full or a record at the \var{flushLevel} or higher.
1193\end{methoddesc}
1194
1195\subsubsection{HTTPHandler}
1196
1197The \class{HTTPHandler} class supports sending logging messages to a
Fred Drake68e6d572003-01-28 22:02:35 +00001198Web server, using either \samp{GET} or \samp{POST} semantics.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001199
1200\begin{classdesc}{HTTPHandler}{host, url\optional{, method}}
1201Returns a new instance of the \class{HTTPHandler} class. The
1202instance is initialized with a host address, url and HTTP method.
Fred Drake68e6d572003-01-28 22:02:35 +00001203If no \var{method} is specified, \samp{GET} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001204\end{classdesc}
1205
1206\begin{methoddesc}{emit}{record}
1207Sends the record to the Web server as an URL-encoded dictionary.
1208\end{methoddesc}
1209
1210\subsection{Formatter Objects}
1211
1212\class{Formatter}s have the following attributes and methods. They are
1213responsible for converting a \class{LogRecord} to (usually) a string
1214which can be interpreted by either a human or an external system. The
1215base
1216\class{Formatter} allows a formatting string to be specified. If none is
Fred Drake8efc74d2004-04-15 06:18:48 +00001217supplied, the default value of \code{'\%(message)s'} is used.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001218
1219A Formatter can be initialized with a format string which makes use of
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001220knowledge of the \class{LogRecord} attributes - such as the default value
1221mentioned above making use of the fact that the user's message and
Fred Drake6b3b0462004-04-09 18:26:40 +00001222arguments are pre-formatted into a \class{LogRecord}'s \var{message}
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001223attribute. This format string contains standard python \%-style
1224mapping keys. See section \ref{typesseq-strings}, ``String Formatting
1225Operations,'' for more information on string formatting.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001226
Fred Drake6b3b0462004-04-09 18:26:40 +00001227Currently, the useful mapping keys in a \class{LogRecord} are:
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001228
Fred Drake9a5b6a62003-07-08 15:38:40 +00001229\begin{tableii}{l|l}{code}{Format}{Description}
1230\lineii{\%(name)s} {Name of the logger (logging channel).}
1231\lineii{\%(levelno)s} {Numeric logging level for the message
1232 (\constant{DEBUG}, \constant{INFO},
1233 \constant{WARNING}, \constant{ERROR},
1234 \constant{CRITICAL}).}
1235\lineii{\%(levelname)s}{Text logging level for the message
1236 (\code{'DEBUG'}, \code{'INFO'},
1237 \code{'WARNING'}, \code{'ERROR'},
1238 \code{'CRITICAL'}).}
1239\lineii{\%(pathname)s} {Full pathname of the source file where the logging
1240 call was issued (if available).}
1241\lineii{\%(filename)s} {Filename portion of pathname.}
1242\lineii{\%(module)s} {Module (name portion of filename).}
1243\lineii{\%(lineno)d} {Source line number where the logging call was issued
1244 (if available).}
Fred Drake6b3b0462004-04-09 18:26:40 +00001245\lineii{\%(created)f} {Time when the \class{LogRecord} was created (as
Fred Drake9a5b6a62003-07-08 15:38:40 +00001246 returned by \function{time.time()}).}
Fred Drake6b3b0462004-04-09 18:26:40 +00001247\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
1248 was created. By default this is of the form
Fred Drake9a5b6a62003-07-08 15:38:40 +00001249 ``2003-07-08 16:49:45,896'' (the numbers after the
1250 comma are millisecond portion of the time).}
1251\lineii{\%(msecs)d} {Millisecond portion of the time when the
1252 \class{LogRecord} was created.}
1253\lineii{\%(thread)d} {Thread ID (if available).}
1254\lineii{\%(process)d} {Process ID (if available).}
1255\lineii{\%(message)s} {The logged message, computed as \code{msg \% args}.}
Anthony Baxtera6b7d342003-07-08 08:40:20 +00001256\end{tableii}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001257
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001258\begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}}
1259Returns a new instance of the \class{Formatter} class. The
1260instance is initialized with a format string for the message as a whole,
1261as well as a format string for the date/time portion of a message. If
Neal Norwitzdd3afa72003-07-08 16:26:34 +00001262no \var{fmt} is specified, \code{'\%(message)s'} is used. If no \var{datefmt}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001263is specified, the ISO8601 date format is used.
1264\end{classdesc}
1265
1266\begin{methoddesc}{format}{record}
1267The record's attribute dictionary is used as the operand to a
1268string formatting operation. Returns the resulting string.
1269Before formatting the dictionary, a couple of preparatory steps
1270are carried out. The \var{message} attribute of the record is computed
1271using \var{msg} \% \var{args}. If the formatting string contains
Fred Drake9a5b6a62003-07-08 15:38:40 +00001272\code{'(asctime)'}, \method{formatTime()} is called to format the
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001273event time. If there is exception information, it is formatted using
1274\method{formatException()} and appended to the message.
1275\end{methoddesc}
1276
1277\begin{methoddesc}{formatTime}{record\optional{, datefmt}}
1278This method should be called from \method{format()} by a formatter which
1279wants to make use of a formatted time. This method can be overridden
1280in formatters to provide for any specific requirement, but the
Raymond Hettinger2ef85a72003-01-25 21:46:53 +00001281basic behavior is as follows: if \var{datefmt} (a string) is specified,
Fred Drakec23e0192003-01-28 22:09:16 +00001282it is used with \function{time.strftime()} to format the creation time of the
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001283record. Otherwise, the ISO8601 format is used. The resulting
1284string is returned.
1285\end{methoddesc}
1286
1287\begin{methoddesc}{formatException}{exc_info}
1288Formats the specified exception information (a standard exception tuple
Fred Drakec23e0192003-01-28 22:09:16 +00001289as returned by \function{sys.exc_info()}) as a string. This default
1290implementation just uses \function{traceback.print_exception()}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001291The resulting string is returned.
1292\end{methoddesc}
1293
1294\subsection{Filter Objects}
1295
1296\class{Filter}s can be used by \class{Handler}s and \class{Logger}s for
1297more sophisticated filtering than is provided by levels. The base filter
1298class only allows events which are below a certain point in the logger
1299hierarchy. For example, a filter initialized with "A.B" will allow events
1300logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB",
1301"B.A.B" etc. If initialized with the empty string, all events are passed.
1302
1303\begin{classdesc}{Filter}{\optional{name}}
1304Returns an instance of the \class{Filter} class. If \var{name} is specified,
1305it names a logger which, together with its children, will have its events
1306allowed through the filter. If no name is specified, allows every event.
1307\end{classdesc}
1308
1309\begin{methoddesc}{filter}{record}
1310Is the specified record to be logged? Returns zero for no, nonzero for
1311yes. If deemed appropriate, the record may be modified in-place by this
1312method.
1313\end{methoddesc}
1314
1315\subsection{LogRecord Objects}
1316
Fred Drake6b3b0462004-04-09 18:26:40 +00001317\class{LogRecord} instances are created every time something is logged. They
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001318contain all the information pertinent to the event being logged. The
1319main information passed in is in msg and args, which are combined
1320using msg \% args to create the message field of the record. The record
1321also includes information such as when the record was created, the
1322source line where the logging call was made, and any exception
1323information to be logged.
1324
Fred Drake6b3b0462004-04-09 18:26:40 +00001325\class{LogRecord} has no methods; it's just a repository for
1326information about the logging event. The only reason it's a class
1327rather than a dictionary is to facilitate extension.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001328
1329\begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args,
Fred Drake9a5b6a62003-07-08 15:38:40 +00001330 exc_info}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001331Returns an instance of \class{LogRecord} initialized with interesting
1332information. The \var{name} is the logger name; \var{lvl} is the
1333numeric level; \var{pathname} is the absolute pathname of the source
1334file in which the logging call was made; \var{lineno} is the line
1335number in that file where the logging call is found; \var{msg} is the
1336user-supplied message (a format string); \var{args} is the tuple
1337which, together with \var{msg}, makes up the user message; and
1338\var{exc_info} is the exception tuple obtained by calling
1339\function{sys.exc_info() }(or \constant{None}, if no exception information
1340is available).
1341\end{classdesc}
1342
1343\subsection{Thread Safety}
1344
1345The logging module is intended to be thread-safe without any special work
1346needing to be done by its clients. It achieves this though using threading
1347locks; there is one lock to serialize access to the module's shared data,
1348and each handler also creates a lock to serialize access to its underlying
1349I/O.
1350
1351\subsection{Configuration}
1352
1353
Fred Drake94ffbb72004-04-08 19:44:31 +00001354\subsubsection{Configuration functions%
1355 \label{logging-config-api}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001356
Fred Drake9a5b6a62003-07-08 15:38:40 +00001357The following functions allow the logging module to be
1358configured. Before they can be used, you must import
1359\module{logging.config}. Their use is optional --- you can configure
1360the logging module entirely by making calls to the main API (defined
1361in \module{logging} itself) and defining handlers which are declared
Raymond Hettinger6f3eaa62003-06-27 21:43:39 +00001362either in \module{logging} or \module{logging.handlers}.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001363
1364\begin{funcdesc}{fileConfig}{fname\optional{, defaults}}
1365Reads the logging configuration from a ConfigParser-format file named
1366\var{fname}. This function can be called several times from an application,
1367allowing an end user the ability to select from various pre-canned
1368configurations (if the developer provides a mechanism to present the
1369choices and load the chosen configuration). Defaults to be passed to
1370ConfigParser can be specified in the \var{defaults} argument.
1371\end{funcdesc}
1372
1373\begin{funcdesc}{listen}{\optional{port}}
1374Starts up a socket server on the specified port, and listens for new
1375configurations. If no port is specified, the module's default
1376\constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations
1377will be sent as a file suitable for processing by \function{fileConfig()}.
1378Returns a \class{Thread} instance on which you can call \method{start()}
1379to start the server, and which you can \method{join()} when appropriate.
1380To stop the server, call \function{stopListening()}.
1381\end{funcdesc}
1382
1383\begin{funcdesc}{stopListening}{}
1384Stops the listening server which was created with a call to
1385\function{listen()}. This is typically called before calling \method{join()}
1386on the return value from \function{listen()}.
1387\end{funcdesc}
1388
Fred Drake94ffbb72004-04-08 19:44:31 +00001389\subsubsection{Configuration file format%
1390 \label{logging-config-fileformat}}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001391
Fred Drake6b3b0462004-04-09 18:26:40 +00001392The configuration file format understood by \function{fileConfig()} is
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001393based on ConfigParser functionality. The file must contain sections
1394called \code{[loggers]}, \code{[handlers]} and \code{[formatters]}
1395which identify by name the entities of each type which are defined in
1396the file. For each such entity, there is a separate section which
1397identified how that entity is configured. Thus, for a logger named
1398\code{log01} in the \code{[loggers]} section, the relevant
1399configuration details are held in a section
1400\code{[logger_log01]}. Similarly, a handler called \code{hand01} in
1401the \code{[handlers]} section will have its configuration held in a
1402section called \code{[handler_hand01]}, while a formatter called
1403\code{form01} in the \code{[formatters]} section will have its
1404configuration specified in a section called
1405\code{[formatter_form01]}. The root logger configuration must be
1406specified in a section called \code{[logger_root]}.
1407
1408Examples of these sections in the file are given below.
Skip Montanaro649698f2002-11-14 03:57:19 +00001409
1410\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001411[loggers]
1412keys=root,log02,log03,log04,log05,log06,log07
Skip Montanaro649698f2002-11-14 03:57:19 +00001413
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001414[handlers]
1415keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
1416
1417[formatters]
1418keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
Skip Montanaro649698f2002-11-14 03:57:19 +00001419\end{verbatim}
1420
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001421The root logger must specify a level and a list of handlers. An
1422example of a root logger section is given below.
Skip Montanaro649698f2002-11-14 03:57:19 +00001423
1424\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001425[logger_root]
1426level=NOTSET
1427handlers=hand01
Skip Montanaro649698f2002-11-14 03:57:19 +00001428\end{verbatim}
1429
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001430The \code{level} entry can be one of \code{DEBUG, INFO, WARNING,
1431ERROR, CRITICAL} or \code{NOTSET}. For the root logger only,
1432\code{NOTSET} means that all messages will be logged. Level values are
1433\function{eval()}uated in the context of the \code{logging} package's
1434namespace.
Skip Montanaro649698f2002-11-14 03:57:19 +00001435
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001436The \code{handlers} entry is a comma-separated list of handler names,
1437which must appear in the \code{[handlers]} section. These names must
1438appear in the \code{[handlers]} section and have corresponding
1439sections in the configuration file.
Skip Montanaro649698f2002-11-14 03:57:19 +00001440
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001441For loggers other than the root logger, some additional information is
1442required. This is illustrated by the following example.
Skip Montanaro649698f2002-11-14 03:57:19 +00001443
1444\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001445[logger_parser]
1446level=DEBUG
1447handlers=hand01
1448propagate=1
1449qualname=compiler.parser
Skip Montanaro649698f2002-11-14 03:57:19 +00001450\end{verbatim}
1451
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001452The \code{level} and \code{handlers} entries are interpreted as for
1453the root logger, except that if a non-root logger's level is specified
1454as \code{NOTSET}, the system consults loggers higher up the hierarchy
1455to determine the effective level of the logger. The \code{propagate}
1456entry is set to 1 to indicate that messages must propagate to handlers
1457higher up the logger hierarchy from this logger, or 0 to indicate that
1458messages are \strong{not} propagated to handlers up the hierarchy. The
1459\code{qualname} entry is the hierarchical channel name of the logger,
Vinay Sajipa13c60b2004-07-03 11:45:53 +00001460that is to say the name used by the application to get the logger.
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001461
1462Sections which specify handler configuration are exemplified by the
1463following.
1464
Skip Montanaro649698f2002-11-14 03:57:19 +00001465\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001466[handler_hand01]
1467class=StreamHandler
1468level=NOTSET
1469formatter=form01
1470args=(sys.stdout,)
Skip Montanaro649698f2002-11-14 03:57:19 +00001471\end{verbatim}
1472
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001473The \code{class} entry indicates the handler's class (as determined by
1474\function{eval()} in the \code{logging} package's namespace). The
1475\code{level} is interpreted as for loggers, and \code{NOTSET} is taken
1476to mean "log everything".
1477
1478The \code{formatter} entry indicates the key name of the formatter for
1479this handler. If blank, a default formatter
1480(\code{logging._defaultFormatter}) is used. If a name is specified, it
1481must appear in the \code{[formatters]} section and have a
1482corresponding section in the configuration file.
1483
1484The \code{args} entry, when \function{eval()}uated in the context of
1485the \code{logging} package's namespace, is the list of arguments to
1486the constructor for the handler class. Refer to the constructors for
1487the relevant handlers, or to the examples below, to see how typical
1488entries are constructed.
Skip Montanaro649698f2002-11-14 03:57:19 +00001489
1490\begin{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001491[handler_hand02]
1492class=FileHandler
1493level=DEBUG
1494formatter=form02
1495args=('python.log', 'w')
1496
1497[handler_hand03]
1498class=handlers.SocketHandler
1499level=INFO
1500formatter=form03
1501args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
1502
1503[handler_hand04]
1504class=handlers.DatagramHandler
1505level=WARN
1506formatter=form04
1507args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)
1508
1509[handler_hand05]
1510class=handlers.SysLogHandler
1511level=ERROR
1512formatter=form05
1513args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
1514
1515[handler_hand06]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001516class=handlers.NTEventLogHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001517level=CRITICAL
1518formatter=form06
1519args=('Python Application', '', 'Application')
1520
1521[handler_hand07]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001522class=handlers.SMTPHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001523level=WARN
1524formatter=form07
1525args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
1526
1527[handler_hand08]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001528class=handlers.MemoryHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001529level=NOTSET
1530formatter=form08
1531target=
1532args=(10, ERROR)
1533
1534[handler_hand09]
Vinay Sajip20f42c42004-07-12 15:48:04 +00001535class=handlers.HTTPHandler
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001536level=NOTSET
1537formatter=form09
1538args=('localhost:9022', '/log', 'GET')
Skip Montanaro649698f2002-11-14 03:57:19 +00001539\end{verbatim}
Neal Norwitzcd5c8c22003-01-25 21:29:41 +00001540
1541Sections which specify formatter configuration are typified by the following.
1542
1543\begin{verbatim}
1544[formatter_form01]
1545format=F1 %(asctime)s %(levelname)s %(message)s
1546datefmt=
1547\end{verbatim}
1548
1549The \code{format} entry is the overall format string, and the
1550\code{datefmt} entry is the \function{strftime()}-compatible date/time format
1551string. If empty, the package substitutes ISO8601 format date/times, which
1552is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S".
1553The ISO8601 format also specifies milliseconds, which are appended to the
1554result of using the above format string, with a comma separator. An example
1555time in ISO8601 format is \code{2003-01-23 00:29:50,411}.