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