Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1 | \section{\module{logging} --- |
| 2 | Logging facility for Python} |
| 3 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 4 | \declaremodule{standard}{logging} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 5 | |
| 6 | % These apply to all modules, and may be given more than once: |
| 7 | |
| 8 | \moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 9 | \sectionauthor{Vinay Sajip}{vinay_sajip@red-dove.com} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 10 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 11 | \modulesynopsis{Logging module for Python based on \pep{282}.} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 12 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 13 | \indexii{Errors}{logging} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 15 | \versionadded{2.3} |
| 16 | This module defines functions and classes which implement a flexible |
| 17 | error logging system for applications. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 18 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 19 | Logging is performed by calling methods on instances of the |
| 20 | \class{Logger} class (hereafter called \dfn{loggers}). Each instance has a |
| 21 | name, and they are conceptually arranged in a name space hierarchy |
| 22 | using dots (periods) as separators. For example, a logger named |
| 23 | "scan" is the parent of loggers "scan.text", "scan.html" and "scan.pdf". |
| 24 | Logger names can be anything you want, and indicate the area of an |
| 25 | application in which a logged message originates. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 26 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 27 | Logged messages also have levels of importance associated with them. |
| 28 | The default levels provided are \constant{DEBUG}, \constant{INFO}, |
| 29 | \constant{WARNING}, \constant{ERROR} and \constant{CRITICAL}. As a |
| 30 | convenience, you indicate the importance of a logged message by calling |
| 31 | an appropriate method of \class{Logger}. The methods are |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 32 | \method{debug()}, \method{info()}, \method{warning()}, \method{error()} and |
| 33 | \method{critical()}, which mirror the default levels. You are not |
| 34 | constrained to use these levels: you can specify your own and use a |
| 35 | more general \class{Logger} method, \method{log()}, which takes an |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 36 | explicit level argument. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 37 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 38 | Levels can also be associated with loggers, being set either by the |
| 39 | developer or through loading a saved logging configuration. When a |
| 40 | logging method is called on a logger, the logger compares its own |
| 41 | level with the level associated with the method call. If the logger's |
| 42 | level is higher than the method call's, no logging message is actually |
| 43 | generated. This is the basic mechanism controlling the verbosity of |
| 44 | logging output. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 45 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 46 | Logging messages are encoded as instances of the \class{LogRecord} class. |
| 47 | When a logger decides to actually log an event, an \class{LogRecord} |
| 48 | instance is created from the logging message. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 49 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 50 | Logging messages are subjected to a dispatch mechanism through the |
| 51 | use of \dfn{handlers}, which are instances of subclasses of the |
| 52 | \class{Handler} class. Handlers are responsible for ensuring that a logged |
| 53 | message (in the form of a \class{LogRecord}) ends up in a particular |
| 54 | location (or set of locations) which is useful for the target audience for |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 55 | that message (such as end users, support desk staff, system administrators, |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 56 | developers). Handlers are passed \class{LogRecord} instances intended for |
| 57 | particular destinations. Each logger can have zero, one or more handlers |
| 58 | associated with it (via the \method{addHandler} method of \class{Logger}). |
| 59 | In addition to any handlers directly associated with a logger, |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 60 | \emph{all handlers associated with all ancestors of the logger} are |
| 61 | called to dispatch the message. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 62 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 63 | Just as for loggers, handlers can have levels associated with them. |
| 64 | A handler's level acts as a filter in the same way as a logger's level does. |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 65 | If a handler decides to actually dispatch an event, the \method{emit()} method |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 66 | is used to send the message to its destination. Most user-defined subclasses |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 67 | of \class{Handler} will need to override this \method{emit()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 68 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 69 | In addition to the base \class{Handler} class, many useful subclasses |
| 70 | are provided: |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 71 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 72 | \begin{enumerate} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 73 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 74 | \item \class{StreamHandler} instances send error messages to |
| 75 | streams (file-like objects). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 76 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 77 | \item \class{FileHandler} instances send error messages to disk |
| 78 | files. |
| 79 | |
| 80 | \item \class{RotatingFileHandler} instances send error messages to disk |
| 81 | files, with support for maximum log file sizes and log file rotation. |
| 82 | |
| 83 | \item \class{SocketHandler} instances send error messages to |
| 84 | TCP/IP sockets. |
| 85 | |
| 86 | \item \class{DatagramHandler} instances send error messages to UDP |
| 87 | sockets. |
| 88 | |
| 89 | \item \class{SMTPHandler} instances send error messages to a |
| 90 | designated email address. |
| 91 | |
| 92 | \item \class{SysLogHandler} instances send error messages to a |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 93 | \UNIX{} syslog daemon, possibly on a remote machine. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 94 | |
| 95 | \item \class{NTEventLogHandler} instances send error messages to a |
| 96 | Windows NT/2000/XP event log. |
| 97 | |
| 98 | \item \class{MemoryHandler} instances send error messages to a |
| 99 | buffer in memory, which is flushed whenever specific criteria are |
| 100 | met. |
| 101 | |
| 102 | \item \class{HTTPHandler} instances send error messages to an |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 103 | HTTP server using either \samp{GET} or \samp{POST} semantics. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 104 | |
| 105 | \end{enumerate} |
| 106 | |
| 107 | The \class{StreamHandler} and \class{FileHandler} classes are defined |
| 108 | in the core logging package. The other handlers are defined in a sub- |
| 109 | module, \module{logging.handlers}. (There is also another sub-module, |
| 110 | \module{logging.config}, for configuration functionality.) |
| 111 | |
| 112 | Logged messages are formatted for presentation through instances of the |
| 113 | \class{Formatter} class. They are initialized with a format string |
| 114 | suitable for use with the \% operator and a dictionary. |
| 115 | |
| 116 | For formatting multiple messages in a batch, instances of |
| 117 | \class{BufferingFormatter} can be used. In addition to the format string |
| 118 | (which is applied to each message in the batch), there is provision for |
| 119 | header and trailer format strings. |
| 120 | |
| 121 | When filtering based on logger level and/or handler level is not enough, |
| 122 | instances of \class{Filter} can be added to both \class{Logger} and |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 123 | \class{Handler} instances (through their \method{addFilter()} method). |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 124 | Before deciding to process a message further, both loggers and handlers |
| 125 | consult all their filters for permission. If any filter returns a false |
| 126 | value, the message is not processed further. |
| 127 | |
| 128 | The basic \class{Filter} functionality allows filtering by specific logger |
| 129 | name. If this feature is used, messages sent to the named logger and its |
| 130 | children are allowed through the filter, and all others dropped. |
| 131 | |
| 132 | In addition to the classes described above, there are a number of module- |
| 133 | level functions. |
| 134 | |
| 135 | \begin{funcdesc}{getLogger}{\optional{name}} |
| 136 | Return a logger with the specified name or, if no name is specified, return |
| 137 | a logger which is the root logger of the hierarchy. |
| 138 | |
| 139 | All calls to this function with a given name return the same logger instance. |
| 140 | This means that logger instances never need to be passed between different |
| 141 | parts of an application. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 142 | \end{funcdesc} |
| 143 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 144 | \begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}} |
| 145 | Logs a message with level \constant{DEBUG} on the root logger. |
| 146 | The \var{msg} is the message format string, and the \var{args} are the |
| 147 | arguments which are merged into \var{msg}. The only keyword argument in |
| 148 | \var{kwargs} which is inspected is \var{exc_info} which, if it does not |
| 149 | evaluate as false, causes exception information (via a call to |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 150 | \function{sys.exc_info()}) to be added to the logging message. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 151 | \end{funcdesc} |
| 152 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 153 | \begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}} |
| 154 | Logs a message with level \constant{INFO} on the root logger. |
| 155 | The arguments are interpreted as for \function{debug()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 156 | \end{funcdesc} |
| 157 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 158 | \begin{funcdesc}{warning}{msg\optional{, *args\optional{, **kwargs}}} |
| 159 | Logs a message with level \constant{WARNING} on the root logger. |
| 160 | The arguments are interpreted as for \function{debug()}. |
| 161 | \end{funcdesc} |
| 162 | |
| 163 | \begin{funcdesc}{error}{msg\optional{, *args\optional{, **kwargs}}} |
| 164 | Logs a message with level \constant{ERROR} on the root logger. |
| 165 | The arguments are interpreted as for \function{debug()}. |
| 166 | \end{funcdesc} |
| 167 | |
| 168 | \begin{funcdesc}{critical}{msg\optional{, *args\optional{, **kwargs}}} |
| 169 | Logs a message with level \constant{CRITICAL} on the root logger. |
| 170 | The arguments are interpreted as for \function{debug()}. |
| 171 | \end{funcdesc} |
| 172 | |
| 173 | \begin{funcdesc}{exception}{msg\optional{, *args}} |
| 174 | Logs a message with level \constant{ERROR} on the root logger. |
| 175 | The arguments are interpreted as for \function{debug()}. Exception info |
| 176 | is added to the logging message. This function should only be called |
| 177 | from an exception handler. |
| 178 | \end{funcdesc} |
| 179 | |
| 180 | \begin{funcdesc}{disable}{lvl} |
| 181 | Provides an overriding level \var{lvl} for all loggers which takes |
| 182 | precedence over the logger's own level. When the need arises to |
| 183 | temporarily throttle logging output down across the whole application, |
| 184 | this function can be useful. |
| 185 | \end{funcdesc} |
| 186 | |
| 187 | \begin{funcdesc}{addLevelName}{lvl, levelName} |
| 188 | Associates level \var{lvl} with text \var{levelName} in an internal |
| 189 | dictionary, which is used to map numeric levels to a textual |
| 190 | representation, for example when a \class{Formatter} formats a message. |
| 191 | This function can also be used to define your own levels. The only |
| 192 | constraints are that all levels used must be registered using this |
| 193 | function, levels should be positive integers and they should increase |
| 194 | in increasing order of severity. |
| 195 | \end{funcdesc} |
| 196 | |
| 197 | \begin{funcdesc}{getLevelName}{lvl} |
| 198 | Returns the textual representation of logging level \var{lvl}. If the |
| 199 | level is one of the predefined levels \constant{CRITICAL}, |
| 200 | \constant{ERROR}, \constant{WARNING}, \constant{INFO} or \constant{DEBUG} |
| 201 | then you get the corresponding string. If you have associated levels |
| 202 | with names using \function{addLevelName()} then the name you have associated |
| 203 | with \var{lvl} is returned. Otherwise, the string "Level \%s" \% lvl is |
| 204 | returned. |
| 205 | \end{funcdesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 206 | |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 207 | \begin{funcdesc}{makeLogRecord}{attrdict} |
| 208 | Creates and returns a new \class{LogRecord} instance whose attributes are |
| 209 | defined by \var{attrdict}. This function is useful for taking a pickled |
| 210 | \class{LogRecord} attribute dictionary, sent over a socket, and reconstituting |
| 211 | it as a \class{LogRecord} instance at the receiving end. |
| 212 | \end{funcdesc} |
| 213 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 214 | \begin{funcdesc}{basicConfig}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 215 | Does basic configuration for the logging system by creating a |
| 216 | \class{StreamHandler} with a default \class{Formatter} and adding it to |
| 217 | the root logger. The functions \function{debug()}, \function{info()}, |
| 218 | \function{warning()}, \function{error()} and \function{critical()} will call |
| 219 | \function{basicConfig()} automatically if no handlers are defined for the |
| 220 | root logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 221 | \end{funcdesc} |
| 222 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 223 | \begin{funcdesc}{shutdown}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 224 | Informs the logging system to perform an orderly shutdown by flushing and |
| 225 | closing all handlers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 226 | \end{funcdesc} |
| 227 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 228 | \begin{funcdesc}{setLoggerClass}{klass} |
| 229 | Tells the logging system to use the class \var{klass} when instantiating a |
| 230 | logger. The class should define \method{__init__()} such that only a name |
| 231 | argument is required, and the \method{__init__()} should call |
| 232 | \method{Logger.__init__()}. This function is typically called before any |
| 233 | loggers are instantiated by applications which need to use custom logger |
| 234 | behavior. |
| 235 | \end{funcdesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 236 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 237 | |
| 238 | \begin{seealso} |
| 239 | \seepep{282}{A Logging System} |
| 240 | {The proposal which described this feature for inclusion in |
| 241 | the Python standard library.} |
| 242 | \end{seealso} |
| 243 | |
| 244 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 245 | \subsection{Logger Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 246 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 247 | Loggers have the following attributes and methods. Note that Loggers are |
| 248 | never instantiated directly, but always through the module-level function |
| 249 | \function{logging.getLogger(name)}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 250 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 251 | \begin{datadesc}{propagate} |
| 252 | If this evaluates to false, logging messages are not passed by this |
| 253 | logger or by child loggers to higher level (ancestor) loggers. The |
| 254 | constructor sets this attribute to 1. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 255 | \end{datadesc} |
| 256 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 257 | \begin{methoddesc}{setLevel}{lvl} |
| 258 | Sets the threshold for this logger to \var{lvl}. Logging messages |
| 259 | which are less severe than \var{lvl} will be ignored. When a logger is |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 260 | created, the level is set to \constant{NOTSET} (which causes all messages |
| 261 | to be processed in the root logger, or delegation to the parent in non-root |
| 262 | loggers). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 263 | \end{methoddesc} |
| 264 | |
| 265 | \begin{methoddesc}{isEnabledFor}{lvl} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 266 | Indicates if a message of severity \var{lvl} would be processed by |
| 267 | this logger. This method checks first the module-level level set by |
| 268 | \function{logging.disable(lvl)} and then the logger's effective level as |
| 269 | determined by \method{getEffectiveLevel()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 270 | \end{methoddesc} |
| 271 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 272 | \begin{methoddesc}{getEffectiveLevel}{} |
| 273 | Indicates the effective level for this logger. If a value other than |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 274 | \constant{NOTSET} has been set using \method{setLevel()}, it is returned. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 275 | Otherwise, the hierarchy is traversed towards the root until a value |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 276 | other than \constant{NOTSET} is found, and that value is returned. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 277 | \end{methoddesc} |
| 278 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 279 | \begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}} |
| 280 | Logs a message with level \constant{DEBUG} on this logger. |
| 281 | The \var{msg} is the message format string, and the \var{args} are the |
| 282 | arguments which are merged into \var{msg}. The only keyword argument in |
| 283 | \var{kwargs} which is inspected is \var{exc_info} which, if it does not |
| 284 | evaluate as false, causes exception information (via a call to |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 285 | \function{sys.exc_info()}) to be added to the logging message. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 286 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 287 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 288 | \begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}} |
| 289 | Logs a message with level \constant{INFO} on this logger. |
| 290 | The arguments are interpreted as for \method{debug()}. |
| 291 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 292 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 293 | \begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}} |
| 294 | Logs a message with level \constant{WARNING} on this logger. |
| 295 | The arguments are interpreted as for \method{debug()}. |
| 296 | \end{methoddesc} |
| 297 | |
| 298 | \begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}} |
| 299 | Logs a message with level \constant{ERROR} on this logger. |
| 300 | The arguments are interpreted as for \method{debug()}. |
| 301 | \end{methoddesc} |
| 302 | |
| 303 | \begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}} |
| 304 | Logs a message with level \constant{CRITICAL} on this logger. |
| 305 | The arguments are interpreted as for \method{debug()}. |
| 306 | \end{methoddesc} |
| 307 | |
| 308 | \begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}} |
| 309 | Logs a message with level \var{lvl} on this logger. |
| 310 | The other arguments are interpreted as for \method{debug()}. |
| 311 | \end{methoddesc} |
| 312 | |
| 313 | \begin{methoddesc}{exception}{msg\optional{, *args}} |
| 314 | Logs a message with level \constant{ERROR} on this logger. |
| 315 | The arguments are interpreted as for \method{debug()}. Exception info |
| 316 | is added to the logging message. This method should only be called |
| 317 | from an exception handler. |
| 318 | \end{methoddesc} |
| 319 | |
| 320 | \begin{methoddesc}{addFilter}{filt} |
| 321 | Adds the specified filter \var{filt} to this logger. |
| 322 | \end{methoddesc} |
| 323 | |
| 324 | \begin{methoddesc}{removeFilter}{filt} |
| 325 | Removes the specified filter \var{filt} from this logger. |
| 326 | \end{methoddesc} |
| 327 | |
| 328 | \begin{methoddesc}{filter}{record} |
| 329 | Applies this logger's filters to the record and returns a true value if |
| 330 | the record is to be processed. |
| 331 | \end{methoddesc} |
| 332 | |
| 333 | \begin{methoddesc}{addHandler}{hdlr} |
| 334 | Adds the specified handler \var{hdlr} to this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 335 | \end{methoddesc} |
| 336 | |
| 337 | \begin{methoddesc}{removeHandler}{hdlr} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 338 | Removes the specified handler \var{hdlr} from this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 339 | \end{methoddesc} |
| 340 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 341 | \begin{methoddesc}{findCaller}{} |
| 342 | Finds the caller's source filename and line number. Returns the filename |
| 343 | and line number as a 2-element tuple. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 344 | \end{methoddesc} |
| 345 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 346 | \begin{methoddesc}{handle}{record} |
| 347 | Handles a record by passing it to all handlers associated with this logger |
| 348 | and its ancestors (until a false value of \var{propagate} is found). |
| 349 | This method is used for unpickled records received from a socket, as well |
| 350 | as those created locally. Logger-level filtering is applied using |
| 351 | \method{filter()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 352 | \end{methoddesc} |
| 353 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 354 | \begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info} |
| 355 | This is a factory method which can be overridden in subclasses to create |
| 356 | specialized \class{LogRecord} instances. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 357 | \end{methoddesc} |
| 358 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 359 | \subsection{Handler Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 360 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 361 | Handlers have the following attributes and methods. Note that |
| 362 | \class{Handler} is never instantiated directly; this class acts as a |
| 363 | base for more useful subclasses. However, the \method{__init__()} |
| 364 | method in subclasses needs to call \method{Handler.__init__()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 365 | |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 366 | \begin{methoddesc}{__init__}{level=\constant{NOTSET}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 367 | Initializes the \class{Handler} instance by setting its level, setting |
| 368 | the list of filters to the empty list and creating a lock (using |
| 369 | \method{getLock()}) for serializing access to an I/O mechanism. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 370 | \end{methoddesc} |
| 371 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 372 | \begin{methoddesc}{createLock}{} |
| 373 | Initializes a thread lock which can be used to serialize access to |
| 374 | underlying I/O functionality which may not be threadsafe. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 375 | \end{methoddesc} |
| 376 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 377 | \begin{methoddesc}{acquire}{} |
| 378 | Acquires the thread lock created with \method{createLock()}. |
| 379 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 380 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 381 | \begin{methoddesc}{release}{} |
| 382 | Releases the thread lock acquired with \method{acquire()}. |
| 383 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 384 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 385 | \begin{methoddesc}{setLevel}{lvl} |
| 386 | Sets the threshold for this handler to \var{lvl}. Logging messages which are |
| 387 | less severe than \var{lvl} will be ignored. When a handler is created, the |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 388 | level is set to \constant{NOTSET} (which causes all messages to be processed). |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 389 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 390 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 391 | \begin{methoddesc}{setFormatter}{form} |
| 392 | Sets the \class{Formatter} for this handler to \var{form}. |
| 393 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 394 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 395 | \begin{methoddesc}{addFilter}{filt} |
| 396 | Adds the specified filter \var{filt} to this handler. |
| 397 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 398 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 399 | \begin{methoddesc}{removeFilter}{filt} |
| 400 | Removes the specified filter \var{filt} from this handler. |
| 401 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 402 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 403 | \begin{methoddesc}{filter}{record} |
| 404 | Applies this handler's filters to the record and returns a true value if |
| 405 | the record is to be processed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 406 | \end{methoddesc} |
| 407 | |
| 408 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 409 | Ensure all logging output has been flushed. This version does |
| 410 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 411 | \end{methoddesc} |
| 412 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 413 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 414 | Tidy up any resources used by the handler. This version does |
| 415 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 416 | \end{methoddesc} |
| 417 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 418 | \begin{methoddesc}{handle}{record} |
| 419 | Conditionally emits the specified logging record, depending on |
| 420 | filters which may have been added to the handler. Wraps the actual |
| 421 | emission of the record with acquisition/release of the I/O thread |
| 422 | lock. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 423 | \end{methoddesc} |
| 424 | |
| 425 | \begin{methoddesc}{handleError}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 426 | This method should be called from handlers when an exception is |
| 427 | encountered during an emit() call. By default it does nothing, |
| 428 | which means that exceptions get silently ignored. This is what is |
| 429 | mostly wanted for a logging system - most users will not care |
| 430 | about errors in the logging system, they are more interested in |
| 431 | application errors. You could, however, replace this with a custom |
| 432 | handler if you wish. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 433 | \end{methoddesc} |
| 434 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 435 | \begin{methoddesc}{format}{record} |
| 436 | Do formatting for a record - if a formatter is set, use it. |
| 437 | Otherwise, use the default formatter for the module. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 438 | \end{methoddesc} |
| 439 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 440 | \begin{methoddesc}{emit}{record} |
| 441 | Do whatever it takes to actually log the specified logging record. |
| 442 | This version is intended to be implemented by subclasses and so |
| 443 | raises a \exception{NotImplementedError}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 444 | \end{methoddesc} |
| 445 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 446 | \subsubsection{StreamHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 447 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 448 | The \class{StreamHandler} class sends logging output to streams such as |
| 449 | \var{sys.stdout}, \var{sys.stderr} or any file-like object (or, more |
| 450 | precisely, any object which supports \method{write()} and \method{flush()} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 451 | methods). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 452 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 453 | \begin{classdesc}{StreamHandler}{\optional{strm}} |
| 454 | Returns a new instance of the \class{StreamHandler} class. If \var{strm} is |
| 455 | specified, the instance will use it for logging output; otherwise, |
| 456 | \var{sys.stderr} will be used. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 457 | \end{classdesc} |
| 458 | |
| 459 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 460 | If a formatter is specified, it is used to format the record. |
| 461 | The record is then written to the stream with a trailing newline. |
| 462 | If exception information is present, it is formatted using |
| 463 | \function{traceback.print_exception()} and appended to the stream. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 464 | \end{methoddesc} |
| 465 | |
| 466 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 467 | Flushes the stream by calling its \method{flush()} method. Note that |
| 468 | the \method{close()} method is inherited from \class{Handler} and |
| 469 | so does nothing, so an explicit \method{flush()} call may be needed |
| 470 | at times. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 471 | \end{methoddesc} |
| 472 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 473 | \subsubsection{FileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 474 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 475 | The \class{FileHandler} class sends logging output to a disk file. |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 476 | It inherits the output functionality from \class{StreamHandler}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 477 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 478 | \begin{classdesc}{FileHandler}{filename\optional{, mode}} |
| 479 | Returns a new instance of the \class{FileHandler} class. The specified |
| 480 | file is opened and used as the stream for logging. If \var{mode} is |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 481 | not specified, \constant{'a'} is used. By default, the file grows |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 482 | indefinitely. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 483 | \end{classdesc} |
| 484 | |
| 485 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 486 | Closes the file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 487 | \end{methoddesc} |
| 488 | |
| 489 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 490 | Outputs the record to the file. |
| 491 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 492 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 493 | \subsubsection{RotatingFileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 494 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 495 | The \class{RotatingFileHandler} class supports rotation of disk log files. |
| 496 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 497 | \begin{classdesc}{RotatingFileHandler}{filename\optional{, mode\optional{, |
| 498 | maxBytes\optional{, backupCount}}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 499 | Returns a new instance of the \class{RotatingFileHandler} class. The |
| 500 | specified file is opened and used as the stream for logging. If |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 501 | \var{mode} is not specified, \code{'a'} is used. By default, the |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 502 | file grows indefinitely. You can use the \var{maxBytes} and |
| 503 | \var{backupCount} values to allow the file to \dfn{rollover} at a |
| 504 | predetermined size. When the size is about to be exceeded, the file is |
| 505 | closed and a new file opened for output, transparently to the |
| 506 | caller. Rollover occurs whenever the current log file is nearly |
| 507 | \var{maxBytes} in length. If \var{backupCount} is >= 1, the system |
| 508 | will successively create new files with the same pathname as the base |
| 509 | file, but with extensions ".1", ".2" etc. appended to it. For example, |
| 510 | with a backupCount of 5 and a base file name of "app.log", you would |
| 511 | get "app.log", "app.log.1", "app.log.2", ... through to |
| 512 | "app.log.5". When the last file reaches its size limit, the logging |
| 513 | reverts to "app.log" which is truncated to zero length. If |
| 514 | \var{maxBytes} is zero, rollover never occurs. |
| 515 | \end{classdesc} |
| 516 | |
| 517 | \begin{methoddesc}{doRollover}{} |
| 518 | Does a rollover, as described above. |
| 519 | \end{methoddesc} |
| 520 | |
| 521 | \begin{methoddesc}{emit}{record} |
| 522 | Outputs the record to the file, catering for rollover as described |
| 523 | in \method{setRollover()}. |
| 524 | \end{methoddesc} |
| 525 | |
| 526 | \subsubsection{SocketHandler} |
| 527 | |
| 528 | The \class{SocketHandler} class sends logging output to a network |
| 529 | socket. The base class uses a TCP socket. |
| 530 | |
| 531 | \begin{classdesc}{SocketHandler}{host, port} |
| 532 | Returns a new instance of the \class{SocketHandler} class intended to |
| 533 | communicate with a remote machine whose address is given by \var{host} |
| 534 | and \var{port}. |
| 535 | \end{classdesc} |
| 536 | |
| 537 | \begin{methoddesc}{close}{} |
| 538 | Closes the socket. |
| 539 | \end{methoddesc} |
| 540 | |
| 541 | \begin{methoddesc}{handleError}{} |
| 542 | \end{methoddesc} |
| 543 | |
| 544 | \begin{methoddesc}{emit}{} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 545 | Pickles the record's attribute dictionary and writes it to the socket in |
| 546 | binary format. If there is an error with the socket, silently drops the |
| 547 | packet. If the connection was previously lost, re-establishes the connection. |
| 548 | To unpickle the record at the receiving end into a LogRecord, use the |
| 549 | \function{makeLogRecord} function. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 550 | \end{methoddesc} |
| 551 | |
| 552 | \begin{methoddesc}{handleError}{} |
| 553 | Handles an error which has occurred during \method{emit()}. The |
| 554 | most likely cause is a lost connection. Closes the socket so that |
| 555 | we can retry on the next event. |
| 556 | \end{methoddesc} |
| 557 | |
| 558 | \begin{methoddesc}{makeSocket}{} |
| 559 | This is a factory method which allows subclasses to define the precise |
| 560 | type of socket they want. The default implementation creates a TCP |
| 561 | socket (\constant{socket.SOCK_STREAM}). |
| 562 | \end{methoddesc} |
| 563 | |
| 564 | \begin{methoddesc}{makePickle}{record} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 565 | Pickles the record's attribute dictionary in binary format with a length |
| 566 | prefix, and returns it ready for transmission across the socket. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 567 | \end{methoddesc} |
| 568 | |
| 569 | \begin{methoddesc}{send}{packet} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 570 | Send a pickled string \var{packet} to the socket. This function allows |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 571 | for partial sends which can happen when the network is busy. |
| 572 | \end{methoddesc} |
| 573 | |
| 574 | \subsubsection{DatagramHandler} |
| 575 | |
| 576 | The \class{DatagramHandler} class inherits from \class{SocketHandler} |
| 577 | to support sending logging messages over UDP sockets. |
| 578 | |
| 579 | \begin{classdesc}{DatagramHandler}{host, port} |
| 580 | Returns a new instance of the \class{DatagramHandler} class intended to |
| 581 | communicate with a remote machine whose address is given by \var{host} |
| 582 | and \var{port}. |
| 583 | \end{classdesc} |
| 584 | |
| 585 | \begin{methoddesc}{emit}{} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 586 | Pickles the record's attribute dictionary and writes it to the socket in |
| 587 | binary format. If there is an error with the socket, silently drops the |
| 588 | packet. |
| 589 | To unpickle the record at the receiving end into a LogRecord, use the |
| 590 | \function{makeLogRecord} function. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 591 | \end{methoddesc} |
| 592 | |
| 593 | \begin{methoddesc}{makeSocket}{} |
| 594 | The factory method of \class{SocketHandler} is here overridden to create |
| 595 | a UDP socket (\constant{socket.SOCK_DGRAM}). |
| 596 | \end{methoddesc} |
| 597 | |
| 598 | \begin{methoddesc}{send}{s} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 599 | Send a pickled string to a socket. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 600 | \end{methoddesc} |
| 601 | |
| 602 | \subsubsection{SysLogHandler} |
| 603 | |
| 604 | The \class{SysLogHandler} class supports sending logging messages to a |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 605 | remote or local \UNIX{} syslog. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 606 | |
| 607 | \begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}} |
| 608 | Returns a new instance of the \class{SysLogHandler} class intended to |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 609 | communicate with a remote \UNIX{} machine whose address is given by |
| 610 | \var{address} in the form of a \code{(\var{host}, \var{port})} |
| 611 | tuple. If \var{address} is not specified, \code{('localhost', 514)} is |
| 612 | used. The address is used to open a UDP socket. If \var{facility} is |
| 613 | not specified, \constant{LOG_USER} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 614 | \end{classdesc} |
| 615 | |
| 616 | \begin{methoddesc}{close}{} |
| 617 | Closes the socket to the remote host. |
| 618 | \end{methoddesc} |
| 619 | |
| 620 | \begin{methoddesc}{emit}{record} |
| 621 | The record is formatted, and then sent to the syslog server. If |
| 622 | exception information is present, it is \emph{not} sent to the server. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 623 | \end{methoddesc} |
| 624 | |
| 625 | \begin{methoddesc}{encodePriority}{facility, priority} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 626 | Encodes the facility and priority into an integer. You can pass in strings |
| 627 | or integers - if strings are passed, internal mapping dictionaries are used |
| 628 | to convert them to integers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 629 | \end{methoddesc} |
| 630 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 631 | \subsubsection{NTEventLogHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 632 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 633 | The \class{NTEventLogHandler} class supports sending logging messages |
| 634 | to a local Windows NT, Windows 2000 or Windows XP event log. Before |
| 635 | you can use it, you need Mark Hammond's Win32 extensions for Python |
| 636 | installed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 637 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 638 | \begin{classdesc}{NTEventLogHandler}{appname\optional{, |
| 639 | dllname\optional{, logtype}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 640 | Returns a new instance of the \class{NTEventLogHandler} class. The |
| 641 | \var{appname} is used to define the application name as it appears in the |
| 642 | event log. An appropriate registry entry is created using this name. |
| 643 | The \var{dllname} should give the fully qualified pathname of a .dll or .exe |
| 644 | which contains message definitions to hold in the log (if not specified, |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 645 | \code{'win32service.pyd'} is used - this is installed with the Win32 |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 646 | extensions and contains some basic placeholder message definitions. |
| 647 | Note that use of these placeholders will make your event logs big, as the |
| 648 | entire message source is held in the log. If you want slimmer logs, you have |
| 649 | to pass in the name of your own .dll or .exe which contains the message |
| 650 | definitions you want to use in the event log). The \var{logtype} is one of |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 651 | \code{'Application'}, \code{'System'} or \code{'Security'}, and |
| 652 | defaults to \code{'Application'}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 653 | \end{classdesc} |
| 654 | |
| 655 | \begin{methoddesc}{close}{} |
| 656 | At this point, you can remove the application name from the registry as a |
| 657 | source of event log entries. However, if you do this, you will not be able |
| 658 | to see the events as you intended in the Event Log Viewer - it needs to be |
| 659 | able to access the registry to get the .dll name. The current version does |
| 660 | not do this (in fact it doesn't do anything). |
| 661 | \end{methoddesc} |
| 662 | |
| 663 | \begin{methoddesc}{emit}{record} |
| 664 | Determines the message ID, event category and event type, and then logs the |
| 665 | message in the NT event log. |
| 666 | \end{methoddesc} |
| 667 | |
| 668 | \begin{methoddesc}{getEventCategory}{record} |
| 669 | Returns the event category for the record. Override this if you |
| 670 | want to specify your own categories. This version returns 0. |
| 671 | \end{methoddesc} |
| 672 | |
| 673 | \begin{methoddesc}{getEventType}{record} |
| 674 | Returns the event type for the record. Override this if you want |
| 675 | to specify your own types. This version does a mapping using the |
| 676 | handler's typemap attribute, which is set up in \method{__init__()} |
| 677 | to a dictionary which contains mappings for \constant{DEBUG}, |
| 678 | \constant{INFO}, \constant{WARNING}, \constant{ERROR} and |
| 679 | \constant{CRITICAL}. If you are using your own levels, you will either need |
| 680 | to override this method or place a suitable dictionary in the |
| 681 | handler's \var{typemap} attribute. |
| 682 | \end{methoddesc} |
| 683 | |
| 684 | \begin{methoddesc}{getMessageID}{record} |
| 685 | Returns the message ID for the record. If you are using your |
| 686 | own messages, you could do this by having the \var{msg} passed to the |
| 687 | logger being an ID rather than a format string. Then, in here, |
| 688 | you could use a dictionary lookup to get the message ID. This |
| 689 | version returns 1, which is the base message ID in |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 690 | \file{win32service.pyd}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 691 | \end{methoddesc} |
| 692 | |
| 693 | \subsubsection{SMTPHandler} |
| 694 | |
| 695 | The \class{SMTPHandler} class supports sending logging messages to an email |
| 696 | address via SMTP. |
| 697 | |
| 698 | \begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject} |
| 699 | Returns a new instance of the \class{SMTPHandler} class. The |
| 700 | instance is initialized with the from and to addresses and subject |
| 701 | line of the email. The \var{toaddrs} should be a list of strings without |
| 702 | domain names (That's what the \var{mailhost} is for). To specify a |
| 703 | non-standard SMTP port, use the (host, port) tuple format for the |
| 704 | \var{mailhost} argument. If you use a string, the standard SMTP port |
| 705 | is used. |
| 706 | \end{classdesc} |
| 707 | |
| 708 | \begin{methoddesc}{emit}{record} |
| 709 | Formats the record and sends it to the specified addressees. |
| 710 | \end{methoddesc} |
| 711 | |
| 712 | \begin{methoddesc}{getSubject}{record} |
| 713 | If you want to specify a subject line which is record-dependent, |
| 714 | override this method. |
| 715 | \end{methoddesc} |
| 716 | |
| 717 | \subsubsection{MemoryHandler} |
| 718 | |
| 719 | The \class{MemoryHandler} supports buffering of logging records in memory, |
| 720 | periodically flushing them to a \dfn{target} handler. Flushing occurs |
| 721 | whenever the buffer is full, or when an event of a certain severity or |
| 722 | greater is seen. |
| 723 | |
| 724 | \class{MemoryHandler} is a subclass of the more general |
| 725 | \class{BufferingHandler}, which is an abstract class. This buffers logging |
| 726 | records in memory. Whenever each record is added to the buffer, a |
| 727 | check is made by calling \method{shouldFlush()} to see if the buffer |
| 728 | should be flushed. If it should, then \method{flush()} is expected to |
| 729 | do the needful. |
| 730 | |
| 731 | \begin{classdesc}{BufferingHandler}{capacity} |
| 732 | Initializes the handler with a buffer of the specified capacity. |
| 733 | \end{classdesc} |
| 734 | |
| 735 | \begin{methoddesc}{emit}{record} |
| 736 | Appends the record to the buffer. If \method{shouldFlush()} returns true, |
| 737 | calls \method{flush()} to process the buffer. |
| 738 | \end{methoddesc} |
| 739 | |
| 740 | \begin{methoddesc}{flush}{} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 741 | You can override this to implement custom flushing behavior. This version |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 742 | just zaps the buffer to empty. |
| 743 | \end{methoddesc} |
| 744 | |
| 745 | \begin{methoddesc}{shouldFlush}{record} |
| 746 | Returns true if the buffer is up to capacity. This method can be |
| 747 | overridden to implement custom flushing strategies. |
| 748 | \end{methoddesc} |
| 749 | |
| 750 | \begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 751 | \optional{, target}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 752 | Returns a new instance of the \class{MemoryHandler} class. The |
| 753 | instance is initialized with a buffer size of \var{capacity}. If |
| 754 | \var{flushLevel} is not specified, \constant{ERROR} is used. If no |
| 755 | \var{target} is specified, the target will need to be set using |
| 756 | \method{setTarget()} before this handler does anything useful. |
| 757 | \end{classdesc} |
| 758 | |
| 759 | \begin{methoddesc}{close}{} |
| 760 | Calls \method{flush()}, sets the target to \constant{None} and |
| 761 | clears the buffer. |
| 762 | \end{methoddesc} |
| 763 | |
| 764 | \begin{methoddesc}{flush}{} |
| 765 | For a \class{MemoryHandler}, flushing means just sending the buffered |
| 766 | records to the target, if there is one. Override if you want |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 767 | different behavior. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 768 | \end{methoddesc} |
| 769 | |
| 770 | \begin{methoddesc}{setTarget}{target} |
| 771 | Sets the target handler for this handler. |
| 772 | \end{methoddesc} |
| 773 | |
| 774 | \begin{methoddesc}{shouldFlush}{record} |
| 775 | Checks for buffer full or a record at the \var{flushLevel} or higher. |
| 776 | \end{methoddesc} |
| 777 | |
| 778 | \subsubsection{HTTPHandler} |
| 779 | |
| 780 | The \class{HTTPHandler} class supports sending logging messages to a |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 781 | Web server, using either \samp{GET} or \samp{POST} semantics. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 782 | |
| 783 | \begin{classdesc}{HTTPHandler}{host, url\optional{, method}} |
| 784 | Returns a new instance of the \class{HTTPHandler} class. The |
| 785 | instance is initialized with a host address, url and HTTP method. |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 786 | If no \var{method} is specified, \samp{GET} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 787 | \end{classdesc} |
| 788 | |
| 789 | \begin{methoddesc}{emit}{record} |
| 790 | Sends the record to the Web server as an URL-encoded dictionary. |
| 791 | \end{methoddesc} |
| 792 | |
| 793 | \subsection{Formatter Objects} |
| 794 | |
| 795 | \class{Formatter}s have the following attributes and methods. They are |
| 796 | responsible for converting a \class{LogRecord} to (usually) a string |
| 797 | which can be interpreted by either a human or an external system. The |
| 798 | base |
| 799 | \class{Formatter} allows a formatting string to be specified. If none is |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 800 | supplied, the default value of \code{'\%(message)s\e'} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 801 | |
| 802 | A Formatter can be initialized with a format string which makes use of |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 803 | knowledge of the \class{LogRecord} attributes - such as the default value |
| 804 | mentioned above making use of the fact that the user's message and |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 805 | arguments are pre-formatted into a LogRecord's \var{message} |
| 806 | attribute. This format string contains standard python \%-style |
| 807 | mapping keys. See section \ref{typesseq-strings}, ``String Formatting |
| 808 | Operations,'' for more information on string formatting. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 809 | |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 810 | Currently, the useful mapping keys in a LogRecord are: |
| 811 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 812 | \begin{tableii}{l|l}{code}{Format}{Description} |
| 813 | \lineii{\%(name)s} {Name of the logger (logging channel).} |
| 814 | \lineii{\%(levelno)s} {Numeric logging level for the message |
| 815 | (\constant{DEBUG}, \constant{INFO}, |
| 816 | \constant{WARNING}, \constant{ERROR}, |
| 817 | \constant{CRITICAL}).} |
| 818 | \lineii{\%(levelname)s}{Text logging level for the message |
| 819 | (\code{'DEBUG'}, \code{'INFO'}, |
| 820 | \code{'WARNING'}, \code{'ERROR'}, |
| 821 | \code{'CRITICAL'}).} |
| 822 | \lineii{\%(pathname)s} {Full pathname of the source file where the logging |
| 823 | call was issued (if available).} |
| 824 | \lineii{\%(filename)s} {Filename portion of pathname.} |
| 825 | \lineii{\%(module)s} {Module (name portion of filename).} |
| 826 | \lineii{\%(lineno)d} {Source line number where the logging call was issued |
| 827 | (if available).} |
| 828 | \lineii{\%(created)f} {Time when the LogRecord was created (as |
| 829 | returned by \function{time.time()}).} |
| 830 | \lineii{\%(asctime)s} {Human-readable time when the LogRecord was created. |
| 831 | By default this is of the form |
| 832 | ``2003-07-08 16:49:45,896'' (the numbers after the |
| 833 | comma are millisecond portion of the time).} |
| 834 | \lineii{\%(msecs)d} {Millisecond portion of the time when the |
| 835 | \class{LogRecord} was created.} |
| 836 | \lineii{\%(thread)d} {Thread ID (if available).} |
| 837 | \lineii{\%(process)d} {Process ID (if available).} |
| 838 | \lineii{\%(message)s} {The logged message, computed as \code{msg \% args}.} |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 839 | \end{tableii} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 840 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 841 | \begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}} |
| 842 | Returns a new instance of the \class{Formatter} class. The |
| 843 | instance is initialized with a format string for the message as a whole, |
| 844 | as well as a format string for the date/time portion of a message. If |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 845 | no \var{fmt} is specified, \code{\%(message)s'} is used. If no \var{datefmt} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 846 | is specified, the ISO8601 date format is used. |
| 847 | \end{classdesc} |
| 848 | |
| 849 | \begin{methoddesc}{format}{record} |
| 850 | The record's attribute dictionary is used as the operand to a |
| 851 | string formatting operation. Returns the resulting string. |
| 852 | Before formatting the dictionary, a couple of preparatory steps |
| 853 | are carried out. The \var{message} attribute of the record is computed |
| 854 | using \var{msg} \% \var{args}. If the formatting string contains |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 855 | \code{'(asctime)'}, \method{formatTime()} is called to format the |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 856 | event time. If there is exception information, it is formatted using |
| 857 | \method{formatException()} and appended to the message. |
| 858 | \end{methoddesc} |
| 859 | |
| 860 | \begin{methoddesc}{formatTime}{record\optional{, datefmt}} |
| 861 | This method should be called from \method{format()} by a formatter which |
| 862 | wants to make use of a formatted time. This method can be overridden |
| 863 | in formatters to provide for any specific requirement, but the |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 864 | basic behavior is as follows: if \var{datefmt} (a string) is specified, |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 865 | it is used with \function{time.strftime()} to format the creation time of the |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 866 | record. Otherwise, the ISO8601 format is used. The resulting |
| 867 | string is returned. |
| 868 | \end{methoddesc} |
| 869 | |
| 870 | \begin{methoddesc}{formatException}{exc_info} |
| 871 | Formats the specified exception information (a standard exception tuple |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 872 | as returned by \function{sys.exc_info()}) as a string. This default |
| 873 | implementation just uses \function{traceback.print_exception()}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 874 | The resulting string is returned. |
| 875 | \end{methoddesc} |
| 876 | |
| 877 | \subsection{Filter Objects} |
| 878 | |
| 879 | \class{Filter}s can be used by \class{Handler}s and \class{Logger}s for |
| 880 | more sophisticated filtering than is provided by levels. The base filter |
| 881 | class only allows events which are below a certain point in the logger |
| 882 | hierarchy. For example, a filter initialized with "A.B" will allow events |
| 883 | logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB", |
| 884 | "B.A.B" etc. If initialized with the empty string, all events are passed. |
| 885 | |
| 886 | \begin{classdesc}{Filter}{\optional{name}} |
| 887 | Returns an instance of the \class{Filter} class. If \var{name} is specified, |
| 888 | it names a logger which, together with its children, will have its events |
| 889 | allowed through the filter. If no name is specified, allows every event. |
| 890 | \end{classdesc} |
| 891 | |
| 892 | \begin{methoddesc}{filter}{record} |
| 893 | Is the specified record to be logged? Returns zero for no, nonzero for |
| 894 | yes. If deemed appropriate, the record may be modified in-place by this |
| 895 | method. |
| 896 | \end{methoddesc} |
| 897 | |
| 898 | \subsection{LogRecord Objects} |
| 899 | |
| 900 | LogRecord instances are created every time something is logged. They |
| 901 | contain all the information pertinent to the event being logged. The |
| 902 | main information passed in is in msg and args, which are combined |
| 903 | using msg \% args to create the message field of the record. The record |
| 904 | also includes information such as when the record was created, the |
| 905 | source line where the logging call was made, and any exception |
| 906 | information to be logged. |
| 907 | |
| 908 | LogRecord has no methods; it's just a repository for information about the |
| 909 | logging event. The only reason it's a class rather than a dictionary is to |
| 910 | facilitate extension. |
| 911 | |
| 912 | \begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args, |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 913 | exc_info} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 914 | Returns an instance of \class{LogRecord} initialized with interesting |
| 915 | information. The \var{name} is the logger name; \var{lvl} is the |
| 916 | numeric level; \var{pathname} is the absolute pathname of the source |
| 917 | file in which the logging call was made; \var{lineno} is the line |
| 918 | number in that file where the logging call is found; \var{msg} is the |
| 919 | user-supplied message (a format string); \var{args} is the tuple |
| 920 | which, together with \var{msg}, makes up the user message; and |
| 921 | \var{exc_info} is the exception tuple obtained by calling |
| 922 | \function{sys.exc_info() }(or \constant{None}, if no exception information |
| 923 | is available). |
| 924 | \end{classdesc} |
| 925 | |
| 926 | \subsection{Thread Safety} |
| 927 | |
| 928 | The logging module is intended to be thread-safe without any special work |
| 929 | needing to be done by its clients. It achieves this though using threading |
| 930 | locks; there is one lock to serialize access to the module's shared data, |
| 931 | and each handler also creates a lock to serialize access to its underlying |
| 932 | I/O. |
| 933 | |
| 934 | \subsection{Configuration} |
| 935 | |
| 936 | |
| 937 | \subsubsection{Configuration functions} |
| 938 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame^] | 939 | The following functions allow the logging module to be |
| 940 | configured. Before they can be used, you must import |
| 941 | \module{logging.config}. Their use is optional --- you can configure |
| 942 | the logging module entirely by making calls to the main API (defined |
| 943 | in \module{logging} itself) and defining handlers which are declared |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 944 | either in \module{logging} or \module{logging.handlers}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 945 | |
| 946 | \begin{funcdesc}{fileConfig}{fname\optional{, defaults}} |
| 947 | Reads the logging configuration from a ConfigParser-format file named |
| 948 | \var{fname}. This function can be called several times from an application, |
| 949 | allowing an end user the ability to select from various pre-canned |
| 950 | configurations (if the developer provides a mechanism to present the |
| 951 | choices and load the chosen configuration). Defaults to be passed to |
| 952 | ConfigParser can be specified in the \var{defaults} argument. |
| 953 | \end{funcdesc} |
| 954 | |
| 955 | \begin{funcdesc}{listen}{\optional{port}} |
| 956 | Starts up a socket server on the specified port, and listens for new |
| 957 | configurations. If no port is specified, the module's default |
| 958 | \constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations |
| 959 | will be sent as a file suitable for processing by \function{fileConfig()}. |
| 960 | Returns a \class{Thread} instance on which you can call \method{start()} |
| 961 | to start the server, and which you can \method{join()} when appropriate. |
| 962 | To stop the server, call \function{stopListening()}. |
| 963 | \end{funcdesc} |
| 964 | |
| 965 | \begin{funcdesc}{stopListening}{} |
| 966 | Stops the listening server which was created with a call to |
| 967 | \function{listen()}. This is typically called before calling \method{join()} |
| 968 | on the return value from \function{listen()}. |
| 969 | \end{funcdesc} |
| 970 | |
| 971 | \subsubsection{Configuration file format} |
| 972 | |
| 973 | The configuration file format understood by \function{fileConfig} is |
| 974 | based on ConfigParser functionality. The file must contain sections |
| 975 | called \code{[loggers]}, \code{[handlers]} and \code{[formatters]} |
| 976 | which identify by name the entities of each type which are defined in |
| 977 | the file. For each such entity, there is a separate section which |
| 978 | identified how that entity is configured. Thus, for a logger named |
| 979 | \code{log01} in the \code{[loggers]} section, the relevant |
| 980 | configuration details are held in a section |
| 981 | \code{[logger_log01]}. Similarly, a handler called \code{hand01} in |
| 982 | the \code{[handlers]} section will have its configuration held in a |
| 983 | section called \code{[handler_hand01]}, while a formatter called |
| 984 | \code{form01} in the \code{[formatters]} section will have its |
| 985 | configuration specified in a section called |
| 986 | \code{[formatter_form01]}. The root logger configuration must be |
| 987 | specified in a section called \code{[logger_root]}. |
| 988 | |
| 989 | Examples of these sections in the file are given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 990 | |
| 991 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 992 | [loggers] |
| 993 | keys=root,log02,log03,log04,log05,log06,log07 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 994 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 995 | [handlers] |
| 996 | keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09 |
| 997 | |
| 998 | [formatters] |
| 999 | keys=form01,form02,form03,form04,form05,form06,form07,form08,form09 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1000 | \end{verbatim} |
| 1001 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1002 | The root logger must specify a level and a list of handlers. An |
| 1003 | example of a root logger section is given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1004 | |
| 1005 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1006 | [logger_root] |
| 1007 | level=NOTSET |
| 1008 | handlers=hand01 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1009 | \end{verbatim} |
| 1010 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1011 | The \code{level} entry can be one of \code{DEBUG, INFO, WARNING, |
| 1012 | ERROR, CRITICAL} or \code{NOTSET}. For the root logger only, |
| 1013 | \code{NOTSET} means that all messages will be logged. Level values are |
| 1014 | \function{eval()}uated in the context of the \code{logging} package's |
| 1015 | namespace. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1016 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1017 | The \code{handlers} entry is a comma-separated list of handler names, |
| 1018 | which must appear in the \code{[handlers]} section. These names must |
| 1019 | appear in the \code{[handlers]} section and have corresponding |
| 1020 | sections in the configuration file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1021 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1022 | For loggers other than the root logger, some additional information is |
| 1023 | required. This is illustrated by the following example. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1024 | |
| 1025 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1026 | [logger_parser] |
| 1027 | level=DEBUG |
| 1028 | handlers=hand01 |
| 1029 | propagate=1 |
| 1030 | qualname=compiler.parser |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1031 | \end{verbatim} |
| 1032 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1033 | The \code{level} and \code{handlers} entries are interpreted as for |
| 1034 | the root logger, except that if a non-root logger's level is specified |
| 1035 | as \code{NOTSET}, the system consults loggers higher up the hierarchy |
| 1036 | to determine the effective level of the logger. The \code{propagate} |
| 1037 | entry is set to 1 to indicate that messages must propagate to handlers |
| 1038 | higher up the logger hierarchy from this logger, or 0 to indicate that |
| 1039 | messages are \strong{not} propagated to handlers up the hierarchy. The |
| 1040 | \code{qualname} entry is the hierarchical channel name of the logger, |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 1041 | for example, the name used by the application to get the logger. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1042 | |
| 1043 | Sections which specify handler configuration are exemplified by the |
| 1044 | following. |
| 1045 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1046 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1047 | [handler_hand01] |
| 1048 | class=StreamHandler |
| 1049 | level=NOTSET |
| 1050 | formatter=form01 |
| 1051 | args=(sys.stdout,) |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1052 | \end{verbatim} |
| 1053 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1054 | The \code{class} entry indicates the handler's class (as determined by |
| 1055 | \function{eval()} in the \code{logging} package's namespace). The |
| 1056 | \code{level} is interpreted as for loggers, and \code{NOTSET} is taken |
| 1057 | to mean "log everything". |
| 1058 | |
| 1059 | The \code{formatter} entry indicates the key name of the formatter for |
| 1060 | this handler. If blank, a default formatter |
| 1061 | (\code{logging._defaultFormatter}) is used. If a name is specified, it |
| 1062 | must appear in the \code{[formatters]} section and have a |
| 1063 | corresponding section in the configuration file. |
| 1064 | |
| 1065 | The \code{args} entry, when \function{eval()}uated in the context of |
| 1066 | the \code{logging} package's namespace, is the list of arguments to |
| 1067 | the constructor for the handler class. Refer to the constructors for |
| 1068 | the relevant handlers, or to the examples below, to see how typical |
| 1069 | entries are constructed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1070 | |
| 1071 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1072 | [handler_hand02] |
| 1073 | class=FileHandler |
| 1074 | level=DEBUG |
| 1075 | formatter=form02 |
| 1076 | args=('python.log', 'w') |
| 1077 | |
| 1078 | [handler_hand03] |
| 1079 | class=handlers.SocketHandler |
| 1080 | level=INFO |
| 1081 | formatter=form03 |
| 1082 | args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT) |
| 1083 | |
| 1084 | [handler_hand04] |
| 1085 | class=handlers.DatagramHandler |
| 1086 | level=WARN |
| 1087 | formatter=form04 |
| 1088 | args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT) |
| 1089 | |
| 1090 | [handler_hand05] |
| 1091 | class=handlers.SysLogHandler |
| 1092 | level=ERROR |
| 1093 | formatter=form05 |
| 1094 | args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER) |
| 1095 | |
| 1096 | [handler_hand06] |
| 1097 | class=NTEventLogHandler |
| 1098 | level=CRITICAL |
| 1099 | formatter=form06 |
| 1100 | args=('Python Application', '', 'Application') |
| 1101 | |
| 1102 | [handler_hand07] |
| 1103 | class=SMTPHandler |
| 1104 | level=WARN |
| 1105 | formatter=form07 |
| 1106 | args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject') |
| 1107 | |
| 1108 | [handler_hand08] |
| 1109 | class=MemoryHandler |
| 1110 | level=NOTSET |
| 1111 | formatter=form08 |
| 1112 | target= |
| 1113 | args=(10, ERROR) |
| 1114 | |
| 1115 | [handler_hand09] |
| 1116 | class=HTTPHandler |
| 1117 | level=NOTSET |
| 1118 | formatter=form09 |
| 1119 | args=('localhost:9022', '/log', 'GET') |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1120 | \end{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1121 | |
| 1122 | Sections which specify formatter configuration are typified by the following. |
| 1123 | |
| 1124 | \begin{verbatim} |
| 1125 | [formatter_form01] |
| 1126 | format=F1 %(asctime)s %(levelname)s %(message)s |
| 1127 | datefmt= |
| 1128 | \end{verbatim} |
| 1129 | |
| 1130 | The \code{format} entry is the overall format string, and the |
| 1131 | \code{datefmt} entry is the \function{strftime()}-compatible date/time format |
| 1132 | string. If empty, the package substitutes ISO8601 format date/times, which |
| 1133 | is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S". |
| 1134 | The ISO8601 format also specifies milliseconds, which are appended to the |
| 1135 | result of using the above format string, with a comma separator. An example |
| 1136 | time in ISO8601 format is \code{2003-01-23 00:29:50,411}. |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 1137 | |
| 1138 | \subsection{Using the logging package} |
| 1139 | |
| 1140 | \subsubsection{Basic example - log to a file} |
| 1141 | |
| 1142 | Here's a simple logging example that just logs to a file. In order, |
| 1143 | it creates a \class{Logger} instance, then a \class{FileHandler} |
| 1144 | and a \class{Formatter}. It attaches the \class{Formatter} to the |
| 1145 | \class{FileHandler}, then the \class{FileHandler} to the \class{Logger}. |
| 1146 | Finally, it sets a debug level for the logger. |
| 1147 | |
| 1148 | \begin{verbatim} |
| 1149 | import logging |
| 1150 | logger = logging.getLogger('myapp') |
| 1151 | hdlr = logging.FileHandler('/var/tmp/myapp.log') |
| 1152 | formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
| 1153 | hdlr.setFormatter(formatter) |
| 1154 | logger.addHandler(hdlr) |
| 1155 | logger.setLevel(logging.WARNING) |
| 1156 | \end{verbatim} |
| 1157 | |
| 1158 | We can use this logger object now to write entries to the log file: |
| 1159 | |
| 1160 | \begin{verbatim} |
| 1161 | logger.error('We have a problem') |
| 1162 | logger.info('While this is just chatty') |
| 1163 | \end{verbatim} |
| 1164 | |
| 1165 | If we look in the file that was created, we'll see something like this: |
| 1166 | \begin{verbatim} |
| 1167 | 2003-07-08 16:49:45,896 ERROR We have a problem |
| 1168 | \end{verbatim} |
| 1169 | |
| 1170 | The info message was not written to the file - we called the \method{setLevel} |
| 1171 | method to say we only wanted \code{WARNING} or worse, so the info message is |
| 1172 | discarded. |
| 1173 | |
| 1174 | The timestamp is of the form |
| 1175 | ``year-month-day hour:minutes:seconds,milliseconds.'' |
| 1176 | Note that despite the three digits of precision in the milliseconds field, |
| 1177 | not all systems provide time with this much precision. |
| 1178 | |