2002-11-13 23:57:19 -04:00
|
|
|
\section{\module{logging} ---
|
|
|
|
Logging facility for Python}
|
|
|
|
|
2003-07-08 12:38:40 -03:00
|
|
|
\declaremodule{standard}{logging}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
% These apply to all modules, and may be given more than once:
|
|
|
|
|
|
|
|
\moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com}
|
2003-01-25 17:29:41 -04:00
|
|
|
\sectionauthor{Vinay Sajip}{vinay_sajip@red-dove.com}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-28 18:02:35 -04:00
|
|
|
\modulesynopsis{Logging module for Python based on \pep{282}.}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\indexii{Errors}{logging}
|
|
|
|
|
|
|
|
\versionadded{2.3}
|
|
|
|
This module defines functions and classes which implement a flexible
|
|
|
|
error logging system for applications.
|
|
|
|
|
|
|
|
Logging is performed by calling methods on instances of the
|
|
|
|
\class{Logger} class (hereafter called \dfn{loggers}). Each instance has a
|
|
|
|
name, and they are conceptually arranged in a name space hierarchy
|
|
|
|
using dots (periods) as separators. For example, a logger named
|
|
|
|
"scan" is the parent of loggers "scan.text", "scan.html" and "scan.pdf".
|
|
|
|
Logger names can be anything you want, and indicate the area of an
|
|
|
|
application in which a logged message originates.
|
|
|
|
|
|
|
|
Logged messages also have levels of importance associated with them.
|
|
|
|
The default levels provided are \constant{DEBUG}, \constant{INFO},
|
|
|
|
\constant{WARNING}, \constant{ERROR} and \constant{CRITICAL}. As a
|
|
|
|
convenience, you indicate the importance of a logged message by calling
|
|
|
|
an appropriate method of \class{Logger}. The methods are
|
2003-01-28 18:09:16 -04:00
|
|
|
\method{debug()}, \method{info()}, \method{warning()}, \method{error()} and
|
|
|
|
\method{critical()}, which mirror the default levels. You are not
|
|
|
|
constrained to use these levels: you can specify your own and use a
|
|
|
|
more general \class{Logger} method, \method{log()}, which takes an
|
2003-01-25 17:29:41 -04:00
|
|
|
explicit level argument.
|
|
|
|
|
2004-12-02 17:27:42 -04:00
|
|
|
The numeric values of logging levels are given in the following table. These
|
|
|
|
are primarily of interest if you want to define your own levels, and need
|
|
|
|
them to have specific values relative to the predefined levels. If you
|
|
|
|
define a level with the same numeric value, it overwrites the predefined
|
|
|
|
value; the predefined name is lost.
|
|
|
|
|
|
|
|
\begin{tableii}{l|l}{code}{Level}{Numeric value}
|
|
|
|
\lineii{CRITICAL}{50}
|
|
|
|
\lineii{ERROR}{40}
|
|
|
|
\lineii{WARNING}{30}
|
|
|
|
\lineii{INFO}{20}
|
|
|
|
\lineii{DEBUG}{10}
|
|
|
|
\lineii{NOTSET}{0}
|
|
|
|
\end{tableii}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
Levels can also be associated with loggers, being set either by the
|
|
|
|
developer or through loading a saved logging configuration. When a
|
|
|
|
logging method is called on a logger, the logger compares its own
|
|
|
|
level with the level associated with the method call. If the logger's
|
|
|
|
level is higher than the method call's, no logging message is actually
|
|
|
|
generated. This is the basic mechanism controlling the verbosity of
|
|
|
|
logging output.
|
|
|
|
|
|
|
|
Logging messages are encoded as instances of the \class{LogRecord} class.
|
2006-01-01 17:35:20 -04:00
|
|
|
When a logger decides to actually log an event, a \class{LogRecord}
|
2003-01-25 17:29:41 -04:00
|
|
|
instance is created from the logging message.
|
|
|
|
|
|
|
|
Logging messages are subjected to a dispatch mechanism through the
|
|
|
|
use of \dfn{handlers}, which are instances of subclasses of the
|
|
|
|
\class{Handler} class. Handlers are responsible for ensuring that a logged
|
|
|
|
message (in the form of a \class{LogRecord}) ends up in a particular
|
|
|
|
location (or set of locations) which is useful for the target audience for
|
2003-06-27 18:43:39 -03:00
|
|
|
that message (such as end users, support desk staff, system administrators,
|
2003-01-25 17:29:41 -04:00
|
|
|
developers). Handlers are passed \class{LogRecord} instances intended for
|
|
|
|
particular destinations. Each logger can have zero, one or more handlers
|
2004-04-09 15:26:40 -03:00
|
|
|
associated with it (via the \method{addHandler()} method of \class{Logger}).
|
2003-01-25 17:29:41 -04:00
|
|
|
In addition to any handlers directly associated with a logger,
|
2003-01-28 18:09:16 -04:00
|
|
|
\emph{all handlers associated with all ancestors of the logger} are
|
|
|
|
called to dispatch the message.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
Just as for loggers, handlers can have levels associated with them.
|
|
|
|
A handler's level acts as a filter in the same way as a logger's level does.
|
2003-01-28 18:09:16 -04:00
|
|
|
If a handler decides to actually dispatch an event, the \method{emit()} method
|
2003-01-25 17:29:41 -04:00
|
|
|
is used to send the message to its destination. Most user-defined subclasses
|
2003-01-28 18:09:16 -04:00
|
|
|
of \class{Handler} will need to override this \method{emit()}.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
In addition to the base \class{Handler} class, many useful subclasses
|
|
|
|
are provided:
|
|
|
|
|
|
|
|
\begin{enumerate}
|
|
|
|
|
|
|
|
\item \class{StreamHandler} instances send error messages to
|
|
|
|
streams (file-like objects).
|
|
|
|
|
|
|
|
\item \class{FileHandler} instances send error messages to disk
|
|
|
|
files.
|
|
|
|
|
2005-08-18 18:45:31 -03:00
|
|
|
\item \class{BaseRotatingHandler} is the base class for handlers that
|
2004-11-07 12:11:35 -04:00
|
|
|
rotate log files at a certain point. It is not meant to be instantiated
|
2004-11-11 09:54:48 -04:00
|
|
|
directly. Instead, use \class{RotatingFileHandler} or
|
|
|
|
\class{TimedRotatingFileHandler}.
|
2004-11-07 12:11:35 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\item \class{RotatingFileHandler} instances send error messages to disk
|
|
|
|
files, with support for maximum log file sizes and log file rotation.
|
|
|
|
|
2004-11-07 10:14:27 -04:00
|
|
|
\item \class{TimedRotatingFileHandler} instances send error messages to
|
|
|
|
disk files rotating the log file at certain timed intervals.
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\item \class{SocketHandler} instances send error messages to
|
|
|
|
TCP/IP sockets.
|
|
|
|
|
|
|
|
\item \class{DatagramHandler} instances send error messages to UDP
|
|
|
|
sockets.
|
|
|
|
|
|
|
|
\item \class{SMTPHandler} instances send error messages to a
|
|
|
|
designated email address.
|
|
|
|
|
|
|
|
\item \class{SysLogHandler} instances send error messages to a
|
2003-01-28 18:02:35 -04:00
|
|
|
\UNIX{} syslog daemon, possibly on a remote machine.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
\item \class{NTEventLogHandler} instances send error messages to a
|
|
|
|
Windows NT/2000/XP event log.
|
|
|
|
|
|
|
|
\item \class{MemoryHandler} instances send error messages to a
|
|
|
|
buffer in memory, which is flushed whenever specific criteria are
|
|
|
|
met.
|
|
|
|
|
|
|
|
\item \class{HTTPHandler} instances send error messages to an
|
2003-01-28 18:02:35 -04:00
|
|
|
HTTP server using either \samp{GET} or \samp{POST} semantics.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
\end{enumerate}
|
|
|
|
|
|
|
|
The \class{StreamHandler} and \class{FileHandler} classes are defined
|
|
|
|
in the core logging package. The other handlers are defined in a sub-
|
|
|
|
module, \module{logging.handlers}. (There is also another sub-module,
|
|
|
|
\module{logging.config}, for configuration functionality.)
|
|
|
|
|
|
|
|
Logged messages are formatted for presentation through instances of the
|
|
|
|
\class{Formatter} class. They are initialized with a format string
|
|
|
|
suitable for use with the \% operator and a dictionary.
|
|
|
|
|
|
|
|
For formatting multiple messages in a batch, instances of
|
|
|
|
\class{BufferingFormatter} can be used. In addition to the format string
|
|
|
|
(which is applied to each message in the batch), there is provision for
|
|
|
|
header and trailer format strings.
|
|
|
|
|
|
|
|
When filtering based on logger level and/or handler level is not enough,
|
|
|
|
instances of \class{Filter} can be added to both \class{Logger} and
|
2003-01-28 18:09:16 -04:00
|
|
|
\class{Handler} instances (through their \method{addFilter()} method).
|
2003-01-25 17:29:41 -04:00
|
|
|
Before deciding to process a message further, both loggers and handlers
|
|
|
|
consult all their filters for permission. If any filter returns a false
|
|
|
|
value, the message is not processed further.
|
|
|
|
|
|
|
|
The basic \class{Filter} functionality allows filtering by specific logger
|
|
|
|
name. If this feature is used, messages sent to the named logger and its
|
|
|
|
children are allowed through the filter, and all others dropped.
|
|
|
|
|
|
|
|
In addition to the classes described above, there are a number of module-
|
|
|
|
level functions.
|
|
|
|
|
|
|
|
\begin{funcdesc}{getLogger}{\optional{name}}
|
|
|
|
Return a logger with the specified name or, if no name is specified, return
|
2004-08-31 07:21:51 -03:00
|
|
|
a logger which is the root logger of the hierarchy. If specified, the name
|
|
|
|
is typically a dot-separated hierarchical name like \var{"a"}, \var{"a.b"}
|
|
|
|
or \var{"a.b.c.d"}. Choice of these names is entirely up to the developer
|
|
|
|
who is using logging.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
All calls to this function with a given name return the same logger instance.
|
|
|
|
This means that logger instances never need to be passed between different
|
|
|
|
parts of an application.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2004-09-22 09:55:16 -03:00
|
|
|
\begin{funcdesc}{getLoggerClass}{}
|
|
|
|
Return either the standard \class{Logger} class, or the last class passed to
|
|
|
|
\function{setLoggerClass()}. This function may be called from within a new
|
|
|
|
class definition, to ensure that installing a customised \class{Logger} class
|
|
|
|
will not undo customisations already applied by other code. For example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
class MyLogger(logging.getLoggerClass()):
|
|
|
|
# ... override behaviour here
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{DEBUG} on the root logger.
|
|
|
|
The \var{msg} is the message format string, and the \var{args} are the
|
2006-02-09 04:54:11 -04:00
|
|
|
arguments which are merged into \var{msg} using the string formatting
|
|
|
|
operator. (Note that this means that you can use keywords in the
|
|
|
|
format string, together with a single dictionary argument.)
|
|
|
|
|
|
|
|
There are two keyword arguments in \var{kwargs} which are inspected:
|
|
|
|
\var{exc_info} which, if it does not evaluate as false, causes exception
|
|
|
|
information to be added to the logging message. If an exception tuple (in the
|
|
|
|
format returned by \function{sys.exc_info()}) is provided, it is used;
|
|
|
|
otherwise, \function{sys.exc_info()} is called to get the exception
|
|
|
|
information.
|
|
|
|
|
|
|
|
The other optional keyword argument is \var{extra} which can be used to pass
|
|
|
|
a dictionary which is used to populate the __dict__ of the LogRecord created
|
|
|
|
for the logging event with user-defined attributes. These custom attributes
|
|
|
|
can then be used as you like. For example, they could be incorporated into
|
|
|
|
logged messages. For example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"
|
|
|
|
logging.basicConfig(format=FORMAT)
|
|
|
|
dict = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
|
|
|
|
logging.warning("Protocol problem: %s", "connection reset", extra=d)
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
would print something like
|
|
|
|
\begin{verbatim}
|
|
|
|
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The keys in the dictionary passed in \var{extra} should not clash with the keys
|
|
|
|
used by the logging system. (See the \class{Formatter} documentation for more
|
|
|
|
information on which keys are used by the logging system.)
|
|
|
|
|
|
|
|
If you choose to use these attributes in logged messages, you need to exercise
|
|
|
|
some care. In the above example, for instance, the \class{Formatter} has been
|
|
|
|
set up with a format string which expects 'clientip' and 'user' in the
|
|
|
|
attribute dictionary of the LogRecord. If these are missing, the message will
|
|
|
|
not be logged because a string formatting exception will occur. So in this
|
|
|
|
case, you always need to pass the \var{extra} dictionary with these keys.
|
|
|
|
|
|
|
|
While this might be annoying, this feature is intended for use in specialized
|
|
|
|
circumstances, such as multi-threaded servers where the same code executes
|
|
|
|
in many contexts, and interesting conditions which arise are dependent on this
|
|
|
|
context (such as remote client IP address and authenticated user name, in the
|
|
|
|
above example). In such circumstances, it is likely that specialized
|
|
|
|
\class{Formatter}s would be used with particular \class{Handler}s.
|
2006-02-15 17:47:32 -04:00
|
|
|
|
|
|
|
\versionchanged[\var{extra} was added]{2.5}
|
|
|
|
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{INFO} on the root logger.
|
|
|
|
The arguments are interpreted as for \function{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{WARNING} on the root logger.
|
|
|
|
The arguments are interpreted as for \function{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{ERROR} on the root logger.
|
|
|
|
The arguments are interpreted as for \function{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{CRITICAL} on the root logger.
|
|
|
|
The arguments are interpreted as for \function{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{exception}{msg\optional{, *args}}
|
|
|
|
Logs a message with level \constant{ERROR} on the root logger.
|
|
|
|
The arguments are interpreted as for \function{debug()}. Exception info
|
|
|
|
is added to the logging message. This function should only be called
|
|
|
|
from an exception handler.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
2004-09-24 08:46:44 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{log}{level, msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \var{level} on the root logger.
|
|
|
|
The other arguments are interpreted as for \function{debug()}.
|
|
|
|
\end{funcdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{disable}{lvl}
|
|
|
|
Provides an overriding level \var{lvl} for all loggers which takes
|
|
|
|
precedence over the logger's own level. When the need arises to
|
|
|
|
temporarily throttle logging output down across the whole application,
|
|
|
|
this function can be useful.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{addLevelName}{lvl, levelName}
|
|
|
|
Associates level \var{lvl} with text \var{levelName} in an internal
|
|
|
|
dictionary, which is used to map numeric levels to a textual
|
|
|
|
representation, for example when a \class{Formatter} formats a message.
|
|
|
|
This function can also be used to define your own levels. The only
|
|
|
|
constraints are that all levels used must be registered using this
|
|
|
|
function, levels should be positive integers and they should increase
|
|
|
|
in increasing order of severity.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{getLevelName}{lvl}
|
|
|
|
Returns the textual representation of logging level \var{lvl}. If the
|
|
|
|
level is one of the predefined levels \constant{CRITICAL},
|
|
|
|
\constant{ERROR}, \constant{WARNING}, \constant{INFO} or \constant{DEBUG}
|
|
|
|
then you get the corresponding string. If you have associated levels
|
|
|
|
with names using \function{addLevelName()} then the name you have associated
|
2004-07-03 08:45:53 -03:00
|
|
|
with \var{lvl} is returned. If a numeric value corresponding to one of the
|
|
|
|
defined levels is passed in, the corresponding string representation is
|
|
|
|
returned. Otherwise, the string "Level \%s" \% lvl is returned.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-06-27 18:43:39 -03:00
|
|
|
\begin{funcdesc}{makeLogRecord}{attrdict}
|
|
|
|
Creates and returns a new \class{LogRecord} instance whose attributes are
|
|
|
|
defined by \var{attrdict}. This function is useful for taking a pickled
|
|
|
|
\class{LogRecord} attribute dictionary, sent over a socket, and reconstituting
|
|
|
|
it as a \class{LogRecord} instance at the receiving end.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2005-07-29 08:52:19 -03:00
|
|
|
\begin{funcdesc}{basicConfig}{\optional{**kwargs}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Does basic configuration for the logging system by creating a
|
|
|
|
\class{StreamHandler} with a default \class{Formatter} and adding it to
|
|
|
|
the root logger. The functions \function{debug()}, \function{info()},
|
|
|
|
\function{warning()}, \function{error()} and \function{critical()} will call
|
|
|
|
\function{basicConfig()} automatically if no handlers are defined for the
|
|
|
|
root logger.
|
2005-07-29 08:52:19 -03:00
|
|
|
|
|
|
|
\versionchanged[Formerly, \function{basicConfig} did not take any keyword
|
|
|
|
arguments]{2.4}
|
|
|
|
|
|
|
|
The following keyword arguments are supported.
|
|
|
|
|
|
|
|
\begin{tableii}{l|l}{code}{Format}{Description}
|
|
|
|
\lineii{filename}{Specifies that a FileHandler be created, using the
|
|
|
|
specified filename, rather than a StreamHandler.}
|
|
|
|
\lineii{filemode}{Specifies the mode to open the file, if filename is
|
|
|
|
specified (if filemode is unspecified, it defaults to 'a').}
|
|
|
|
\lineii{format}{Use the specified format string for the handler.}
|
|
|
|
\lineii{datefmt}{Use the specified date/time format.}
|
|
|
|
\lineii{level}{Set the root logger level to the specified level.}
|
|
|
|
\lineii{stream}{Use the specified stream to initialize the StreamHandler.
|
|
|
|
Note that this argument is incompatible with 'filename' - if both
|
|
|
|
are present, 'stream' is ignored.}
|
|
|
|
\end{tableii}
|
|
|
|
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{shutdown}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
Informs the logging system to perform an orderly shutdown by flushing and
|
|
|
|
closing all handlers.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{setLoggerClass}{klass}
|
|
|
|
Tells the logging system to use the class \var{klass} when instantiating a
|
|
|
|
logger. The class should define \method{__init__()} such that only a name
|
|
|
|
argument is required, and the \method{__init__()} should call
|
|
|
|
\method{Logger.__init__()}. This function is typically called before any
|
|
|
|
loggers are instantiated by applications which need to use custom logger
|
|
|
|
behavior.
|
|
|
|
\end{funcdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-28 18:02:35 -04:00
|
|
|
|
|
|
|
\begin{seealso}
|
|
|
|
\seepep{282}{A Logging System}
|
|
|
|
{The proposal which described this feature for inclusion in
|
|
|
|
the Python standard library.}
|
2004-01-08 10:59:02 -04:00
|
|
|
\seelink{http://www.red-dove.com/python_logging.html}
|
|
|
|
{Original Python \module{logging} package}
|
|
|
|
{This is the original source for the \module{logging}
|
|
|
|
package. The version of the package available from this
|
2004-07-03 08:45:53 -03:00
|
|
|
site is suitable for use with Python 1.5.2, 2.1.x and 2.2.x,
|
|
|
|
which do not include the \module{logging} package in the standard
|
2004-01-08 10:59:02 -04:00
|
|
|
library.}
|
2003-01-28 18:02:35 -04:00
|
|
|
\end{seealso}
|
|
|
|
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsection{Logger Objects}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
Loggers have the following attributes and methods. Note that Loggers are
|
|
|
|
never instantiated directly, but always through the module-level function
|
|
|
|
\function{logging.getLogger(name)}.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{datadesc}{propagate}
|
|
|
|
If this evaluates to false, logging messages are not passed by this
|
|
|
|
logger or by child loggers to higher level (ancestor) loggers. The
|
|
|
|
constructor sets this attribute to 1.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{datadesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{setLevel}{lvl}
|
|
|
|
Sets the threshold for this logger to \var{lvl}. Logging messages
|
|
|
|
which are less severe than \var{lvl} will be ignored. When a logger is
|
2003-02-18 10:20:07 -04:00
|
|
|
created, the level is set to \constant{NOTSET} (which causes all messages
|
2004-12-02 17:27:42 -04:00
|
|
|
to be processed when the logger is the root logger, or delegation to the
|
|
|
|
parent when the logger is a non-root logger). Note that the root logger
|
|
|
|
is created with level \constant{WARNING}.
|
2005-09-25 21:14:46 -03:00
|
|
|
|
|
|
|
The term "delegation to the parent" means that if a logger has a level
|
|
|
|
of NOTSET, its chain of ancestor loggers is traversed until either an
|
|
|
|
ancestor with a level other than NOTSET is found, or the root is
|
|
|
|
reached.
|
|
|
|
|
|
|
|
If an ancestor is found with a level other than NOTSET, then that
|
|
|
|
ancestor's level is treated as the effective level of the logger where
|
|
|
|
the ancestor search began, and is used to determine how a logging
|
|
|
|
event is handled.
|
|
|
|
|
|
|
|
If the root is reached, and it has a level of NOTSET, then all
|
|
|
|
messages will be processed. Otherwise, the root's level will be used
|
|
|
|
as the effective level.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{isEnabledFor}{lvl}
|
|
|
|
Indicates if a message of severity \var{lvl} would be processed by
|
|
|
|
this logger. This method checks first the module-level level set by
|
|
|
|
\function{logging.disable(lvl)} and then the logger's effective level as
|
|
|
|
determined by \method{getEffectiveLevel()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{getEffectiveLevel}{}
|
|
|
|
Indicates the effective level for this logger. If a value other than
|
2003-02-18 10:20:07 -04:00
|
|
|
\constant{NOTSET} has been set using \method{setLevel()}, it is returned.
|
2003-01-25 17:29:41 -04:00
|
|
|
Otherwise, the hierarchy is traversed towards the root until a value
|
2003-06-27 18:43:39 -03:00
|
|
|
other than \constant{NOTSET} is found, and that value is returned.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{DEBUG} on this logger.
|
|
|
|
The \var{msg} is the message format string, and the \var{args} are the
|
2006-02-09 04:54:11 -04:00
|
|
|
arguments which are merged into \var{msg} using the string formatting
|
|
|
|
operator. (Note that this means that you can use keywords in the
|
|
|
|
format string, together with a single dictionary argument.)
|
|
|
|
|
|
|
|
There are two keyword arguments in \var{kwargs} which are inspected:
|
|
|
|
\var{exc_info} which, if it does not evaluate as false, causes exception
|
|
|
|
information to be added to the logging message. If an exception tuple (in the
|
|
|
|
format returned by \function{sys.exc_info()}) is provided, it is used;
|
|
|
|
otherwise, \function{sys.exc_info()} is called to get the exception
|
|
|
|
information.
|
|
|
|
|
|
|
|
The other optional keyword argument is \var{extra} which can be used to pass
|
|
|
|
a dictionary which is used to populate the __dict__ of the LogRecord created
|
|
|
|
for the logging event with user-defined attributes. These custom attributes
|
|
|
|
can then be used as you like. For example, they could be incorporated into
|
|
|
|
logged messages. For example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"
|
|
|
|
logging.basicConfig(format=FORMAT)
|
|
|
|
dict = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
|
|
|
|
logger = logging.getLogger("tcpserver")
|
|
|
|
logger.warning("Protocol problem: %s", "connection reset", extra=d)
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
would print something like
|
|
|
|
\begin{verbatim}
|
|
|
|
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The keys in the dictionary passed in \var{extra} should not clash with the keys
|
|
|
|
used by the logging system. (See the \class{Formatter} documentation for more
|
|
|
|
information on which keys are used by the logging system.)
|
|
|
|
|
|
|
|
If you choose to use these attributes in logged messages, you need to exercise
|
|
|
|
some care. In the above example, for instance, the \class{Formatter} has been
|
|
|
|
set up with a format string which expects 'clientip' and 'user' in the
|
|
|
|
attribute dictionary of the LogRecord. If these are missing, the message will
|
|
|
|
not be logged because a string formatting exception will occur. So in this
|
|
|
|
case, you always need to pass the \var{extra} dictionary with these keys.
|
|
|
|
|
|
|
|
While this might be annoying, this feature is intended for use in specialized
|
|
|
|
circumstances, such as multi-threaded servers where the same code executes
|
|
|
|
in many contexts, and interesting conditions which arise are dependent on this
|
|
|
|
context (such as remote client IP address and authenticated user name, in the
|
|
|
|
above example). In such circumstances, it is likely that specialized
|
|
|
|
\class{Formatter}s would be used with particular \class{Handler}s.
|
2006-02-15 17:47:32 -04:00
|
|
|
|
|
|
|
\versionchanged[\var{extra} was added]{2.5}
|
|
|
|
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{INFO} on this logger.
|
|
|
|
The arguments are interpreted as for \method{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{WARNING} on this logger.
|
|
|
|
The arguments are interpreted as for \method{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{ERROR} on this logger.
|
|
|
|
The arguments are interpreted as for \method{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}}
|
|
|
|
Logs a message with level \constant{CRITICAL} on this logger.
|
|
|
|
The arguments are interpreted as for \method{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}}
|
2004-08-04 05:36:44 -03:00
|
|
|
Logs a message with integer level \var{lvl} on this logger.
|
2003-01-25 17:29:41 -04:00
|
|
|
The other arguments are interpreted as for \method{debug()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{exception}{msg\optional{, *args}}
|
|
|
|
Logs a message with level \constant{ERROR} on this logger.
|
|
|
|
The arguments are interpreted as for \method{debug()}. Exception info
|
|
|
|
is added to the logging message. This method should only be called
|
|
|
|
from an exception handler.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{addFilter}{filt}
|
|
|
|
Adds the specified filter \var{filt} to this logger.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{removeFilter}{filt}
|
|
|
|
Removes the specified filter \var{filt} from this logger.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{filter}{record}
|
|
|
|
Applies this logger's filters to the record and returns a true value if
|
|
|
|
the record is to be processed.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{addHandler}{hdlr}
|
|
|
|
Adds the specified handler \var{hdlr} to this logger.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{removeHandler}{hdlr}
|
|
|
|
Removes the specified handler \var{hdlr} from this logger.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{findCaller}{}
|
|
|
|
Finds the caller's source filename and line number. Returns the filename
|
|
|
|
and line number as a 2-element tuple.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{handle}{record}
|
|
|
|
Handles a record by passing it to all handlers associated with this logger
|
|
|
|
and its ancestors (until a false value of \var{propagate} is found).
|
|
|
|
This method is used for unpickled records received from a socket, as well
|
|
|
|
as those created locally. Logger-level filtering is applied using
|
|
|
|
\method{filter()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2006-10-03 15:20:26 -03:00
|
|
|
\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info
|
|
|
|
\optional{, func, extra}}
|
2003-01-25 17:29:41 -04:00
|
|
|
This is a factory method which can be overridden in subclasses to create
|
|
|
|
specialized \class{LogRecord} instances.
|
2006-02-12 22:04:37 -04:00
|
|
|
\versionchanged[\var{func} and \var{extra} were added]{2.5}
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2004-07-03 08:45:53 -03:00
|
|
|
\subsection{Basic example \label{minimal-example}}
|
|
|
|
|
2005-07-29 08:52:19 -03:00
|
|
|
\versionchanged[formerly \function{basicConfig} did not take any keyword
|
|
|
|
arguments]{2.4}
|
|
|
|
|
2004-07-03 08:45:53 -03:00
|
|
|
The \module{logging} package provides a lot of flexibility, and its
|
|
|
|
configuration can appear daunting. This section demonstrates that simple
|
|
|
|
use of the logging package is possible.
|
|
|
|
|
|
|
|
The simplest example shows logging to the console:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.debug('A debug message')
|
|
|
|
logging.info('Some information')
|
|
|
|
logging.warning('A shot across the bows')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
If you run the above script, you'll see this:
|
|
|
|
\begin{verbatim}
|
|
|
|
WARNING:root:A shot across the bows
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
Because no particular logger was specified, the system used the root logger.
|
|
|
|
The debug and info messages didn't appear because by default, the root
|
|
|
|
logger is configured to only handle messages with a severity of WARNING
|
|
|
|
or above. The message format is also a configuration default, as is the output
|
|
|
|
destination of the messages - \code{sys.stderr}. The severity level,
|
|
|
|
the message format and destination can be easily changed, as shown in
|
|
|
|
the example below:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
2004-07-07 12:59:49 -03:00
|
|
|
format='%(asctime)s %(levelname)s %(message)s',
|
|
|
|
filename='/tmp/myapp.log',
|
|
|
|
filemode='w')
|
2004-07-03 08:45:53 -03:00
|
|
|
logging.debug('A debug message')
|
|
|
|
logging.info('Some information')
|
|
|
|
logging.warning('A shot across the bows')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The \method{basicConfig()} method is used to change the configuration
|
|
|
|
defaults, which results in output (written to \code{/tmp/myapp.log})
|
|
|
|
which should look something like the following:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
2004-07-02 13:00:08,743 DEBUG A debug message
|
|
|
|
2004-07-02 13:00:08,743 INFO Some information
|
|
|
|
2004-07-02 13:00:08,743 WARNING A shot across the bows
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
This time, all messages with a severity of DEBUG or above were handled,
|
|
|
|
and the format of the messages was also changed, and output went to the
|
|
|
|
specified file rather than the console.
|
|
|
|
|
|
|
|
Formatting uses standard Python string formatting - see section
|
|
|
|
\ref{typesseq-strings}. The format string takes the following
|
|
|
|
common specifiers. For a complete list of specifiers, consult the
|
|
|
|
\class{Formatter} documentation.
|
|
|
|
|
|
|
|
\begin{tableii}{l|l}{code}{Format}{Description}
|
|
|
|
\lineii{\%(name)s} {Name of the logger (logging channel).}
|
|
|
|
\lineii{\%(levelname)s}{Text logging level for the message
|
|
|
|
(\code{'DEBUG'}, \code{'INFO'},
|
|
|
|
\code{'WARNING'}, \code{'ERROR'},
|
|
|
|
\code{'CRITICAL'}).}
|
|
|
|
\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
|
|
|
|
was created. By default this is of the form
|
|
|
|
``2003-07-08 16:49:45,896'' (the numbers after the
|
|
|
|
comma are millisecond portion of the time).}
|
|
|
|
\lineii{\%(message)s} {The logged message.}
|
|
|
|
\end{tableii}
|
|
|
|
|
|
|
|
To change the date/time format, you can pass an additional keyword parameter,
|
|
|
|
\var{datefmt}, as in the following:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
2004-07-07 12:59:49 -03:00
|
|
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
|
|
|
datefmt='%a, %d %b %Y %H:%M:%S',
|
|
|
|
filename='/temp/myapp.log',
|
|
|
|
filemode='w')
|
2004-07-03 08:45:53 -03:00
|
|
|
logging.debug('A debug message')
|
|
|
|
logging.info('Some information')
|
|
|
|
logging.warning('A shot across the bows')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
which would result in output like
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
Fri, 02 Jul 2004 13:06:18 DEBUG A debug message
|
|
|
|
Fri, 02 Jul 2004 13:06:18 INFO Some information
|
|
|
|
Fri, 02 Jul 2004 13:06:18 WARNING A shot across the bows
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The date format string follows the requirements of \function{strftime()} -
|
|
|
|
see the documentation for the \refmodule{time} module.
|
|
|
|
|
|
|
|
If, instead of sending logging output to the console or a file, you'd rather
|
|
|
|
use a file-like object which you have created separately, you can pass it
|
|
|
|
to \function{basicConfig()} using the \var{stream} keyword argument. Note
|
|
|
|
that if both \var{stream} and \var{filename} keyword arguments are passed,
|
|
|
|
the \var{stream} argument is ignored.
|
|
|
|
|
2004-07-21 11:40:11 -03:00
|
|
|
Of course, you can put variable information in your output. To do this,
|
|
|
|
simply have the message be a format string and pass in additional arguments
|
|
|
|
containing the variable information, as in the following example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
|
|
|
datefmt='%a, %d %b %Y %H:%M:%S',
|
|
|
|
filename='/temp/myapp.log',
|
|
|
|
filemode='w')
|
2004-10-22 18:43:15 -03:00
|
|
|
logging.error('Pack my box with %d dozen %s', 5, 'liquor jugs')
|
2004-07-21 11:40:11 -03:00
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
which would result in
|
|
|
|
|
|
|
|
\begin{verbatim}
|
2004-10-22 18:43:15 -03:00
|
|
|
Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs
|
2004-07-21 11:40:11 -03:00
|
|
|
\end{verbatim}
|
|
|
|
|
2004-10-22 18:43:15 -03:00
|
|
|
\subsection{Logging to multiple destinations \label{multiple-destinations}}
|
|
|
|
|
|
|
|
Let's say you want to log to console and file with different message formats
|
|
|
|
and in differing circumstances. Say you want to log messages with levels
|
|
|
|
of DEBUG and higher to file, and those messages at level INFO and higher to
|
|
|
|
the console. Let's also assume that the file should contain timestamps, but
|
|
|
|
the console messages should not. Here's how you can achieve this:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
# set up logging to file - see previous section for more details
|
2004-10-22 18:43:15 -03:00
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
|
|
|
|
datefmt='%m-%d %H:%M',
|
|
|
|
filename='/temp/myapp.log',
|
|
|
|
filemode='w')
|
2004-10-29 11:35:42 -03:00
|
|
|
# define a Handler which writes INFO messages or higher to the sys.stderr
|
2004-10-22 18:43:15 -03:00
|
|
|
console = logging.StreamHandler()
|
|
|
|
console.setLevel(logging.INFO)
|
2004-10-29 11:35:42 -03:00
|
|
|
# set a format which is simpler for console use
|
2004-10-22 18:43:15 -03:00
|
|
|
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
|
2004-10-29 11:35:42 -03:00
|
|
|
# tell the handler to use this format
|
2004-10-22 18:43:15 -03:00
|
|
|
console.setFormatter(formatter)
|
2004-10-29 11:35:42 -03:00
|
|
|
# add the handler to the root logger
|
2004-10-22 18:43:15 -03:00
|
|
|
logging.getLogger('').addHandler(console)
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
# Now, we can log to the root logger, or any other logger. First the root...
|
2004-10-22 18:43:15 -03:00
|
|
|
logging.info('Jackdaws love my big sphinx of quartz.')
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
# Now, define a couple of other loggers which might represent areas in your
|
|
|
|
# application:
|
2004-10-22 18:43:15 -03:00
|
|
|
|
|
|
|
logger1 = logging.getLogger('myapp.area1')
|
|
|
|
logger2 = logging.getLogger('myapp.area2')
|
|
|
|
|
|
|
|
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
|
|
|
|
logger1.info('How quickly daft jumping zebras vex.')
|
|
|
|
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
|
|
|
|
logger2.error('The five boxing wizards jump quickly.')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
When you run this, on the console you will see
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
root : INFO Jackdaws love my big sphinx of quartz.
|
|
|
|
myapp.area1 : INFO How quickly daft jumping zebras vex.
|
|
|
|
myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.
|
|
|
|
myapp.area2 : ERROR The five boxing wizards jump quickly.
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
and in the file you will see something like
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
|
|
|
|
10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
|
|
|
|
10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
|
|
|
|
10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
|
|
|
|
10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
As you can see, the DEBUG message only shows up in the file. The other
|
|
|
|
messages are sent to both destinations.
|
|
|
|
|
2004-10-29 09:30:28 -03:00
|
|
|
This example uses console and file handlers, but you can use any number and
|
|
|
|
combination of handlers you choose.
|
|
|
|
|
|
|
|
\subsection{Sending and receiving logging events across a network
|
|
|
|
\label{network-logging}}
|
|
|
|
|
|
|
|
Let's say you want to send logging events across a network, and handle them
|
|
|
|
at the receiving end. A simple way of doing this is attaching a
|
|
|
|
\class{SocketHandler} instance to the root logger at the sending end:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import logging, logging.handlers
|
|
|
|
|
|
|
|
rootLogger = logging.getLogger('')
|
|
|
|
rootLogger.setLevel(logging.DEBUG)
|
|
|
|
socketHandler = logging.handlers.SocketHandler('localhost',
|
|
|
|
logging.handlers.DEFAULT_TCP_LOGGING_PORT)
|
|
|
|
# don't bother with a formatter, since a socket handler sends the event as
|
|
|
|
# an unformatted pickle
|
|
|
|
rootLogger.addHandler(socketHandler)
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
# Now, we can log to the root logger, or any other logger. First the root...
|
2004-10-29 09:30:28 -03:00
|
|
|
logging.info('Jackdaws love my big sphinx of quartz.')
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
# Now, define a couple of other loggers which might represent areas in your
|
|
|
|
# application:
|
2004-10-29 09:30:28 -03:00
|
|
|
|
|
|
|
logger1 = logging.getLogger('myapp.area1')
|
|
|
|
logger2 = logging.getLogger('myapp.area2')
|
|
|
|
|
|
|
|
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
|
|
|
|
logger1.info('How quickly daft jumping zebras vex.')
|
|
|
|
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
|
|
|
|
logger2.error('The five boxing wizards jump quickly.')
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
At the receiving end, you can set up a receiver using the
|
|
|
|
\module{SocketServer} module. Here is a basic working example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
2004-10-29 11:35:42 -03:00
|
|
|
import cPickle
|
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
import SocketServer
|
|
|
|
import struct
|
2004-10-29 09:30:28 -03:00
|
|
|
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
class LogRecordStreamHandler(SocketServer.StreamRequestHandler):
|
|
|
|
"""Handler for a streaming logging request.
|
|
|
|
|
|
|
|
This basically logs the record using whatever logging policy is
|
|
|
|
configured locally.
|
2004-10-29 09:30:28 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
"""
|
|
|
|
Handle multiple requests - each expected to be a 4-byte length,
|
|
|
|
followed by the LogRecord in pickle format. Logs the record
|
|
|
|
according to whatever policy is configured locally.
|
|
|
|
"""
|
|
|
|
while 1:
|
|
|
|
chunk = self.connection.recv(4)
|
|
|
|
if len(chunk) < 4:
|
|
|
|
break
|
|
|
|
slen = struct.unpack(">L", chunk)[0]
|
|
|
|
chunk = self.connection.recv(slen)
|
|
|
|
while len(chunk) < slen:
|
|
|
|
chunk = chunk + self.connection.recv(slen - len(chunk))
|
|
|
|
obj = self.unPickle(chunk)
|
|
|
|
record = logging.makeLogRecord(obj)
|
|
|
|
self.handleLogRecord(record)
|
|
|
|
|
|
|
|
def unPickle(self, data):
|
|
|
|
return cPickle.loads(data)
|
|
|
|
|
|
|
|
def handleLogRecord(self, record):
|
2004-10-29 11:35:42 -03:00
|
|
|
# if a name is specified, we use the named logger rather than the one
|
|
|
|
# implied by the record.
|
2004-10-29 09:30:28 -03:00
|
|
|
if self.server.logname is not None:
|
|
|
|
name = self.server.logname
|
|
|
|
else:
|
|
|
|
name = record.name
|
|
|
|
logger = logging.getLogger(name)
|
2004-10-29 11:35:42 -03:00
|
|
|
# N.B. EVERY record gets logged. This is because Logger.handle
|
|
|
|
# is normally called AFTER logger-level filtering. If you want
|
|
|
|
# to do filtering, do it at the client end to save wasting
|
|
|
|
# cycles and network bandwidth!
|
2004-10-29 09:30:28 -03:00
|
|
|
logger.handle(record)
|
|
|
|
|
2004-10-29 11:35:42 -03:00
|
|
|
class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):
|
|
|
|
"""simple TCP socket-based logging receiver suitable for testing.
|
2004-10-29 09:30:28 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
allow_reuse_address = 1
|
|
|
|
|
|
|
|
def __init__(self, host='localhost',
|
2004-10-29 11:35:42 -03:00
|
|
|
port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
|
|
|
|
handler=LogRecordStreamHandler):
|
|
|
|
SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
|
2004-10-29 09:30:28 -03:00
|
|
|
self.abort = 0
|
|
|
|
self.timeout = 1
|
|
|
|
self.logname = None
|
|
|
|
|
|
|
|
def serve_until_stopped(self):
|
|
|
|
import select
|
|
|
|
abort = 0
|
|
|
|
while not abort:
|
|
|
|
rd, wr, ex = select.select([self.socket.fileno()],
|
|
|
|
[], [],
|
|
|
|
self.timeout)
|
|
|
|
if rd:
|
|
|
|
self.handle_request()
|
|
|
|
abort = self.abort
|
|
|
|
|
|
|
|
def main():
|
|
|
|
logging.basicConfig(
|
2004-11-11 09:54:48 -04:00
|
|
|
format="%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s")
|
2004-10-29 09:30:28 -03:00
|
|
|
tcpserver = LogRecordSocketReceiver()
|
|
|
|
print "About to start TCP server..."
|
|
|
|
tcpserver.serve_until_stopped()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
\end{verbatim}
|
|
|
|
|
2004-11-11 09:54:48 -04:00
|
|
|
First run the server, and then the client. On the client side, nothing is
|
|
|
|
printed on the console; on the server side, you should see something like:
|
2004-10-29 09:30:28 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
About to start TCP server...
|
|
|
|
59 root INFO Jackdaws love my big sphinx of quartz.
|
|
|
|
59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
|
|
|
|
69 myapp.area1 INFO How quickly daft jumping zebras vex.
|
|
|
|
69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
|
|
|
|
69 myapp.area2 ERROR The five boxing wizards jump quickly.
|
|
|
|
\end{verbatim}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsection{Handler Objects}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-28 18:02:35 -04:00
|
|
|
Handlers have the following attributes and methods. Note that
|
|
|
|
\class{Handler} is never instantiated directly; this class acts as a
|
|
|
|
base for more useful subclasses. However, the \method{__init__()}
|
|
|
|
method in subclasses needs to call \method{Handler.__init__()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-02-18 10:20:07 -04:00
|
|
|
\begin{methoddesc}{__init__}{level=\constant{NOTSET}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Initializes the \class{Handler} instance by setting its level, setting
|
|
|
|
the list of filters to the empty list and creating a lock (using
|
2003-09-01 19:50:52 -03:00
|
|
|
\method{createLock()}) for serializing access to an I/O mechanism.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{createLock}{}
|
|
|
|
Initializes a thread lock which can be used to serialize access to
|
|
|
|
underlying I/O functionality which may not be threadsafe.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{acquire}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
Acquires the thread lock created with \method{createLock()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{release}{}
|
|
|
|
Releases the thread lock acquired with \method{acquire()}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{setLevel}{lvl}
|
|
|
|
Sets the threshold for this handler to \var{lvl}. Logging messages which are
|
|
|
|
less severe than \var{lvl} will be ignored. When a handler is created, the
|
2003-02-18 10:20:07 -04:00
|
|
|
level is set to \constant{NOTSET} (which causes all messages to be processed).
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{methoddesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{setFormatter}{form}
|
|
|
|
Sets the \class{Formatter} for this handler to \var{form}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{addFilter}{filt}
|
|
|
|
Adds the specified filter \var{filt} to this handler.
|
|
|
|
\end{methoddesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{removeFilter}{filt}
|
|
|
|
Removes the specified filter \var{filt} from this handler.
|
|
|
|
\end{methoddesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{filter}{record}
|
|
|
|
Applies this handler's filters to the record and returns a true value if
|
|
|
|
the record is to be processed.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{flush}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
Ensure all logging output has been flushed. This version does
|
|
|
|
nothing and is intended to be implemented by subclasses.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{close}{}
|
|
|
|
Tidy up any resources used by the handler. This version does
|
|
|
|
nothing and is intended to be implemented by subclasses.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{handle}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Conditionally emits the specified logging record, depending on
|
|
|
|
filters which may have been added to the handler. Wraps the actual
|
|
|
|
emission of the record with acquisition/release of the I/O thread
|
|
|
|
lock.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2004-07-03 08:45:53 -03:00
|
|
|
\begin{methoddesc}{handleError}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
This method should be called from handlers when an exception is
|
2004-07-03 08:45:53 -03:00
|
|
|
encountered during an \method{emit()} call. By default it does nothing,
|
2003-01-25 17:29:41 -04:00
|
|
|
which means that exceptions get silently ignored. This is what is
|
|
|
|
mostly wanted for a logging system - most users will not care
|
|
|
|
about errors in the logging system, they are more interested in
|
|
|
|
application errors. You could, however, replace this with a custom
|
2004-07-03 08:45:53 -03:00
|
|
|
handler if you wish. The specified record is the one which was being
|
|
|
|
processed when the exception occurred.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{format}{record}
|
|
|
|
Do formatting for a record - if a formatter is set, use it.
|
|
|
|
Otherwise, use the default formatter for the module.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
Do whatever it takes to actually log the specified logging record.
|
|
|
|
This version is intended to be implemented by subclasses and so
|
|
|
|
raises a \exception{NotImplementedError}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{StreamHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{StreamHandler} class, located in the core \module{logging}
|
|
|
|
package, sends logging output to streams such as \var{sys.stdout},
|
|
|
|
\var{sys.stderr} or any file-like object (or, more precisely, any
|
|
|
|
object which supports \method{write()} and \method{flush()} methods).
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{StreamHandler}{\optional{strm}}
|
|
|
|
Returns a new instance of the \class{StreamHandler} class. If \var{strm} is
|
|
|
|
specified, the instance will use it for logging output; otherwise,
|
|
|
|
\var{sys.stderr} will be used.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
If a formatter is specified, it is used to format the record.
|
|
|
|
The record is then written to the stream with a trailing newline.
|
|
|
|
If exception information is present, it is formatted using
|
|
|
|
\function{traceback.print_exception()} and appended to the stream.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{flush}{}
|
|
|
|
Flushes the stream by calling its \method{flush()} method. Note that
|
|
|
|
the \method{close()} method is inherited from \class{Handler} and
|
|
|
|
so does nothing, so an explicit \method{flush()} call may be needed
|
|
|
|
at times.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{FileHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{FileHandler} class, located in the core \module{logging}
|
|
|
|
package, sends logging output to a disk file. It inherits the output
|
|
|
|
functionality from \class{StreamHandler}.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{FileHandler}{filename\optional{, mode}}
|
|
|
|
Returns a new instance of the \class{FileHandler} class. The specified
|
|
|
|
file is opened and used as the stream for logging. If \var{mode} is
|
2003-07-08 12:38:40 -03:00
|
|
|
not specified, \constant{'a'} is used. By default, the file grows
|
2003-01-25 17:29:41 -04:00
|
|
|
indefinitely.
|
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{close}{}
|
|
|
|
Closes the file.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
Outputs the record to the file.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\subsubsection{RotatingFileHandler}
|
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{RotatingFileHandler} class, located in the \module{logging.handlers}
|
|
|
|
module, supports rotation of disk log files.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
2003-07-08 12:38:40 -03:00
|
|
|
\begin{classdesc}{RotatingFileHandler}{filename\optional{, mode\optional{,
|
|
|
|
maxBytes\optional{, backupCount}}}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns a new instance of the \class{RotatingFileHandler} class. The
|
|
|
|
specified file is opened and used as the stream for logging. If
|
2003-01-28 18:02:35 -04:00
|
|
|
\var{mode} is not specified, \code{'a'} is used. By default, the
|
2004-07-03 08:45:53 -03:00
|
|
|
file grows indefinitely.
|
2003-09-26 10:45:18 -03:00
|
|
|
|
|
|
|
You can use the \var{maxBytes} and
|
2003-01-25 17:29:41 -04:00
|
|
|
\var{backupCount} values to allow the file to \dfn{rollover} at a
|
|
|
|
predetermined size. When the size is about to be exceeded, the file is
|
2003-09-26 10:45:18 -03:00
|
|
|
closed and a new file is silently opened for output. Rollover occurs
|
|
|
|
whenever the current log file is nearly \var{maxBytes} in length; if
|
|
|
|
\var{maxBytes} is zero, rollover never occurs. If \var{backupCount}
|
|
|
|
is non-zero, the system will save old log files by appending the
|
|
|
|
extensions ".1", ".2" etc., to the filename. For example, with
|
|
|
|
a \var{backupCount} of 5 and a base file name of
|
|
|
|
\file{app.log}, you would get \file{app.log},
|
|
|
|
\file{app.log.1}, \file{app.log.2}, up to \file{app.log.5}. The file being
|
|
|
|
written to is always \file{app.log}. When this file is filled, it is
|
|
|
|
closed and renamed to \file{app.log.1}, and if files \file{app.log.1},
|
|
|
|
\file{app.log.2}, etc. exist, then they are renamed to \file{app.log.2},
|
2004-07-03 08:45:53 -03:00
|
|
|
\file{app.log.3} etc. respectively.
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{doRollover}{}
|
|
|
|
Does a rollover, as described above.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
2004-11-07 12:11:35 -04:00
|
|
|
Outputs the record to the file, catering for rollover as described previously.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2004-11-07 10:14:27 -04:00
|
|
|
\subsubsection{TimedRotatingFileHandler}
|
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{TimedRotatingFileHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports rotation of disk log files
|
2004-11-07 10:14:27 -04:00
|
|
|
at certain timed intervals.
|
|
|
|
|
|
|
|
\begin{classdesc}{TimedRotatingFileHandler}{filename
|
|
|
|
\optional{,when
|
|
|
|
\optional{,interval
|
|
|
|
\optional{,backupCount}}}}
|
|
|
|
|
|
|
|
Returns a new instance of the \class{TimedRotatingFileHandler} class. The
|
|
|
|
specified file is opened and used as the stream for logging. On rotating
|
|
|
|
it also sets the filename suffix. Rotating happens based on the product
|
2004-11-11 09:54:48 -04:00
|
|
|
of \var{when} and \var{interval}.
|
2004-11-07 10:14:27 -04:00
|
|
|
|
|
|
|
You can use the \var{when} to specify the type of \var{interval}. The
|
|
|
|
list of possible values is, note that they are not case sensitive:
|
|
|
|
|
|
|
|
\begin{tableii}{l|l}{}{Value}{Type of interval}
|
|
|
|
\lineii{S}{Seconds}
|
|
|
|
\lineii{M}{Minutes}
|
|
|
|
\lineii{H}{Hours}
|
|
|
|
\lineii{D}{Days}
|
|
|
|
\lineii{W}{Week day (0=Monday)}
|
|
|
|
\lineii{midnight}{Roll over at midnight}
|
|
|
|
\end{tableii}
|
|
|
|
|
|
|
|
If \var{backupCount} is non-zero, the system will save old log files by
|
2006-07-20 13:28:39 -03:00
|
|
|
appending extensions to the filename. The extensions are date-and-time
|
2006-07-20 17:11:57 -03:00
|
|
|
based, using the strftime format \code{\%Y-\%m-\%d_\%H-\%M-\%S} or a leading
|
2006-07-20 13:28:39 -03:00
|
|
|
portion thereof, depending on the rollover interval. At most \var{backupCount}
|
|
|
|
files will be kept, and if more would be created when rollover occurs, the
|
|
|
|
oldest one is deleted.
|
2004-11-07 10:14:27 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{doRollover}{}
|
|
|
|
Does a rollover, as described above.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
Outputs the record to the file, catering for rollover as described
|
|
|
|
above.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{SocketHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{SocketHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, sends logging output to a network
|
2003-01-25 17:29:41 -04:00
|
|
|
socket. The base class uses a TCP socket.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{SocketHandler}{host, port}
|
|
|
|
Returns a new instance of the \class{SocketHandler} class intended to
|
|
|
|
communicate with a remote machine whose address is given by \var{host}
|
|
|
|
and \var{port}.
|
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{close}{}
|
|
|
|
Closes the socket.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{handleError}{}
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{}
|
2003-06-27 18:43:39 -03:00
|
|
|
Pickles the record's attribute dictionary and writes it to the socket in
|
|
|
|
binary format. If there is an error with the socket, silently drops the
|
|
|
|
packet. If the connection was previously lost, re-establishes the connection.
|
2004-04-09 15:26:40 -03:00
|
|
|
To unpickle the record at the receiving end into a \class{LogRecord}, use the
|
|
|
|
\function{makeLogRecord()} function.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{handleError}{}
|
|
|
|
Handles an error which has occurred during \method{emit()}. The
|
|
|
|
most likely cause is a lost connection. Closes the socket so that
|
|
|
|
we can retry on the next event.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{makeSocket}{}
|
|
|
|
This is a factory method which allows subclasses to define the precise
|
|
|
|
type of socket they want. The default implementation creates a TCP
|
|
|
|
socket (\constant{socket.SOCK_STREAM}).
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{makePickle}{record}
|
2003-06-27 18:43:39 -03:00
|
|
|
Pickles the record's attribute dictionary in binary format with a length
|
|
|
|
prefix, and returns it ready for transmission across the socket.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{send}{packet}
|
2003-01-25 17:46:53 -04:00
|
|
|
Send a pickled string \var{packet} to the socket. This function allows
|
2003-01-25 17:29:41 -04:00
|
|
|
for partial sends which can happen when the network is busy.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{DatagramHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{DatagramHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, inherits from \class{SocketHandler}
|
2003-01-25 17:29:41 -04:00
|
|
|
to support sending logging messages over UDP sockets.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{DatagramHandler}{host, port}
|
|
|
|
Returns a new instance of the \class{DatagramHandler} class intended to
|
|
|
|
communicate with a remote machine whose address is given by \var{host}
|
|
|
|
and \var{port}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{}
|
2003-06-27 18:43:39 -03:00
|
|
|
Pickles the record's attribute dictionary and writes it to the socket in
|
|
|
|
binary format. If there is an error with the socket, silently drops the
|
|
|
|
packet.
|
2004-04-09 15:26:40 -03:00
|
|
|
To unpickle the record at the receiving end into a \class{LogRecord}, use the
|
|
|
|
\function{makeLogRecord()} function.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{makeSocket}{}
|
|
|
|
The factory method of \class{SocketHandler} is here overridden to create
|
|
|
|
a UDP socket (\constant{socket.SOCK_DGRAM}).
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{send}{s}
|
2003-06-27 18:43:39 -03:00
|
|
|
Send a pickled string to a socket.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{SysLogHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{SysLogHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports sending logging messages to
|
|
|
|
a remote or local \UNIX{} syslog.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}}
|
|
|
|
Returns a new instance of the \class{SysLogHandler} class intended to
|
2003-01-28 18:02:35 -04:00
|
|
|
communicate with a remote \UNIX{} machine whose address is given by
|
|
|
|
\var{address} in the form of a \code{(\var{host}, \var{port})}
|
|
|
|
tuple. If \var{address} is not specified, \code{('localhost', 514)} is
|
|
|
|
used. The address is used to open a UDP socket. If \var{facility} is
|
|
|
|
not specified, \constant{LOG_USER} is used.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{close}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
Closes the socket to the remote host.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
The record is formatted, and then sent to the syslog server. If
|
|
|
|
exception information is present, it is \emph{not} sent to the server.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{encodePriority}{facility, priority}
|
|
|
|
Encodes the facility and priority into an integer. You can pass in strings
|
|
|
|
or integers - if strings are passed, internal mapping dictionaries are used
|
|
|
|
to convert them to integers.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\subsubsection{NTEventLogHandler}
|
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{NTEventLogHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports sending logging messages to
|
|
|
|
a local Windows NT, Windows 2000 or Windows XP event log. Before you
|
|
|
|
can use it, you need Mark Hammond's Win32 extensions for Python
|
2003-01-25 17:29:41 -04:00
|
|
|
installed.
|
|
|
|
|
2003-07-08 12:38:40 -03:00
|
|
|
\begin{classdesc}{NTEventLogHandler}{appname\optional{,
|
|
|
|
dllname\optional{, logtype}}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns a new instance of the \class{NTEventLogHandler} class. The
|
|
|
|
\var{appname} is used to define the application name as it appears in the
|
|
|
|
event log. An appropriate registry entry is created using this name.
|
|
|
|
The \var{dllname} should give the fully qualified pathname of a .dll or .exe
|
|
|
|
which contains message definitions to hold in the log (if not specified,
|
2003-07-08 12:38:40 -03:00
|
|
|
\code{'win32service.pyd'} is used - this is installed with the Win32
|
2003-01-25 17:29:41 -04:00
|
|
|
extensions and contains some basic placeholder message definitions.
|
|
|
|
Note that use of these placeholders will make your event logs big, as the
|
|
|
|
entire message source is held in the log. If you want slimmer logs, you have
|
|
|
|
to pass in the name of your own .dll or .exe which contains the message
|
|
|
|
definitions you want to use in the event log). The \var{logtype} is one of
|
2003-07-08 12:38:40 -03:00
|
|
|
\code{'Application'}, \code{'System'} or \code{'Security'}, and
|
|
|
|
defaults to \code{'Application'}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{close}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
At this point, you can remove the application name from the registry as a
|
|
|
|
source of event log entries. However, if you do this, you will not be able
|
|
|
|
to see the events as you intended in the Event Log Viewer - it needs to be
|
|
|
|
able to access the registry to get the .dll name. The current version does
|
|
|
|
not do this (in fact it doesn't do anything).
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{emit}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Determines the message ID, event category and event type, and then logs the
|
|
|
|
message in the NT event log.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{getEventCategory}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns the event category for the record. Override this if you
|
|
|
|
want to specify your own categories. This version returns 0.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{getEventType}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns the event type for the record. Override this if you want
|
|
|
|
to specify your own types. This version does a mapping using the
|
|
|
|
handler's typemap attribute, which is set up in \method{__init__()}
|
|
|
|
to a dictionary which contains mappings for \constant{DEBUG},
|
|
|
|
\constant{INFO}, \constant{WARNING}, \constant{ERROR} and
|
|
|
|
\constant{CRITICAL}. If you are using your own levels, you will either need
|
|
|
|
to override this method or place a suitable dictionary in the
|
|
|
|
handler's \var{typemap} attribute.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{getMessageID}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns the message ID for the record. If you are using your
|
|
|
|
own messages, you could do this by having the \var{msg} passed to the
|
|
|
|
logger being an ID rather than a format string. Then, in here,
|
|
|
|
you could use a dictionary lookup to get the message ID. This
|
|
|
|
version returns 1, which is the base message ID in
|
2003-07-08 12:38:40 -03:00
|
|
|
\file{win32service.pyd}.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{SMTPHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{SMTPHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports sending logging messages to
|
|
|
|
an email address via SMTP.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject}
|
|
|
|
Returns a new instance of the \class{SMTPHandler} class. The
|
|
|
|
instance is initialized with the from and to addresses and subject
|
2005-02-18 07:50:11 -04:00
|
|
|
line of the email. The \var{toaddrs} should be a list of strings. To specify a
|
2002-11-13 23:57:19 -04:00
|
|
|
non-standard SMTP port, use the (host, port) tuple format for the
|
2003-01-25 17:29:41 -04:00
|
|
|
\var{mailhost} argument. If you use a string, the standard SMTP port
|
|
|
|
is used.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{emit}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Formats the record and sends it to the specified addressees.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{getSubject}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
If you want to specify a subject line which is record-dependent,
|
2002-11-13 23:57:19 -04:00
|
|
|
override this method.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{MemoryHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{MemoryHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports buffering of logging
|
|
|
|
records in memory, periodically flushing them to a \dfn{target}
|
|
|
|
handler. Flushing occurs whenever the buffer is full, or when an event
|
|
|
|
of a certain severity or greater is seen.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\class{MemoryHandler} is a subclass of the more general
|
|
|
|
\class{BufferingHandler}, which is an abstract class. This buffers logging
|
|
|
|
records in memory. Whenever each record is added to the buffer, a
|
|
|
|
check is made by calling \method{shouldFlush()} to see if the buffer
|
|
|
|
should be flushed. If it should, then \method{flush()} is expected to
|
|
|
|
do the needful.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{BufferingHandler}{capacity}
|
|
|
|
Initializes the handler with a buffer of the specified capacity.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{emit}{record}
|
2003-01-25 17:29:41 -04:00
|
|
|
Appends the record to the buffer. If \method{shouldFlush()} returns true,
|
|
|
|
calls \method{flush()} to process the buffer.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{flush}{}
|
2003-01-25 17:46:53 -04:00
|
|
|
You can override this to implement custom flushing behavior. This version
|
2003-01-25 17:29:41 -04:00
|
|
|
just zaps the buffer to empty.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{shouldFlush}{record}
|
|
|
|
Returns true if the buffer is up to capacity. This method can be
|
|
|
|
overridden to implement custom flushing strategies.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel
|
2003-02-18 10:20:07 -04:00
|
|
|
\optional{, target}}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns a new instance of the \class{MemoryHandler} class. The
|
|
|
|
instance is initialized with a buffer size of \var{capacity}. If
|
|
|
|
\var{flushLevel} is not specified, \constant{ERROR} is used. If no
|
|
|
|
\var{target} is specified, the target will need to be set using
|
|
|
|
\method{setTarget()} before this handler does anything useful.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{close}{}
|
|
|
|
Calls \method{flush()}, sets the target to \constant{None} and
|
|
|
|
clears the buffer.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{flush}{}
|
2003-01-25 17:29:41 -04:00
|
|
|
For a \class{MemoryHandler}, flushing means just sending the buffered
|
|
|
|
records to the target, if there is one. Override if you want
|
2003-01-25 17:46:53 -04:00
|
|
|
different behavior.
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{methoddesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{setTarget}{target}
|
|
|
|
Sets the target handler for this handler.
|
|
|
|
\end{methoddesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{shouldFlush}{record}
|
|
|
|
Checks for buffer full or a record at the \var{flushLevel} or higher.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsubsection{HTTPHandler}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The \class{HTTPHandler} class, located in the
|
|
|
|
\module{logging.handlers} module, supports sending logging messages to
|
|
|
|
a Web server, using either \samp{GET} or \samp{POST} semantics.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{HTTPHandler}{host, url\optional{, method}}
|
|
|
|
Returns a new instance of the \class{HTTPHandler} class. The
|
|
|
|
instance is initialized with a host address, url and HTTP method.
|
2005-10-28 21:40:15 -03:00
|
|
|
The \var{host} can be of the form \code{host:port}, should you need to
|
|
|
|
use a specific port number. If no \var{method} is specified, \samp{GET}
|
|
|
|
is used.
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{emit}{record}
|
|
|
|
Sends the record to the Web server as an URL-encoded dictionary.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\subsection{Formatter Objects}
|
|
|
|
|
|
|
|
\class{Formatter}s have the following attributes and methods. They are
|
|
|
|
responsible for converting a \class{LogRecord} to (usually) a string
|
|
|
|
which can be interpreted by either a human or an external system. The
|
|
|
|
base
|
|
|
|
\class{Formatter} allows a formatting string to be specified. If none is
|
2004-04-15 03:18:48 -03:00
|
|
|
supplied, the default value of \code{'\%(message)s'} is used.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
A Formatter can be initialized with a format string which makes use of
|
2003-06-27 18:43:39 -03:00
|
|
|
knowledge of the \class{LogRecord} attributes - such as the default value
|
|
|
|
mentioned above making use of the fact that the user's message and
|
2004-04-09 15:26:40 -03:00
|
|
|
arguments are pre-formatted into a \class{LogRecord}'s \var{message}
|
2003-07-08 05:40:20 -03:00
|
|
|
attribute. This format string contains standard python \%-style
|
|
|
|
mapping keys. See section \ref{typesseq-strings}, ``String Formatting
|
|
|
|
Operations,'' for more information on string formatting.
|
|
|
|
|
2004-04-09 15:26:40 -03:00
|
|
|
Currently, the useful mapping keys in a \class{LogRecord} are:
|
2003-07-08 05:40:20 -03:00
|
|
|
|
2003-07-08 12:38:40 -03:00
|
|
|
\begin{tableii}{l|l}{code}{Format}{Description}
|
|
|
|
\lineii{\%(name)s} {Name of the logger (logging channel).}
|
|
|
|
\lineii{\%(levelno)s} {Numeric logging level for the message
|
|
|
|
(\constant{DEBUG}, \constant{INFO},
|
|
|
|
\constant{WARNING}, \constant{ERROR},
|
|
|
|
\constant{CRITICAL}).}
|
|
|
|
\lineii{\%(levelname)s}{Text logging level for the message
|
|
|
|
(\code{'DEBUG'}, \code{'INFO'},
|
|
|
|
\code{'WARNING'}, \code{'ERROR'},
|
|
|
|
\code{'CRITICAL'}).}
|
|
|
|
\lineii{\%(pathname)s} {Full pathname of the source file where the logging
|
|
|
|
call was issued (if available).}
|
|
|
|
\lineii{\%(filename)s} {Filename portion of pathname.}
|
|
|
|
\lineii{\%(module)s} {Module (name portion of filename).}
|
2006-02-12 22:04:37 -04:00
|
|
|
\lineii{\%(funcName)s} {Name of function containing the logging call.}
|
2003-07-08 12:38:40 -03:00
|
|
|
\lineii{\%(lineno)d} {Source line number where the logging call was issued
|
|
|
|
(if available).}
|
2004-04-09 15:26:40 -03:00
|
|
|
\lineii{\%(created)f} {Time when the \class{LogRecord} was created (as
|
2003-07-08 12:38:40 -03:00
|
|
|
returned by \function{time.time()}).}
|
2006-10-31 13:34:31 -04:00
|
|
|
\lineii{\%(relativeCreated)d} {Time in milliseconds when the LogRecord was
|
|
|
|
created, relative to the time the logging module was
|
|
|
|
loaded.}
|
2004-04-09 15:26:40 -03:00
|
|
|
\lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord}
|
|
|
|
was created. By default this is of the form
|
2003-07-08 12:38:40 -03:00
|
|
|
``2003-07-08 16:49:45,896'' (the numbers after the
|
|
|
|
comma are millisecond portion of the time).}
|
|
|
|
\lineii{\%(msecs)d} {Millisecond portion of the time when the
|
|
|
|
\class{LogRecord} was created.}
|
|
|
|
\lineii{\%(thread)d} {Thread ID (if available).}
|
2005-03-31 16:18:06 -04:00
|
|
|
\lineii{\%(threadName)s} {Thread name (if available).}
|
2003-07-08 12:38:40 -03:00
|
|
|
\lineii{\%(process)d} {Process ID (if available).}
|
|
|
|
\lineii{\%(message)s} {The logged message, computed as \code{msg \% args}.}
|
2003-07-08 05:40:20 -03:00
|
|
|
\end{tableii}
|
2003-01-25 17:29:41 -04:00
|
|
|
|
2006-02-12 22:04:37 -04:00
|
|
|
\versionchanged[\var{funcName} was added]{2.5}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}}
|
|
|
|
Returns a new instance of the \class{Formatter} class. The
|
|
|
|
instance is initialized with a format string for the message as a whole,
|
|
|
|
as well as a format string for the date/time portion of a message. If
|
2003-07-08 13:26:34 -03:00
|
|
|
no \var{fmt} is specified, \code{'\%(message)s'} is used. If no \var{datefmt}
|
2003-01-25 17:29:41 -04:00
|
|
|
is specified, the ISO8601 date format is used.
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{format}{record}
|
|
|
|
The record's attribute dictionary is used as the operand to a
|
|
|
|
string formatting operation. Returns the resulting string.
|
|
|
|
Before formatting the dictionary, a couple of preparatory steps
|
|
|
|
are carried out. The \var{message} attribute of the record is computed
|
|
|
|
using \var{msg} \% \var{args}. If the formatting string contains
|
2003-07-08 12:38:40 -03:00
|
|
|
\code{'(asctime)'}, \method{formatTime()} is called to format the
|
2003-01-25 17:29:41 -04:00
|
|
|
event time. If there is exception information, it is formatted using
|
|
|
|
\method{formatException()} and appended to the message.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{formatTime}{record\optional{, datefmt}}
|
|
|
|
This method should be called from \method{format()} by a formatter which
|
|
|
|
wants to make use of a formatted time. This method can be overridden
|
|
|
|
in formatters to provide for any specific requirement, but the
|
2003-01-25 17:46:53 -04:00
|
|
|
basic behavior is as follows: if \var{datefmt} (a string) is specified,
|
2003-01-28 18:09:16 -04:00
|
|
|
it is used with \function{time.strftime()} to format the creation time of the
|
2003-01-25 17:29:41 -04:00
|
|
|
record. Otherwise, the ISO8601 format is used. The resulting
|
|
|
|
string is returned.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{formatException}{exc_info}
|
|
|
|
Formats the specified exception information (a standard exception tuple
|
2003-01-28 18:09:16 -04:00
|
|
|
as returned by \function{sys.exc_info()}) as a string. This default
|
|
|
|
implementation just uses \function{traceback.print_exception()}.
|
2003-01-25 17:29:41 -04:00
|
|
|
The resulting string is returned.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\subsection{Filter Objects}
|
|
|
|
|
|
|
|
\class{Filter}s can be used by \class{Handler}s and \class{Logger}s for
|
|
|
|
more sophisticated filtering than is provided by levels. The base filter
|
|
|
|
class only allows events which are below a certain point in the logger
|
|
|
|
hierarchy. For example, a filter initialized with "A.B" will allow events
|
|
|
|
logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB",
|
|
|
|
"B.A.B" etc. If initialized with the empty string, all events are passed.
|
|
|
|
|
|
|
|
\begin{classdesc}{Filter}{\optional{name}}
|
|
|
|
Returns an instance of the \class{Filter} class. If \var{name} is specified,
|
|
|
|
it names a logger which, together with its children, will have its events
|
|
|
|
allowed through the filter. If no name is specified, allows every event.
|
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{methoddesc}{filter}{record}
|
|
|
|
Is the specified record to be logged? Returns zero for no, nonzero for
|
|
|
|
yes. If deemed appropriate, the record may be modified in-place by this
|
|
|
|
method.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\subsection{LogRecord Objects}
|
|
|
|
|
2004-04-09 15:26:40 -03:00
|
|
|
\class{LogRecord} instances are created every time something is logged. They
|
2003-01-25 17:29:41 -04:00
|
|
|
contain all the information pertinent to the event being logged. The
|
|
|
|
main information passed in is in msg and args, which are combined
|
|
|
|
using msg \% args to create the message field of the record. The record
|
|
|
|
also includes information such as when the record was created, the
|
|
|
|
source line where the logging call was made, and any exception
|
|
|
|
information to be logged.
|
|
|
|
|
|
|
|
\begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args,
|
2006-10-03 15:20:26 -03:00
|
|
|
exc_info \optional{, func}}
|
2003-01-25 17:29:41 -04:00
|
|
|
Returns an instance of \class{LogRecord} initialized with interesting
|
|
|
|
information. The \var{name} is the logger name; \var{lvl} is the
|
|
|
|
numeric level; \var{pathname} is the absolute pathname of the source
|
|
|
|
file in which the logging call was made; \var{lineno} is the line
|
|
|
|
number in that file where the logging call is found; \var{msg} is the
|
|
|
|
user-supplied message (a format string); \var{args} is the tuple
|
|
|
|
which, together with \var{msg}, makes up the user message; and
|
|
|
|
\var{exc_info} is the exception tuple obtained by calling
|
|
|
|
\function{sys.exc_info() }(or \constant{None}, if no exception information
|
2006-10-03 15:02:37 -03:00
|
|
|
is available). The \var{func} is the name of the function from which the
|
2006-10-03 15:20:26 -03:00
|
|
|
logging call was made. If not specified, it defaults to \var{None}.
|
2006-10-03 15:02:37 -03:00
|
|
|
\versionchanged[\var{func} was added]{2.5}
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{classdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2004-12-02 17:27:42 -04:00
|
|
|
\begin{methoddesc}{getMessage}{}
|
|
|
|
Returns the message for this \class{LogRecord} instance after merging any
|
|
|
|
user-supplied arguments with the message.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsection{Thread Safety}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The logging module is intended to be thread-safe without any special work
|
|
|
|
needing to be done by its clients. It achieves this though using threading
|
|
|
|
locks; there is one lock to serialize access to the module's shared data,
|
|
|
|
and each handler also creates a lock to serialize access to its underlying
|
|
|
|
I/O.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\subsection{Configuration}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
|
2004-04-08 16:44:31 -03:00
|
|
|
\subsubsection{Configuration functions%
|
|
|
|
\label{logging-config-api}}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2006-01-22 07:58:39 -04:00
|
|
|
The following functions configure the logging module. They are located in the
|
|
|
|
\module{logging.config} module. Their use is optional --- you can configure
|
|
|
|
the logging module using these functions or by making calls to the
|
|
|
|
main API (defined in \module{logging} itself) and defining handlers
|
|
|
|
which are declared either in \module{logging} or
|
|
|
|
\module{logging.handlers}.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{fileConfig}{fname\optional{, defaults}}
|
|
|
|
Reads the logging configuration from a ConfigParser-format file named
|
|
|
|
\var{fname}. This function can be called several times from an application,
|
|
|
|
allowing an end user the ability to select from various pre-canned
|
|
|
|
configurations (if the developer provides a mechanism to present the
|
|
|
|
choices and load the chosen configuration). Defaults to be passed to
|
|
|
|
ConfigParser can be specified in the \var{defaults} argument.
|
|
|
|
\end{funcdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{listen}{\optional{port}}
|
|
|
|
Starts up a socket server on the specified port, and listens for new
|
|
|
|
configurations. If no port is specified, the module's default
|
|
|
|
\constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations
|
|
|
|
will be sent as a file suitable for processing by \function{fileConfig()}.
|
|
|
|
Returns a \class{Thread} instance on which you can call \method{start()}
|
|
|
|
to start the server, and which you can \method{join()} when appropriate.
|
2005-06-05 17:39:36 -03:00
|
|
|
To stop the server, call \function{stopListening()}. To send a configuration
|
|
|
|
to the socket, read in the configuration file and send it to the socket
|
|
|
|
as a string of bytes preceded by a four-byte length packed in binary using
|
2006-07-20 17:11:57 -03:00
|
|
|
struct.\code{pack('>L', n)}.
|
2003-01-25 17:29:41 -04:00
|
|
|
\end{funcdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
\begin{funcdesc}{stopListening}{}
|
|
|
|
Stops the listening server which was created with a call to
|
|
|
|
\function{listen()}. This is typically called before calling \method{join()}
|
|
|
|
on the return value from \function{listen()}.
|
|
|
|
\end{funcdesc}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2004-04-08 16:44:31 -03:00
|
|
|
\subsubsection{Configuration file format%
|
|
|
|
\label{logging-config-fileformat}}
|
2003-01-25 17:29:41 -04:00
|
|
|
|
2004-04-09 15:26:40 -03:00
|
|
|
The configuration file format understood by \function{fileConfig()} is
|
2003-01-25 17:29:41 -04:00
|
|
|
based on ConfigParser functionality. The file must contain sections
|
|
|
|
called \code{[loggers]}, \code{[handlers]} and \code{[formatters]}
|
|
|
|
which identify by name the entities of each type which are defined in
|
|
|
|
the file. For each such entity, there is a separate section which
|
|
|
|
identified how that entity is configured. Thus, for a logger named
|
|
|
|
\code{log01} in the \code{[loggers]} section, the relevant
|
|
|
|
configuration details are held in a section
|
|
|
|
\code{[logger_log01]}. Similarly, a handler called \code{hand01} in
|
|
|
|
the \code{[handlers]} section will have its configuration held in a
|
|
|
|
section called \code{[handler_hand01]}, while a formatter called
|
|
|
|
\code{form01} in the \code{[formatters]} section will have its
|
|
|
|
configuration specified in a section called
|
|
|
|
\code{[formatter_form01]}. The root logger configuration must be
|
|
|
|
specified in a section called \code{[logger_root]}.
|
|
|
|
|
|
|
|
Examples of these sections in the file are given below.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
[loggers]
|
|
|
|
keys=root,log02,log03,log04,log05,log06,log07
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
[handlers]
|
|
|
|
keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
|
|
|
|
|
|
|
|
[formatters]
|
|
|
|
keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The root logger must specify a level and a list of handlers. An
|
|
|
|
example of a root logger section is given below.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
[logger_root]
|
|
|
|
level=NOTSET
|
|
|
|
handlers=hand01
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{level} entry can be one of \code{DEBUG, INFO, WARNING,
|
|
|
|
ERROR, CRITICAL} or \code{NOTSET}. For the root logger only,
|
|
|
|
\code{NOTSET} means that all messages will be logged. Level values are
|
|
|
|
\function{eval()}uated in the context of the \code{logging} package's
|
|
|
|
namespace.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{handlers} entry is a comma-separated list of handler names,
|
|
|
|
which must appear in the \code{[handlers]} section. These names must
|
|
|
|
appear in the \code{[handlers]} section and have corresponding
|
|
|
|
sections in the configuration file.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
For loggers other than the root logger, some additional information is
|
|
|
|
required. This is illustrated by the following example.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
[logger_parser]
|
|
|
|
level=DEBUG
|
|
|
|
handlers=hand01
|
|
|
|
propagate=1
|
|
|
|
qualname=compiler.parser
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{level} and \code{handlers} entries are interpreted as for
|
|
|
|
the root logger, except that if a non-root logger's level is specified
|
|
|
|
as \code{NOTSET}, the system consults loggers higher up the hierarchy
|
|
|
|
to determine the effective level of the logger. The \code{propagate}
|
|
|
|
entry is set to 1 to indicate that messages must propagate to handlers
|
|
|
|
higher up the logger hierarchy from this logger, or 0 to indicate that
|
|
|
|
messages are \strong{not} propagated to handlers up the hierarchy. The
|
|
|
|
\code{qualname} entry is the hierarchical channel name of the logger,
|
2004-07-03 08:45:53 -03:00
|
|
|
that is to say the name used by the application to get the logger.
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
Sections which specify handler configuration are exemplified by the
|
|
|
|
following.
|
|
|
|
|
2002-11-13 23:57:19 -04:00
|
|
|
\begin{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
[handler_hand01]
|
|
|
|
class=StreamHandler
|
|
|
|
level=NOTSET
|
|
|
|
formatter=form01
|
|
|
|
args=(sys.stdout,)
|
|
|
|
\end{verbatim}
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{class} entry indicates the handler's class (as determined by
|
|
|
|
\function{eval()} in the \code{logging} package's namespace). The
|
|
|
|
\code{level} is interpreted as for loggers, and \code{NOTSET} is taken
|
|
|
|
to mean "log everything".
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{formatter} entry indicates the key name of the formatter for
|
|
|
|
this handler. If blank, a default formatter
|
|
|
|
(\code{logging._defaultFormatter}) is used. If a name is specified, it
|
|
|
|
must appear in the \code{[formatters]} section and have a
|
|
|
|
corresponding section in the configuration file.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
The \code{args} entry, when \function{eval()}uated in the context of
|
|
|
|
the \code{logging} package's namespace, is the list of arguments to
|
|
|
|
the constructor for the handler class. Refer to the constructors for
|
|
|
|
the relevant handlers, or to the examples below, to see how typical
|
|
|
|
entries are constructed.
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
[handler_hand02]
|
|
|
|
class=FileHandler
|
|
|
|
level=DEBUG
|
|
|
|
formatter=form02
|
|
|
|
args=('python.log', 'w')
|
|
|
|
|
|
|
|
[handler_hand03]
|
|
|
|
class=handlers.SocketHandler
|
|
|
|
level=INFO
|
|
|
|
formatter=form03
|
|
|
|
args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)
|
|
|
|
|
|
|
|
[handler_hand04]
|
|
|
|
class=handlers.DatagramHandler
|
|
|
|
level=WARN
|
|
|
|
formatter=form04
|
|
|
|
args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)
|
|
|
|
|
|
|
|
[handler_hand05]
|
|
|
|
class=handlers.SysLogHandler
|
|
|
|
level=ERROR
|
|
|
|
formatter=form05
|
|
|
|
args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
|
|
|
|
|
|
|
|
[handler_hand06]
|
2004-07-12 12:48:04 -03:00
|
|
|
class=handlers.NTEventLogHandler
|
2003-01-25 17:29:41 -04:00
|
|
|
level=CRITICAL
|
|
|
|
formatter=form06
|
|
|
|
args=('Python Application', '', 'Application')
|
|
|
|
|
|
|
|
[handler_hand07]
|
2004-07-12 12:48:04 -03:00
|
|
|
class=handlers.SMTPHandler
|
2003-01-25 17:29:41 -04:00
|
|
|
level=WARN
|
|
|
|
formatter=form07
|
|
|
|
args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
|
|
|
|
|
|
|
|
[handler_hand08]
|
2004-07-12 12:48:04 -03:00
|
|
|
class=handlers.MemoryHandler
|
2003-01-25 17:29:41 -04:00
|
|
|
level=NOTSET
|
|
|
|
formatter=form08
|
|
|
|
target=
|
|
|
|
args=(10, ERROR)
|
|
|
|
|
|
|
|
[handler_hand09]
|
2004-07-12 12:48:04 -03:00
|
|
|
class=handlers.HTTPHandler
|
2003-01-25 17:29:41 -04:00
|
|
|
level=NOTSET
|
|
|
|
formatter=form09
|
|
|
|
args=('localhost:9022', '/log', 'GET')
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{verbatim}
|
|
|
|
|
2003-01-25 17:29:41 -04:00
|
|
|
Sections which specify formatter configuration are typified by the following.
|
2002-11-13 23:57:19 -04:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
[formatter_form01]
|
|
|
|
format=F1 %(asctime)s %(levelname)s %(message)s
|
|
|
|
datefmt=
|
2006-01-22 07:58:39 -04:00
|
|
|
class=logging.Formatter
|
2002-11-13 23:57:19 -04:00
|
|
|
\end{verbatim}
|
2003-01-25 17:29:41 -04:00
|
|
|
|
|
|
|
The \code{format} entry is the overall format string, and the
|
|
|
|
\code{datefmt} entry is the \function{strftime()}-compatible date/time format
|
|
|
|
string. If empty, the package substitutes ISO8601 format date/times, which
|
|
|
|
is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S".
|
|
|
|
The ISO8601 format also specifies milliseconds, which are appended to the
|
|
|
|
result of using the above format string, with a comma separator. An example
|
|
|
|
time in ISO8601 format is \code{2003-01-23 00:29:50,411}.
|
2006-01-22 07:58:39 -04:00
|
|
|
|
|
|
|
The \code{class} entry is optional. It indicates the name of the
|
|
|
|
formatter's class (as a dotted module and class name.) This option is
|
|
|
|
useful for instantiating a \class{Formatter} subclass. Subclasses of
|
|
|
|
\class{Formatter} can present exception tracebacks in an expanded or
|
|
|
|
condensed format.
|