📄 liblogging.tex
字号:
\section{\module{logging} --- Logging facility for Python}\declaremodule{standard}{logging}% These apply to all modules, and may be given more than once:\moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com}\sectionauthor{Vinay Sajip}{vinay_sajip@red-dove.com}\modulesynopsis{Logging module for Python based on \pep{282}.}\indexii{Errors}{logging}\versionadded{2.3}This module defines functions and classes which implement a flexibleerror logging system for applications.Logging is performed by calling methods on instances of the\class{Logger} class (hereafter called \dfn{loggers}). Each instance has aname, and they are conceptually arranged in a name space hierarchyusing 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 anapplication 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 aconvenience, you indicate the importance of a logged message by callingan appropriate method of \class{Logger}. The methods are\method{debug()}, \method{info()}, \method{warning()}, \method{error()} and\method{critical()}, which mirror the default levels. You are notconstrained to use these levels: you can specify your own and use amore general \class{Logger} method, \method{log()}, which takes anexplicit level argument.Levels can also be associated with loggers, being set either by thedeveloper or through loading a saved logging configuration. When alogging method is called on a logger, the logger compares its ownlevel with the level associated with the method call. If the logger'slevel is higher than the method call's, no logging message is actuallygenerated. This is the basic mechanism controlling the verbosity oflogging output.Logging messages are encoded as instances of the \class{LogRecord} class.When a logger decides to actually log an event, an \class{LogRecord}instance is created from the logging message.Logging messages are subjected to a dispatch mechanism through theuse of \dfn{handlers}, which are instances of subclasses of the\class{Handler} class. Handlers are responsible for ensuring that a loggedmessage (in the form of a \class{LogRecord}) ends up in a particularlocation (or set of locations) which is useful for the target audience forthat message (such as end users, support desk staff, system administrators,developers). Handlers are passed \class{LogRecord} instances intended forparticular destinations. Each logger can have zero, one or more handlersassociated with it (via the \method{addHandler} method of \class{Logger}).In addition to any handlers directly associated with a logger,\emph{all handlers associated with all ancestors of the logger} arecalled to dispatch the message.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.If a handler decides to actually dispatch an event, the \method{emit()} methodis used to send the message to its destination. Most user-defined subclassesof \class{Handler} will need to override this \method{emit()}.In addition to the base \class{Handler} class, many useful subclassesare provided:\begin{enumerate}\item \class{StreamHandler} instances send error messages tostreams (file-like objects).\item \class{FileHandler} instances send error messages to diskfiles.\item \class{RotatingFileHandler} instances send error messages to diskfiles, with support for maximum log file sizes and log file rotation.\item \class{SocketHandler} instances send error messages toTCP/IP sockets.\item \class{DatagramHandler} instances send error messages to UDPsockets.\item \class{SMTPHandler} instances send error messages to adesignated email address.\item \class{SysLogHandler} instances send error messages to a\UNIX{} syslog daemon, possibly on a remote machine.\item \class{NTEventLogHandler} instances send error messages to aWindows NT/2000/XP event log.\item \class{MemoryHandler} instances send error messages to abuffer in memory, which is flushed whenever specific criteria aremet.\item \class{HTTPHandler} instances send error messages to anHTTP server using either \samp{GET} or \samp{POST} semantics.\end{enumerate}The \class{StreamHandler} and \class{FileHandler} classes are definedin 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 stringsuitable 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 forheader 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\class{Handler} instances (through their \method{addFilter()} method).Before deciding to process a message further, both loggers and handlersconsult all their filters for permission. If any filter returns a falsevalue, the message is not processed further.The basic \class{Filter} functionality allows filtering by specific loggername. If this feature is used, messages sent to the named logger and itschildren 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, returna logger which is the root logger of the hierarchy.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 differentparts of an application.\end{funcdesc}\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 thearguments which are merged into \var{msg}. The only keyword argument in\var{kwargs} which is inspected is \var{exc_info} which, if it does notevaluate as false, causes exception information (via a call to\function{sys.exc_info()}) to be added to the logging message.\end{funcdesc}\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()}.\end{funcdesc}\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()}.\end{funcdesc}\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()}.\end{funcdesc}\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()}.\end{funcdesc}\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 infois added to the logging message. This function should only be calledfrom an exception handler.\end{funcdesc}\begin{funcdesc}{disable}{lvl}Provides an overriding level \var{lvl} for all loggers which takesprecedence over the logger's own level. When the need arises totemporarily throttle logging output down across the whole application,this function can be useful.\end{funcdesc}\begin{funcdesc}{addLevelName}{lvl, levelName}Associates level \var{lvl} with text \var{levelName} in an internaldictionary, which is used to map numeric levels to a textualrepresentation, for example when a \class{Formatter} formats a message.This function can also be used to define your own levels. The onlyconstraints are that all levels used must be registered using thisfunction, levels should be positive integers and they should increasein increasing order of severity.\end{funcdesc}\begin{funcdesc}{getLevelName}{lvl}Returns the textual representation of logging level \var{lvl}. If thelevel 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 levelswith names using \function{addLevelName()} then the name you have associatedwith \var{lvl} is returned. Otherwise, the string "Level \%s" \% lvl isreturned.\end{funcdesc}\begin{funcdesc}{makeLogRecord}{attrdict}Creates and returns a new \class{LogRecord} instance whose attributes aredefined by \var{attrdict}. This function is useful for taking a pickled\class{LogRecord} attribute dictionary, sent over a socket, and reconstitutingit as a \class{LogRecord} instance at the receiving end.\end{funcdesc}\begin{funcdesc}{basicConfig}{}Does basic configuration for the logging system by creating a\class{StreamHandler} with a default \class{Formatter} and adding it tothe 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 theroot logger.\end{funcdesc}\begin{funcdesc}{shutdown}{}Informs the logging system to perform an orderly shutdown by flushing andclosing all handlers.\end{funcdesc}\begin{funcdesc}{setLoggerClass}{klass}Tells the logging system to use the class \var{klass} when instantiating alogger. The class should define \method{__init__()} such that only a nameargument is required, and the \method{__init__()} should call\method{Logger.__init__()}. This function is typically called before anyloggers are instantiated by applications which need to use custom loggerbehavior.\end{funcdesc}\begin{seealso} \seepep{282}{A Logging System} {The proposal which described this feature for inclusion in the Python standard library.} \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 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 library.}\end{seealso}\subsection{Logger Objects}Loggers have the following attributes and methods. Note that Loggers arenever instantiated directly, but always through the module-level function\function{logging.getLogger(name)}.\begin{datadesc}{propagate}If this evaluates to false, logging messages are not passed by thislogger or by child loggers to higher level (ancestor) loggers. Theconstructor sets this attribute to 1.\end{datadesc}\begin{methoddesc}{setLevel}{lvl}Sets the threshold for this logger to \var{lvl}. Logging messageswhich are less severe than \var{lvl} will be ignored. When a logger iscreated, the level is set to \constant{NOTSET} (which causes all messagesto be processed in the root logger, or delegation to the parent in non-rootloggers).\end{methoddesc}\begin{methoddesc}{isEnabledFor}{lvl}Indicates if a message of severity \var{lvl} would be processed bythis logger. This method checks first the module-level level set by\function{logging.disable(lvl)} and then the logger's effective level asdetermined by \method{getEffectiveLevel()}.\end{methoddesc}\begin{methoddesc}{getEffectiveLevel}{}Indicates the effective level for this logger. If a value other than\constant{NOTSET} has been set using \method{setLevel()}, it is returned.Otherwise, the hierarchy is traversed towards the root until a valueother than \constant{NOTSET} is found, and that value is returned.\end{methoddesc}\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 thearguments which are merged into \var{msg}. The only keyword argument in\var{kwargs} which is inspected is \var{exc_info} which, if it does notevaluate as false, causes exception information (via a call to\function{sys.exc_info()}) to be added to the logging message.\end{methoddesc}\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()}.\end{methoddesc}\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()}.\end{methoddesc}\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()}.\end{methoddesc}\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()}.\end{methoddesc}\begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}}Logs a message with level \var{lvl} on this logger.The other arguments are interpreted as for \method{debug()}.\end{methoddesc}\begin{methoddesc}{exception}{msg\optional{, *args}}Logs a message with level \constant{ERROR} on this logger.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -