Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1 | \section{\module{logging} --- |
| 2 | Logging facility for Python} |
| 3 | |
| 4 | \declaremodule{standard}{logging} % standard library, in Python |
| 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 | |
| 11 | \modulesynopsis{Logging module for Python based on PEP 282.} |
| 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 |
| 32 | \method{debug}, \method{info}, \method{warning}, \method{error} and |
| 33 | \method{critical}, which mirrors 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 |
| 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 |
| 55 | that message (e.g. end users, support desk staff, system administrators, |
| 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, |
| 60 | \emph{all handlers associated with all ancestors of the logger} are called |
| 61 | upon 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. |
| 65 | If a handler decides to actually dispatch an event, the \method{emit} method |
| 66 | is used to send the message to its destination. Most user-defined subclasses |
| 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 |
| 93 | Unix syslog, possibly on a remote machine. |
| 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 |
| 103 | HTTP server using either GET or POST semantics. |
| 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 |
| 123 | \class{Handler} instances (through their \method{addFilter} method). |
| 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 |
| 150 | \method{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 | |
| 207 | \begin{funcdesc}{basicConfig}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 208 | Does basic configuration for the logging system by creating a |
| 209 | \class{StreamHandler} with a default \class{Formatter} and adding it to |
| 210 | the root logger. The functions \function{debug()}, \function{info()}, |
| 211 | \function{warning()}, \function{error()} and \function{critical()} will call |
| 212 | \function{basicConfig()} automatically if no handlers are defined for the |
| 213 | root logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 214 | \end{funcdesc} |
| 215 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 216 | \begin{funcdesc}{shutdown}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 217 | Informs the logging system to perform an orderly shutdown by flushing and |
| 218 | closing all handlers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 219 | \end{funcdesc} |
| 220 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 221 | \begin{funcdesc}{setLoggerClass}{klass} |
| 222 | Tells the logging system to use the class \var{klass} when instantiating a |
| 223 | logger. The class should define \method{__init__()} such that only a name |
| 224 | argument is required, and the \method{__init__()} should call |
| 225 | \method{Logger.__init__()}. This function is typically called before any |
| 226 | loggers are instantiated by applications which need to use custom logger |
| 227 | behavior. |
| 228 | \end{funcdesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 229 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 230 | \subsection{Logger Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 231 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 232 | Loggers have the following attributes and methods. Note that Loggers are |
| 233 | never instantiated directly, but always through the module-level function |
| 234 | \function{logging.getLogger(name)}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 235 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 236 | \begin{datadesc}{propagate} |
| 237 | If this evaluates to false, logging messages are not passed by this |
| 238 | logger or by child loggers to higher level (ancestor) loggers. The |
| 239 | constructor sets this attribute to 1. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 240 | \end{datadesc} |
| 241 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 242 | \begin{methoddesc}{setLevel}{lvl} |
| 243 | Sets the threshold for this logger to \var{lvl}. Logging messages |
| 244 | which are less severe than \var{lvl} will be ignored. When a logger is |
| 245 | created, the level is set to \constant{ALL} (which causes all messages |
| 246 | to be processed). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 247 | \end{methoddesc} |
| 248 | |
| 249 | \begin{methoddesc}{isEnabledFor}{lvl} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 250 | Indicates if a message of severity \var{lvl} would be processed by |
| 251 | this logger. This method checks first the module-level level set by |
| 252 | \function{logging.disable(lvl)} and then the logger's effective level as |
| 253 | determined by \method{getEffectiveLevel()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 254 | \end{methoddesc} |
| 255 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 256 | \begin{methoddesc}{getEffectiveLevel}{} |
| 257 | Indicates the effective level for this logger. If a value other than |
| 258 | \constant{ALL} has been set using \method{setLevel()}, it is returned. |
| 259 | Otherwise, the hierarchy is traversed towards the root until a value |
| 260 | other than \constant{ALL} is found,and that value is returned. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 261 | \end{methoddesc} |
| 262 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 263 | \begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}} |
| 264 | Logs a message with level \constant{DEBUG} on this logger. |
| 265 | The \var{msg} is the message format string, and the \var{args} are the |
| 266 | arguments which are merged into \var{msg}. The only keyword argument in |
| 267 | \var{kwargs} which is inspected is \var{exc_info} which, if it does not |
| 268 | evaluate as false, causes exception information (via a call to |
| 269 | \method{sys.exc_info()}) to be added to the logging message. |
| 270 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 271 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 272 | \begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}} |
| 273 | Logs a message with level \constant{INFO} on this logger. |
| 274 | The arguments are interpreted as for \method{debug()}. |
| 275 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 276 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 277 | \begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}} |
| 278 | Logs a message with level \constant{WARNING} on this logger. |
| 279 | The arguments are interpreted as for \method{debug()}. |
| 280 | \end{methoddesc} |
| 281 | |
| 282 | \begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}} |
| 283 | Logs a message with level \constant{ERROR} on this logger. |
| 284 | The arguments are interpreted as for \method{debug()}. |
| 285 | \end{methoddesc} |
| 286 | |
| 287 | \begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}} |
| 288 | Logs a message with level \constant{CRITICAL} on this logger. |
| 289 | The arguments are interpreted as for \method{debug()}. |
| 290 | \end{methoddesc} |
| 291 | |
| 292 | \begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}} |
| 293 | Logs a message with level \var{lvl} on this logger. |
| 294 | The other arguments are interpreted as for \method{debug()}. |
| 295 | \end{methoddesc} |
| 296 | |
| 297 | \begin{methoddesc}{exception}{msg\optional{, *args}} |
| 298 | Logs a message with level \constant{ERROR} on this logger. |
| 299 | The arguments are interpreted as for \method{debug()}. Exception info |
| 300 | is added to the logging message. This method should only be called |
| 301 | from an exception handler. |
| 302 | \end{methoddesc} |
| 303 | |
| 304 | \begin{methoddesc}{addFilter}{filt} |
| 305 | Adds the specified filter \var{filt} to this logger. |
| 306 | \end{methoddesc} |
| 307 | |
| 308 | \begin{methoddesc}{removeFilter}{filt} |
| 309 | Removes the specified filter \var{filt} from this logger. |
| 310 | \end{methoddesc} |
| 311 | |
| 312 | \begin{methoddesc}{filter}{record} |
| 313 | Applies this logger's filters to the record and returns a true value if |
| 314 | the record is to be processed. |
| 315 | \end{methoddesc} |
| 316 | |
| 317 | \begin{methoddesc}{addHandler}{hdlr} |
| 318 | Adds the specified handler \var{hdlr} to this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 319 | \end{methoddesc} |
| 320 | |
| 321 | \begin{methoddesc}{removeHandler}{hdlr} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 322 | Removes the specified handler \var{hdlr} from this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 323 | \end{methoddesc} |
| 324 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 325 | \begin{methoddesc}{findCaller}{} |
| 326 | Finds the caller's source filename and line number. Returns the filename |
| 327 | and line number as a 2-element tuple. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 328 | \end{methoddesc} |
| 329 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 330 | \begin{methoddesc}{handle}{record} |
| 331 | Handles a record by passing it to all handlers associated with this logger |
| 332 | and its ancestors (until a false value of \var{propagate} is found). |
| 333 | This method is used for unpickled records received from a socket, as well |
| 334 | as those created locally. Logger-level filtering is applied using |
| 335 | \method{filter()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 336 | \end{methoddesc} |
| 337 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 338 | \begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info} |
| 339 | This is a factory method which can be overridden in subclasses to create |
| 340 | specialized \class{LogRecord} instances. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 341 | \end{methoddesc} |
| 342 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 343 | \subsection{Handler Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 344 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 345 | Handlers have the following attributes and methods. Note that a Handler is |
| 346 | never instantiated directly; this class acts as a base for more useful |
| 347 | subclasses. However, the \method{__init__()} in subclasses needs to call |
| 348 | \method{Handler.__init__()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 349 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 350 | \begin{methoddesc}{__init__}{level=\constant{ALL}} |
| 351 | Initializes the \class{Handler} instance by setting its level, setting |
| 352 | the list of filters to the empty list and creating a lock (using |
| 353 | \method{getLock()}) for serializing access to an I/O mechanism. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 354 | \end{methoddesc} |
| 355 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 356 | \begin{methoddesc}{createLock}{} |
| 357 | Initializes a thread lock which can be used to serialize access to |
| 358 | underlying I/O functionality which may not be threadsafe. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 359 | \end{methoddesc} |
| 360 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 361 | \begin{methoddesc}{acquire}{} |
| 362 | Acquires the thread lock created with \method{createLock()}. |
| 363 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 364 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 365 | \begin{methoddesc}{release}{} |
| 366 | Releases the thread lock acquired with \method{acquire()}. |
| 367 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 368 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 369 | \begin{methoddesc}{setLevel}{lvl} |
| 370 | Sets the threshold for this handler to \var{lvl}. Logging messages which are |
| 371 | less severe than \var{lvl} will be ignored. When a handler is created, the |
| 372 | level is set to \constant{ALL} (which causes all messages to be processed). |
| 373 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 374 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 375 | \begin{methoddesc}{setFormatter}{form} |
| 376 | Sets the \class{Formatter} for this handler to \var{form}. |
| 377 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 378 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 379 | \begin{methoddesc}{addFilter}{filt} |
| 380 | Adds the specified filter \var{filt} to this handler. |
| 381 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 382 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 383 | \begin{methoddesc}{removeFilter}{filt} |
| 384 | Removes the specified filter \var{filt} from this handler. |
| 385 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 386 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 387 | \begin{methoddesc}{filter}{record} |
| 388 | Applies this handler's filters to the record and returns a true value if |
| 389 | the record is to be processed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 390 | \end{methoddesc} |
| 391 | |
| 392 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 393 | Ensure all logging output has been flushed. This version does |
| 394 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 395 | \end{methoddesc} |
| 396 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 397 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 398 | Tidy up any resources used by the handler. This version does |
| 399 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 400 | \end{methoddesc} |
| 401 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 402 | \begin{methoddesc}{handle}{record} |
| 403 | Conditionally emits the specified logging record, depending on |
| 404 | filters which may have been added to the handler. Wraps the actual |
| 405 | emission of the record with acquisition/release of the I/O thread |
| 406 | lock. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 407 | \end{methoddesc} |
| 408 | |
| 409 | \begin{methoddesc}{handleError}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 410 | This method should be called from handlers when an exception is |
| 411 | encountered during an emit() call. By default it does nothing, |
| 412 | which means that exceptions get silently ignored. This is what is |
| 413 | mostly wanted for a logging system - most users will not care |
| 414 | about errors in the logging system, they are more interested in |
| 415 | application errors. You could, however, replace this with a custom |
| 416 | handler if you wish. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 417 | \end{methoddesc} |
| 418 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 419 | \begin{methoddesc}{format}{record} |
| 420 | Do formatting for a record - if a formatter is set, use it. |
| 421 | Otherwise, use the default formatter for the module. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 422 | \end{methoddesc} |
| 423 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 424 | \begin{methoddesc}{emit}{record} |
| 425 | Do whatever it takes to actually log the specified logging record. |
| 426 | This version is intended to be implemented by subclasses and so |
| 427 | raises a \exception{NotImplementedError}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 428 | \end{methoddesc} |
| 429 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 430 | \subsubsection{StreamHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 431 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 432 | The \class{StreamHandler} class sends logging output to streams such as |
| 433 | \var{sys.stdout}, \var{sys.stderr} or any file-like object (or, more |
| 434 | precisely, any object which supports \method{write()} and \method{flush()} |
| 435 | methods. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 436 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 437 | \begin{classdesc}{StreamHandler}{\optional{strm}} |
| 438 | Returns a new instance of the \class{StreamHandler} class. If \var{strm} is |
| 439 | specified, the instance will use it for logging output; otherwise, |
| 440 | \var{sys.stderr} will be used. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 441 | \end{classdesc} |
| 442 | |
| 443 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 444 | If a formatter is specified, it is used to format the record. |
| 445 | The record is then written to the stream with a trailing newline. |
| 446 | If exception information is present, it is formatted using |
| 447 | \function{traceback.print_exception()} and appended to the stream. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 448 | \end{methoddesc} |
| 449 | |
| 450 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 451 | Flushes the stream by calling its \method{flush()} method. Note that |
| 452 | the \method{close()} method is inherited from \class{Handler} and |
| 453 | so does nothing, so an explicit \method{flush()} call may be needed |
| 454 | at times. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 455 | \end{methoddesc} |
| 456 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 457 | \subsubsection{FileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 458 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 459 | The \class{FileHandler} class sends logging output to a disk file. |
| 460 | It delegates the output functionality from \class{StreamHandler}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 461 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 462 | \begin{classdesc}{FileHandler}{filename\optional{, mode}} |
| 463 | Returns a new instance of the \class{FileHandler} class. The specified |
| 464 | file is opened and used as the stream for logging. If \var{mode} is |
| 465 | not specified, \constant{"a"} is used. By default, the file grows |
| 466 | indefinitely. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 467 | \end{classdesc} |
| 468 | |
| 469 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 470 | Closes the file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 471 | \end{methoddesc} |
| 472 | |
| 473 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 474 | Outputs the record to the file. |
| 475 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 476 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 477 | \subsubsection{RotatingFileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 478 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 479 | The \class{RotatingFileHandler} class supports rotation of disk log files. |
| 480 | |
| 481 | \begin{classdesc}{RotatingFileHandler}{filename\optional{, mode, maxBytes, |
| 482 | backupCount}} |
| 483 | Returns a new instance of the \class{RotatingFileHandler} class. The |
| 484 | specified file is opened and used as the stream for logging. If |
| 485 | \var{mode} is not specified, \constant{"a"} is used. By default, the |
| 486 | file grows indefinitely. You can use the \var{maxBytes} and |
| 487 | \var{backupCount} values to allow the file to \dfn{rollover} at a |
| 488 | predetermined size. When the size is about to be exceeded, the file is |
| 489 | closed and a new file opened for output, transparently to the |
| 490 | caller. Rollover occurs whenever the current log file is nearly |
| 491 | \var{maxBytes} in length. If \var{backupCount} is >= 1, the system |
| 492 | will successively create new files with the same pathname as the base |
| 493 | file, but with extensions ".1", ".2" etc. appended to it. For example, |
| 494 | with a backupCount of 5 and a base file name of "app.log", you would |
| 495 | get "app.log", "app.log.1", "app.log.2", ... through to |
| 496 | "app.log.5". When the last file reaches its size limit, the logging |
| 497 | reverts to "app.log" which is truncated to zero length. If |
| 498 | \var{maxBytes} is zero, rollover never occurs. |
| 499 | \end{classdesc} |
| 500 | |
| 501 | \begin{methoddesc}{doRollover}{} |
| 502 | Does a rollover, as described above. |
| 503 | \end{methoddesc} |
| 504 | |
| 505 | \begin{methoddesc}{emit}{record} |
| 506 | Outputs the record to the file, catering for rollover as described |
| 507 | in \method{setRollover()}. |
| 508 | \end{methoddesc} |
| 509 | |
| 510 | \subsubsection{SocketHandler} |
| 511 | |
| 512 | The \class{SocketHandler} class sends logging output to a network |
| 513 | socket. The base class uses a TCP socket. |
| 514 | |
| 515 | \begin{classdesc}{SocketHandler}{host, port} |
| 516 | Returns a new instance of the \class{SocketHandler} class intended to |
| 517 | communicate with a remote machine whose address is given by \var{host} |
| 518 | and \var{port}. |
| 519 | \end{classdesc} |
| 520 | |
| 521 | \begin{methoddesc}{close}{} |
| 522 | Closes the socket. |
| 523 | \end{methoddesc} |
| 524 | |
| 525 | \begin{methoddesc}{handleError}{} |
| 526 | \end{methoddesc} |
| 527 | |
| 528 | \begin{methoddesc}{emit}{} |
| 529 | Pickles the record and writes it to the socket in binary format. |
| 530 | If there is an error with the socket, silently drops the packet. |
| 531 | If the connection was previously lost, re-establishes the connection. |
| 532 | \end{methoddesc} |
| 533 | |
| 534 | \begin{methoddesc}{handleError}{} |
| 535 | Handles an error which has occurred during \method{emit()}. The |
| 536 | most likely cause is a lost connection. Closes the socket so that |
| 537 | we can retry on the next event. |
| 538 | \end{methoddesc} |
| 539 | |
| 540 | \begin{methoddesc}{makeSocket}{} |
| 541 | This is a factory method which allows subclasses to define the precise |
| 542 | type of socket they want. The default implementation creates a TCP |
| 543 | socket (\constant{socket.SOCK_STREAM}). |
| 544 | \end{methoddesc} |
| 545 | |
| 546 | \begin{methoddesc}{makePickle}{record} |
| 547 | Pickles the record in binary format with a length prefix, and returns |
| 548 | it ready for transmission across the socket. |
| 549 | \end{methoddesc} |
| 550 | |
| 551 | \begin{methoddesc}{send}{packet} |
| 552 | Send a pickled string \var{packe} to the socket. This function allows |
| 553 | for partial sends which can happen when the network is busy. |
| 554 | \end{methoddesc} |
| 555 | |
| 556 | \subsubsection{DatagramHandler} |
| 557 | |
| 558 | The \class{DatagramHandler} class inherits from \class{SocketHandler} |
| 559 | to support sending logging messages over UDP sockets. |
| 560 | |
| 561 | \begin{classdesc}{DatagramHandler}{host, port} |
| 562 | Returns a new instance of the \class{DatagramHandler} class intended to |
| 563 | communicate with a remote machine whose address is given by \var{host} |
| 564 | and \var{port}. |
| 565 | \end{classdesc} |
| 566 | |
| 567 | \begin{methoddesc}{emit}{} |
| 568 | Pickles the record and writes it to the socket in binary format. |
| 569 | If there is an error with the socket, silently drops the packet. |
| 570 | \end{methoddesc} |
| 571 | |
| 572 | \begin{methoddesc}{makeSocket}{} |
| 573 | The factory method of \class{SocketHandler} is here overridden to create |
| 574 | a UDP socket (\constant{socket.SOCK_DGRAM}). |
| 575 | \end{methoddesc} |
| 576 | |
| 577 | \begin{methoddesc}{send}{s} |
| 578 | Send a pickled string to a socket. This function allows for |
| 579 | partial sends which can happen when the network is busy. |
| 580 | \end{methoddesc} |
| 581 | |
| 582 | \subsubsection{SysLogHandler} |
| 583 | |
| 584 | The \class{SysLogHandler} class supports sending logging messages to a |
| 585 | remote or local Unix syslog. |
| 586 | |
| 587 | \begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}} |
| 588 | Returns a new instance of the \class{SysLogHandler} class intended to |
| 589 | communicate with a remote Unix machine whose address is given by |
| 590 | \var{address} in the form of a (host, port) tuple. If \var{address} is not |
| 591 | specified, ('localhost', 514) is used. The address is used to open a UDP |
| 592 | socket. If \var{facility} is not specified, \constant{LOG_USER} is used. |
| 593 | \end{classdesc} |
| 594 | |
| 595 | \begin{methoddesc}{close}{} |
| 596 | Closes the socket to the remote host. |
| 597 | \end{methoddesc} |
| 598 | |
| 599 | \begin{methoddesc}{emit}{record} |
| 600 | The record is formatted, and then sent to the syslog server. If |
| 601 | exception information is present, it is \emph{not} sent to the server. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 602 | \end{methoddesc} |
| 603 | |
| 604 | \begin{methoddesc}{encodePriority}{facility, priority} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 605 | Encodes the facility and priority into an integer. You can pass in strings |
| 606 | or integers - if strings are passed, internal mapping dictionaries are used |
| 607 | to convert them to integers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 608 | \end{methoddesc} |
| 609 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 610 | \subsubsection{NTEventLogHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 611 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 612 | The \class{NTEventLogHandler} class supports sending logging messages |
| 613 | to a local Windows NT, Windows 2000 or Windows XP event log. Before |
| 614 | you can use it, you need Mark Hammond's Win32 extensions for Python |
| 615 | installed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 616 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 617 | \begin{classdesc}{NTEventLogHandler}{appname |
| 618 | \optional{, dllname\optional{, logtype}}} |
| 619 | Returns a new instance of the \class{NTEventLogHandler} class. The |
| 620 | \var{appname} is used to define the application name as it appears in the |
| 621 | event log. An appropriate registry entry is created using this name. |
| 622 | The \var{dllname} should give the fully qualified pathname of a .dll or .exe |
| 623 | which contains message definitions to hold in the log (if not specified, |
| 624 | \constant{"win32service.pyd"} is used - this is installed with the Win32 |
| 625 | extensions and contains some basic placeholder message definitions. |
| 626 | Note that use of these placeholders will make your event logs big, as the |
| 627 | entire message source is held in the log. If you want slimmer logs, you have |
| 628 | to pass in the name of your own .dll or .exe which contains the message |
| 629 | definitions you want to use in the event log). The \var{logtype} is one of |
| 630 | \constant{"Application"}, \constant{"System"} or \constant{"Security"}, and |
| 631 | defaults to \constant{"Application"}. |
| 632 | \end{classdesc} |
| 633 | |
| 634 | \begin{methoddesc}{close}{} |
| 635 | At this point, you can remove the application name from the registry as a |
| 636 | source of event log entries. However, if you do this, you will not be able |
| 637 | to see the events as you intended in the Event Log Viewer - it needs to be |
| 638 | able to access the registry to get the .dll name. The current version does |
| 639 | not do this (in fact it doesn't do anything). |
| 640 | \end{methoddesc} |
| 641 | |
| 642 | \begin{methoddesc}{emit}{record} |
| 643 | Determines the message ID, event category and event type, and then logs the |
| 644 | message in the NT event log. |
| 645 | \end{methoddesc} |
| 646 | |
| 647 | \begin{methoddesc}{getEventCategory}{record} |
| 648 | Returns the event category for the record. Override this if you |
| 649 | want to specify your own categories. This version returns 0. |
| 650 | \end{methoddesc} |
| 651 | |
| 652 | \begin{methoddesc}{getEventType}{record} |
| 653 | Returns the event type for the record. Override this if you want |
| 654 | to specify your own types. This version does a mapping using the |
| 655 | handler's typemap attribute, which is set up in \method{__init__()} |
| 656 | to a dictionary which contains mappings for \constant{DEBUG}, |
| 657 | \constant{INFO}, \constant{WARNING}, \constant{ERROR} and |
| 658 | \constant{CRITICAL}. If you are using your own levels, you will either need |
| 659 | to override this method or place a suitable dictionary in the |
| 660 | handler's \var{typemap} attribute. |
| 661 | \end{methoddesc} |
| 662 | |
| 663 | \begin{methoddesc}{getMessageID}{record} |
| 664 | Returns the message ID for the record. If you are using your |
| 665 | own messages, you could do this by having the \var{msg} passed to the |
| 666 | logger being an ID rather than a format string. Then, in here, |
| 667 | you could use a dictionary lookup to get the message ID. This |
| 668 | version returns 1, which is the base message ID in |
| 669 | \constant{win32service.pyd}. |
| 670 | \end{methoddesc} |
| 671 | |
| 672 | \subsubsection{SMTPHandler} |
| 673 | |
| 674 | The \class{SMTPHandler} class supports sending logging messages to an email |
| 675 | address via SMTP. |
| 676 | |
| 677 | \begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject} |
| 678 | Returns a new instance of the \class{SMTPHandler} class. The |
| 679 | instance is initialized with the from and to addresses and subject |
| 680 | line of the email. The \var{toaddrs} should be a list of strings without |
| 681 | domain names (That's what the \var{mailhost} is for). To specify a |
| 682 | non-standard SMTP port, use the (host, port) tuple format for the |
| 683 | \var{mailhost} argument. If you use a string, the standard SMTP port |
| 684 | is used. |
| 685 | \end{classdesc} |
| 686 | |
| 687 | \begin{methoddesc}{emit}{record} |
| 688 | Formats the record and sends it to the specified addressees. |
| 689 | \end{methoddesc} |
| 690 | |
| 691 | \begin{methoddesc}{getSubject}{record} |
| 692 | If you want to specify a subject line which is record-dependent, |
| 693 | override this method. |
| 694 | \end{methoddesc} |
| 695 | |
| 696 | \subsubsection{MemoryHandler} |
| 697 | |
| 698 | The \class{MemoryHandler} supports buffering of logging records in memory, |
| 699 | periodically flushing them to a \dfn{target} handler. Flushing occurs |
| 700 | whenever the buffer is full, or when an event of a certain severity or |
| 701 | greater is seen. |
| 702 | |
| 703 | \class{MemoryHandler} is a subclass of the more general |
| 704 | \class{BufferingHandler}, which is an abstract class. This buffers logging |
| 705 | records in memory. Whenever each record is added to the buffer, a |
| 706 | check is made by calling \method{shouldFlush()} to see if the buffer |
| 707 | should be flushed. If it should, then \method{flush()} is expected to |
| 708 | do the needful. |
| 709 | |
| 710 | \begin{classdesc}{BufferingHandler}{capacity} |
| 711 | Initializes the handler with a buffer of the specified capacity. |
| 712 | \end{classdesc} |
| 713 | |
| 714 | \begin{methoddesc}{emit}{record} |
| 715 | Appends the record to the buffer. If \method{shouldFlush()} returns true, |
| 716 | calls \method{flush()} to process the buffer. |
| 717 | \end{methoddesc} |
| 718 | |
| 719 | \begin{methoddesc}{flush}{} |
| 720 | You can override this to implement custom flushing behaviour. This version |
| 721 | just zaps the buffer to empty. |
| 722 | \end{methoddesc} |
| 723 | |
| 724 | \begin{methoddesc}{shouldFlush}{record} |
| 725 | Returns true if the buffer is up to capacity. This method can be |
| 726 | overridden to implement custom flushing strategies. |
| 727 | \end{methoddesc} |
| 728 | |
| 729 | \begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel |
| 730 | \optional{, target}}} |
| 731 | Returns a new instance of the \class{MemoryHandler} class. The |
| 732 | instance is initialized with a buffer size of \var{capacity}. If |
| 733 | \var{flushLevel} is not specified, \constant{ERROR} is used. If no |
| 734 | \var{target} is specified, the target will need to be set using |
| 735 | \method{setTarget()} before this handler does anything useful. |
| 736 | \end{classdesc} |
| 737 | |
| 738 | \begin{methoddesc}{close}{} |
| 739 | Calls \method{flush()}, sets the target to \constant{None} and |
| 740 | clears the buffer. |
| 741 | \end{methoddesc} |
| 742 | |
| 743 | \begin{methoddesc}{flush}{} |
| 744 | For a \class{MemoryHandler}, flushing means just sending the buffered |
| 745 | records to the target, if there is one. Override if you want |
| 746 | different behaviour. |
| 747 | \end{methoddesc} |
| 748 | |
| 749 | \begin{methoddesc}{setTarget}{target} |
| 750 | Sets the target handler for this handler. |
| 751 | \end{methoddesc} |
| 752 | |
| 753 | \begin{methoddesc}{shouldFlush}{record} |
| 754 | Checks for buffer full or a record at the \var{flushLevel} or higher. |
| 755 | \end{methoddesc} |
| 756 | |
| 757 | \subsubsection{HTTPHandler} |
| 758 | |
| 759 | The \class{HTTPHandler} class supports sending logging messages to a |
| 760 | Web server, using either GET or POST semantics. |
| 761 | |
| 762 | \begin{classdesc}{HTTPHandler}{host, url\optional{, method}} |
| 763 | Returns a new instance of the \class{HTTPHandler} class. The |
| 764 | instance is initialized with a host address, url and HTTP method. |
| 765 | If no \var{method} is specified, GET is used. |
| 766 | \end{classdesc} |
| 767 | |
| 768 | \begin{methoddesc}{emit}{record} |
| 769 | Sends the record to the Web server as an URL-encoded dictionary. |
| 770 | \end{methoddesc} |
| 771 | |
| 772 | \subsection{Formatter Objects} |
| 773 | |
| 774 | \class{Formatter}s have the following attributes and methods. They are |
| 775 | responsible for converting a \class{LogRecord} to (usually) a string |
| 776 | which can be interpreted by either a human or an external system. The |
| 777 | base |
| 778 | \class{Formatter} allows a formatting string to be specified. If none is |
| 779 | supplied, the default value of "\%s(message)\\n" is used. |
| 780 | |
| 781 | A Formatter can be initialized with a format string which makes use of |
| 782 | knowledge of the \class{LogRecord} attributes - e.g. the default value |
| 783 | mentioned above makes use of the fact that the user's message and |
| 784 | arguments are pre- formatted into a LogRecord's \var{message} |
| 785 | attribute. Currently, the useful attributes in a LogRecord are |
| 786 | described by: |
| 787 | |
| 788 | \%(name)s Name of the logger (logging channel) |
| 789 | \%(levelno)s Numeric logging level for the message (DEBUG, INFO, |
| 790 | WARNING, ERROR, CRITICAL) |
| 791 | \%(levelname)s Text logging level for the message ("DEBUG", "INFO", |
| 792 | "WARNING", "ERROR", "CRITICAL") |
| 793 | \%(pathname)s Full pathname of the source file where the logging |
| 794 | call was issued (if available) |
| 795 | \%(filename)s Filename portion of pathname |
| 796 | \%(module)s Module (name portion of filename) |
| 797 | \%(lineno)d Source line number where the logging call was issued |
| 798 | (if available) |
| 799 | \%(created)f Time when the LogRecord was created (time.time() |
| 800 | return value) |
| 801 | \%(asctime)s Textual time when the LogRecord was created |
| 802 | \%(msecs)d Millisecond portion of the creation time |
| 803 | \%(relativeCreated)d Time in milliseconds when the LogRecord was created, |
| 804 | relative to the time the logging module was loaded |
| 805 | (typically at application startup time) |
| 806 | \%(thread)d Thread ID (if available) |
| 807 | \%(message)s The result of msg \% args, computed just as the |
| 808 | record is emitted |
| 809 | |
| 810 | |
| 811 | \begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}} |
| 812 | Returns a new instance of the \class{Formatter} class. The |
| 813 | instance is initialized with a format string for the message as a whole, |
| 814 | as well as a format string for the date/time portion of a message. If |
| 815 | no \var{fmt} is specified, "\%(message)s" is used. If no \var{datefmt} |
| 816 | is specified, the ISO8601 date format is used. |
| 817 | \end{classdesc} |
| 818 | |
| 819 | \begin{methoddesc}{format}{record} |
| 820 | The record's attribute dictionary is used as the operand to a |
| 821 | string formatting operation. Returns the resulting string. |
| 822 | Before formatting the dictionary, a couple of preparatory steps |
| 823 | are carried out. The \var{message} attribute of the record is computed |
| 824 | using \var{msg} \% \var{args}. If the formatting string contains |
| 825 | \constant{"(asctime)"}, \method{formatTime()} is called to format the |
| 826 | event time. If there is exception information, it is formatted using |
| 827 | \method{formatException()} and appended to the message. |
| 828 | \end{methoddesc} |
| 829 | |
| 830 | \begin{methoddesc}{formatTime}{record\optional{, datefmt}} |
| 831 | This method should be called from \method{format()} by a formatter which |
| 832 | wants to make use of a formatted time. This method can be overridden |
| 833 | in formatters to provide for any specific requirement, but the |
| 834 | basic behaviour is as follows: if \var{datefmt} (a string) is specified, |
| 835 | it is used with \method{time.strftime()} to format the creation time of the |
| 836 | record. Otherwise, the ISO8601 format is used. The resulting |
| 837 | string is returned. |
| 838 | \end{methoddesc} |
| 839 | |
| 840 | \begin{methoddesc}{formatException}{exc_info} |
| 841 | Formats the specified exception information (a standard exception tuple |
| 842 | as returned by \method{sys.exc_info()}) as a string. This default |
| 843 | implementation just uses \method{traceback.print_exception()}. |
| 844 | The resulting string is returned. |
| 845 | \end{methoddesc} |
| 846 | |
| 847 | \subsection{Filter Objects} |
| 848 | |
| 849 | \class{Filter}s can be used by \class{Handler}s and \class{Logger}s for |
| 850 | more sophisticated filtering than is provided by levels. The base filter |
| 851 | class only allows events which are below a certain point in the logger |
| 852 | hierarchy. For example, a filter initialized with "A.B" will allow events |
| 853 | logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB", |
| 854 | "B.A.B" etc. If initialized with the empty string, all events are passed. |
| 855 | |
| 856 | \begin{classdesc}{Filter}{\optional{name}} |
| 857 | Returns an instance of the \class{Filter} class. If \var{name} is specified, |
| 858 | it names a logger which, together with its children, will have its events |
| 859 | allowed through the filter. If no name is specified, allows every event. |
| 860 | \end{classdesc} |
| 861 | |
| 862 | \begin{methoddesc}{filter}{record} |
| 863 | Is the specified record to be logged? Returns zero for no, nonzero for |
| 864 | yes. If deemed appropriate, the record may be modified in-place by this |
| 865 | method. |
| 866 | \end{methoddesc} |
| 867 | |
| 868 | \subsection{LogRecord Objects} |
| 869 | |
| 870 | LogRecord instances are created every time something is logged. They |
| 871 | contain all the information pertinent to the event being logged. The |
| 872 | main information passed in is in msg and args, which are combined |
| 873 | using msg \% args to create the message field of the record. The record |
| 874 | also includes information such as when the record was created, the |
| 875 | source line where the logging call was made, and any exception |
| 876 | information to be logged. |
| 877 | |
| 878 | LogRecord has no methods; it's just a repository for information about the |
| 879 | logging event. The only reason it's a class rather than a dictionary is to |
| 880 | facilitate extension. |
| 881 | |
| 882 | \begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args, |
| 883 | exc_info} |
| 884 | Returns an instance of \class{LogRecord} initialized with interesting |
| 885 | information. The \var{name} is the logger name; \var{lvl} is the |
| 886 | numeric level; \var{pathname} is the absolute pathname of the source |
| 887 | file in which the logging call was made; \var{lineno} is the line |
| 888 | number in that file where the logging call is found; \var{msg} is the |
| 889 | user-supplied message (a format string); \var{args} is the tuple |
| 890 | which, together with \var{msg}, makes up the user message; and |
| 891 | \var{exc_info} is the exception tuple obtained by calling |
| 892 | \function{sys.exc_info() }(or \constant{None}, if no exception information |
| 893 | is available). |
| 894 | \end{classdesc} |
| 895 | |
| 896 | \subsection{Thread Safety} |
| 897 | |
| 898 | The logging module is intended to be thread-safe without any special work |
| 899 | needing to be done by its clients. It achieves this though using threading |
| 900 | locks; there is one lock to serialize access to the module's shared data, |
| 901 | and each handler also creates a lock to serialize access to its underlying |
| 902 | I/O. |
| 903 | |
| 904 | \subsection{Configuration} |
| 905 | |
| 906 | |
| 907 | \subsubsection{Configuration functions} |
| 908 | |
| 909 | The following functions allow the logging module to be configured. |
| 910 | |
| 911 | \begin{funcdesc}{fileConfig}{fname\optional{, defaults}} |
| 912 | Reads the logging configuration from a ConfigParser-format file named |
| 913 | \var{fname}. This function can be called several times from an application, |
| 914 | allowing an end user the ability to select from various pre-canned |
| 915 | configurations (if the developer provides a mechanism to present the |
| 916 | choices and load the chosen configuration). Defaults to be passed to |
| 917 | ConfigParser can be specified in the \var{defaults} argument. |
| 918 | \end{funcdesc} |
| 919 | |
| 920 | \begin{funcdesc}{listen}{\optional{port}} |
| 921 | Starts up a socket server on the specified port, and listens for new |
| 922 | configurations. If no port is specified, the module's default |
| 923 | \constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations |
| 924 | will be sent as a file suitable for processing by \function{fileConfig()}. |
| 925 | Returns a \class{Thread} instance on which you can call \method{start()} |
| 926 | to start the server, and which you can \method{join()} when appropriate. |
| 927 | To stop the server, call \function{stopListening()}. |
| 928 | \end{funcdesc} |
| 929 | |
| 930 | \begin{funcdesc}{stopListening}{} |
| 931 | Stops the listening server which was created with a call to |
| 932 | \function{listen()}. This is typically called before calling \method{join()} |
| 933 | on the return value from \function{listen()}. |
| 934 | \end{funcdesc} |
| 935 | |
| 936 | \subsubsection{Configuration file format} |
| 937 | |
| 938 | The configuration file format understood by \function{fileConfig} is |
| 939 | based on ConfigParser functionality. The file must contain sections |
| 940 | called \code{[loggers]}, \code{[handlers]} and \code{[formatters]} |
| 941 | which identify by name the entities of each type which are defined in |
| 942 | the file. For each such entity, there is a separate section which |
| 943 | identified how that entity is configured. Thus, for a logger named |
| 944 | \code{log01} in the \code{[loggers]} section, the relevant |
| 945 | configuration details are held in a section |
| 946 | \code{[logger_log01]}. Similarly, a handler called \code{hand01} in |
| 947 | the \code{[handlers]} section will have its configuration held in a |
| 948 | section called \code{[handler_hand01]}, while a formatter called |
| 949 | \code{form01} in the \code{[formatters]} section will have its |
| 950 | configuration specified in a section called |
| 951 | \code{[formatter_form01]}. The root logger configuration must be |
| 952 | specified in a section called \code{[logger_root]}. |
| 953 | |
| 954 | Examples of these sections in the file are given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 955 | |
| 956 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 957 | [loggers] |
| 958 | keys=root,log02,log03,log04,log05,log06,log07 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 959 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 960 | [handlers] |
| 961 | keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09 |
| 962 | |
| 963 | [formatters] |
| 964 | keys=form01,form02,form03,form04,form05,form06,form07,form08,form09 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 965 | \end{verbatim} |
| 966 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 967 | The root logger must specify a level and a list of handlers. An |
| 968 | example of a root logger section is given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 969 | |
| 970 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 971 | [logger_root] |
| 972 | level=NOTSET |
| 973 | handlers=hand01 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 974 | \end{verbatim} |
| 975 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 976 | The \code{level} entry can be one of \code{DEBUG, INFO, WARNING, |
| 977 | ERROR, CRITICAL} or \code{NOTSET}. For the root logger only, |
| 978 | \code{NOTSET} means that all messages will be logged. Level values are |
| 979 | \function{eval()}uated in the context of the \code{logging} package's |
| 980 | namespace. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 981 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 982 | The \code{handlers} entry is a comma-separated list of handler names, |
| 983 | which must appear in the \code{[handlers]} section. These names must |
| 984 | appear in the \code{[handlers]} section and have corresponding |
| 985 | sections in the configuration file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 986 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 987 | For loggers other than the root logger, some additional information is |
| 988 | required. This is illustrated by the following example. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 989 | |
| 990 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 991 | [logger_parser] |
| 992 | level=DEBUG |
| 993 | handlers=hand01 |
| 994 | propagate=1 |
| 995 | qualname=compiler.parser |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 996 | \end{verbatim} |
| 997 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 998 | The \code{level} and \code{handlers} entries are interpreted as for |
| 999 | the root logger, except that if a non-root logger's level is specified |
| 1000 | as \code{NOTSET}, the system consults loggers higher up the hierarchy |
| 1001 | to determine the effective level of the logger. The \code{propagate} |
| 1002 | entry is set to 1 to indicate that messages must propagate to handlers |
| 1003 | higher up the logger hierarchy from this logger, or 0 to indicate that |
| 1004 | messages are \strong{not} propagated to handlers up the hierarchy. The |
| 1005 | \code{qualname} entry is the hierarchical channel name of the logger, |
| 1006 | i.e. the name used by the application to get the logger. |
| 1007 | |
| 1008 | Sections which specify handler configuration are exemplified by the |
| 1009 | following. |
| 1010 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1011 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1012 | [handler_hand01] |
| 1013 | class=StreamHandler |
| 1014 | level=NOTSET |
| 1015 | formatter=form01 |
| 1016 | args=(sys.stdout,) |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1017 | \end{verbatim} |
| 1018 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1019 | The \code{class} entry indicates the handler's class (as determined by |
| 1020 | \function{eval()} in the \code{logging} package's namespace). The |
| 1021 | \code{level} is interpreted as for loggers, and \code{NOTSET} is taken |
| 1022 | to mean "log everything". |
| 1023 | |
| 1024 | The \code{formatter} entry indicates the key name of the formatter for |
| 1025 | this handler. If blank, a default formatter |
| 1026 | (\code{logging._defaultFormatter}) is used. If a name is specified, it |
| 1027 | must appear in the \code{[formatters]} section and have a |
| 1028 | corresponding section in the configuration file. |
| 1029 | |
| 1030 | The \code{args} entry, when \function{eval()}uated in the context of |
| 1031 | the \code{logging} package's namespace, is the list of arguments to |
| 1032 | the constructor for the handler class. Refer to the constructors for |
| 1033 | the relevant handlers, or to the examples below, to see how typical |
| 1034 | entries are constructed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1035 | |
| 1036 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1037 | [handler_hand02] |
| 1038 | class=FileHandler |
| 1039 | level=DEBUG |
| 1040 | formatter=form02 |
| 1041 | args=('python.log', 'w') |
| 1042 | |
| 1043 | [handler_hand03] |
| 1044 | class=handlers.SocketHandler |
| 1045 | level=INFO |
| 1046 | formatter=form03 |
| 1047 | args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT) |
| 1048 | |
| 1049 | [handler_hand04] |
| 1050 | class=handlers.DatagramHandler |
| 1051 | level=WARN |
| 1052 | formatter=form04 |
| 1053 | args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT) |
| 1054 | |
| 1055 | [handler_hand05] |
| 1056 | class=handlers.SysLogHandler |
| 1057 | level=ERROR |
| 1058 | formatter=form05 |
| 1059 | args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER) |
| 1060 | |
| 1061 | [handler_hand06] |
| 1062 | class=NTEventLogHandler |
| 1063 | level=CRITICAL |
| 1064 | formatter=form06 |
| 1065 | args=('Python Application', '', 'Application') |
| 1066 | |
| 1067 | [handler_hand07] |
| 1068 | class=SMTPHandler |
| 1069 | level=WARN |
| 1070 | formatter=form07 |
| 1071 | args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject') |
| 1072 | |
| 1073 | [handler_hand08] |
| 1074 | class=MemoryHandler |
| 1075 | level=NOTSET |
| 1076 | formatter=form08 |
| 1077 | target= |
| 1078 | args=(10, ERROR) |
| 1079 | |
| 1080 | [handler_hand09] |
| 1081 | class=HTTPHandler |
| 1082 | level=NOTSET |
| 1083 | formatter=form09 |
| 1084 | args=('localhost:9022', '/log', 'GET') |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1085 | \end{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1086 | |
| 1087 | Sections which specify formatter configuration are typified by the following. |
| 1088 | |
| 1089 | \begin{verbatim} |
| 1090 | [formatter_form01] |
| 1091 | format=F1 %(asctime)s %(levelname)s %(message)s |
| 1092 | datefmt= |
| 1093 | \end{verbatim} |
| 1094 | |
| 1095 | The \code{format} entry is the overall format string, and the |
| 1096 | \code{datefmt} entry is the \function{strftime()}-compatible date/time format |
| 1097 | string. If empty, the package substitutes ISO8601 format date/times, which |
| 1098 | is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S". |
| 1099 | The ISO8601 format also specifies milliseconds, which are appended to the |
| 1100 | result of using the above format string, with a comma separator. An example |
| 1101 | time in ISO8601 format is \code{2003-01-23 00:29:50,411}. |