Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1 | \section{\module{logging} --- |
| 2 | Logging facility for Python} |
| 3 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{logging} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 5 | |
| 6 | % These apply to all modules, and may be given more than once: |
| 7 | |
| 8 | \moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 9 | \sectionauthor{Vinay Sajip}{vinay_sajip@red-dove.com} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 10 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 11 | \modulesynopsis{Logging module for Python based on \pep{282}.} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 12 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 13 | \indexii{Errors}{logging} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 15 | \versionadded{2.3} |
| 16 | This module defines functions and classes which implement a flexible |
| 17 | error logging system for applications. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 18 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 19 | Logging is performed by calling methods on instances of the |
| 20 | \class{Logger} class (hereafter called \dfn{loggers}). Each instance has a |
| 21 | name, and they are conceptually arranged in a name space hierarchy |
| 22 | using dots (periods) as separators. For example, a logger named |
| 23 | "scan" is the parent of loggers "scan.text", "scan.html" and "scan.pdf". |
| 24 | Logger names can be anything you want, and indicate the area of an |
| 25 | application in which a logged message originates. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 26 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 27 | Logged messages also have levels of importance associated with them. |
| 28 | The default levels provided are \constant{DEBUG}, \constant{INFO}, |
| 29 | \constant{WARNING}, \constant{ERROR} and \constant{CRITICAL}. As a |
| 30 | convenience, you indicate the importance of a logged message by calling |
| 31 | an appropriate method of \class{Logger}. The methods are |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 32 | \method{debug()}, \method{info()}, \method{warning()}, \method{error()} and |
| 33 | \method{critical()}, which mirror the default levels. You are not |
| 34 | constrained to use these levels: you can specify your own and use a |
| 35 | more general \class{Logger} method, \method{log()}, which takes an |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 36 | explicit level argument. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 37 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 38 | Levels can also be associated with loggers, being set either by the |
| 39 | developer or through loading a saved logging configuration. When a |
| 40 | logging method is called on a logger, the logger compares its own |
| 41 | level with the level associated with the method call. If the logger's |
| 42 | level is higher than the method call's, no logging message is actually |
| 43 | generated. This is the basic mechanism controlling the verbosity of |
| 44 | logging output. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 45 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 46 | Logging messages are encoded as instances of the \class{LogRecord} class. |
| 47 | When a logger decides to actually log an event, an \class{LogRecord} |
| 48 | instance is created from the logging message. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 49 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 50 | Logging messages are subjected to a dispatch mechanism through the |
| 51 | use of \dfn{handlers}, which are instances of subclasses of the |
| 52 | \class{Handler} class. Handlers are responsible for ensuring that a logged |
| 53 | message (in the form of a \class{LogRecord}) ends up in a particular |
| 54 | location (or set of locations) which is useful for the target audience for |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 55 | that message (such as end users, support desk staff, system administrators, |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 56 | developers). Handlers are passed \class{LogRecord} instances intended for |
| 57 | particular destinations. Each logger can have zero, one or more handlers |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 58 | associated with it (via the \method{addHandler()} method of \class{Logger}). |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 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 |
Vinay Sajip | 17952b7 | 2004-08-31 10:21:51 +0000 | [diff] [blame] | 137 | a logger which is the root logger of the hierarchy. If specified, the name |
| 138 | is typically a dot-separated hierarchical name like \var{"a"}, \var{"a.b"} |
| 139 | or \var{"a.b.c.d"}. Choice of these names is entirely up to the developer |
| 140 | who is using logging. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 141 | |
| 142 | All calls to this function with a given name return the same logger instance. |
| 143 | This means that logger instances never need to be passed between different |
| 144 | parts of an application. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 145 | \end{funcdesc} |
| 146 | |
Vinay Sajip | c6646c0 | 2004-09-22 12:55:16 +0000 | [diff] [blame] | 147 | \begin{funcdesc}{getLoggerClass}{} |
| 148 | Return either the standard \class{Logger} class, or the last class passed to |
| 149 | \function{setLoggerClass()}. This function may be called from within a new |
| 150 | class definition, to ensure that installing a customised \class{Logger} class |
| 151 | will not undo customisations already applied by other code. For example: |
| 152 | |
| 153 | \begin{verbatim} |
| 154 | class MyLogger(logging.getLoggerClass()): |
| 155 | # ... override behaviour here |
| 156 | \end{verbatim} |
| 157 | |
| 158 | \end{funcdesc} |
| 159 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 160 | \begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}} |
| 161 | Logs a message with level \constant{DEBUG} on the root logger. |
| 162 | The \var{msg} is the message format string, and the \var{args} are the |
| 163 | arguments which are merged into \var{msg}. The only keyword argument in |
| 164 | \var{kwargs} which is inspected is \var{exc_info} which, if it does not |
Vinay Sajip | 1dc5b1e | 2004-10-03 19:10:05 +0000 | [diff] [blame] | 165 | evaluate as false, causes exception information to be added to the logging |
| 166 | message. If an exception tuple (in the format returned by |
| 167 | \function{sys.exc_info()}) is provided, it is used; otherwise, |
| 168 | \function{sys.exc_info()} is called to get the exception information. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 169 | \end{funcdesc} |
| 170 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 171 | \begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}} |
| 172 | Logs a message with level \constant{INFO} on the root logger. |
| 173 | The arguments are interpreted as for \function{debug()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 174 | \end{funcdesc} |
| 175 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 176 | \begin{funcdesc}{warning}{msg\optional{, *args\optional{, **kwargs}}} |
| 177 | Logs a message with level \constant{WARNING} on the root logger. |
| 178 | The arguments are interpreted as for \function{debug()}. |
| 179 | \end{funcdesc} |
| 180 | |
| 181 | \begin{funcdesc}{error}{msg\optional{, *args\optional{, **kwargs}}} |
| 182 | Logs a message with level \constant{ERROR} on the root logger. |
| 183 | The arguments are interpreted as for \function{debug()}. |
| 184 | \end{funcdesc} |
| 185 | |
| 186 | \begin{funcdesc}{critical}{msg\optional{, *args\optional{, **kwargs}}} |
| 187 | Logs a message with level \constant{CRITICAL} on the root logger. |
| 188 | The arguments are interpreted as for \function{debug()}. |
| 189 | \end{funcdesc} |
| 190 | |
| 191 | \begin{funcdesc}{exception}{msg\optional{, *args}} |
| 192 | Logs a message with level \constant{ERROR} on the root logger. |
| 193 | The arguments are interpreted as for \function{debug()}. Exception info |
| 194 | is added to the logging message. This function should only be called |
| 195 | from an exception handler. |
| 196 | \end{funcdesc} |
| 197 | |
Vinay Sajip | 739d49e | 2004-09-24 11:46:44 +0000 | [diff] [blame] | 198 | \begin{funcdesc}{log}{level, msg\optional{, *args\optional{, **kwargs}}} |
| 199 | Logs a message with level \var{level} on the root logger. |
| 200 | The other arguments are interpreted as for \function{debug()}. |
| 201 | \end{funcdesc} |
| 202 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 203 | \begin{funcdesc}{disable}{lvl} |
| 204 | Provides an overriding level \var{lvl} for all loggers which takes |
| 205 | precedence over the logger's own level. When the need arises to |
| 206 | temporarily throttle logging output down across the whole application, |
| 207 | this function can be useful. |
| 208 | \end{funcdesc} |
| 209 | |
| 210 | \begin{funcdesc}{addLevelName}{lvl, levelName} |
| 211 | Associates level \var{lvl} with text \var{levelName} in an internal |
| 212 | dictionary, which is used to map numeric levels to a textual |
| 213 | representation, for example when a \class{Formatter} formats a message. |
| 214 | This function can also be used to define your own levels. The only |
| 215 | constraints are that all levels used must be registered using this |
| 216 | function, levels should be positive integers and they should increase |
| 217 | in increasing order of severity. |
| 218 | \end{funcdesc} |
| 219 | |
| 220 | \begin{funcdesc}{getLevelName}{lvl} |
| 221 | Returns the textual representation of logging level \var{lvl}. If the |
| 222 | level is one of the predefined levels \constant{CRITICAL}, |
| 223 | \constant{ERROR}, \constant{WARNING}, \constant{INFO} or \constant{DEBUG} |
| 224 | then you get the corresponding string. If you have associated levels |
| 225 | with names using \function{addLevelName()} then the name you have associated |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 226 | with \var{lvl} is returned. If a numeric value corresponding to one of the |
| 227 | defined levels is passed in, the corresponding string representation is |
| 228 | returned. Otherwise, the string "Level \%s" \% lvl is returned. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 229 | \end{funcdesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 230 | |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 231 | \begin{funcdesc}{makeLogRecord}{attrdict} |
| 232 | Creates and returns a new \class{LogRecord} instance whose attributes are |
| 233 | defined by \var{attrdict}. This function is useful for taking a pickled |
| 234 | \class{LogRecord} attribute dictionary, sent over a socket, and reconstituting |
| 235 | it as a \class{LogRecord} instance at the receiving end. |
| 236 | \end{funcdesc} |
| 237 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 238 | \begin{funcdesc}{basicConfig}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 239 | Does basic configuration for the logging system by creating a |
| 240 | \class{StreamHandler} with a default \class{Formatter} and adding it to |
| 241 | the root logger. The functions \function{debug()}, \function{info()}, |
| 242 | \function{warning()}, \function{error()} and \function{critical()} will call |
| 243 | \function{basicConfig()} automatically if no handlers are defined for the |
| 244 | root logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 245 | \end{funcdesc} |
| 246 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 247 | \begin{funcdesc}{shutdown}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 248 | Informs the logging system to perform an orderly shutdown by flushing and |
| 249 | closing all handlers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 250 | \end{funcdesc} |
| 251 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 252 | \begin{funcdesc}{setLoggerClass}{klass} |
| 253 | Tells the logging system to use the class \var{klass} when instantiating a |
| 254 | logger. The class should define \method{__init__()} such that only a name |
| 255 | argument is required, and the \method{__init__()} should call |
| 256 | \method{Logger.__init__()}. This function is typically called before any |
| 257 | loggers are instantiated by applications which need to use custom logger |
| 258 | behavior. |
| 259 | \end{funcdesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 260 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 261 | |
| 262 | \begin{seealso} |
| 263 | \seepep{282}{A Logging System} |
| 264 | {The proposal which described this feature for inclusion in |
| 265 | the Python standard library.} |
Fred Drake | 1151479 | 2004-01-08 14:59:02 +0000 | [diff] [blame] | 266 | \seelink{http://www.red-dove.com/python_logging.html} |
| 267 | {Original Python \module{logging} package} |
| 268 | {This is the original source for the \module{logging} |
| 269 | package. The version of the package available from this |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 270 | site is suitable for use with Python 1.5.2, 2.1.x and 2.2.x, |
| 271 | which do not include the \module{logging} package in the standard |
Fred Drake | 1151479 | 2004-01-08 14:59:02 +0000 | [diff] [blame] | 272 | library.} |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 273 | \end{seealso} |
| 274 | |
| 275 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 276 | \subsection{Logger Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 277 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 278 | Loggers have the following attributes and methods. Note that Loggers are |
| 279 | never instantiated directly, but always through the module-level function |
| 280 | \function{logging.getLogger(name)}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 281 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 282 | \begin{datadesc}{propagate} |
| 283 | If this evaluates to false, logging messages are not passed by this |
| 284 | logger or by child loggers to higher level (ancestor) loggers. The |
| 285 | constructor sets this attribute to 1. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 286 | \end{datadesc} |
| 287 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 288 | \begin{methoddesc}{setLevel}{lvl} |
| 289 | Sets the threshold for this logger to \var{lvl}. Logging messages |
| 290 | which are less severe than \var{lvl} will be ignored. When a logger is |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 291 | created, the level is set to \constant{NOTSET} (which causes all messages |
| 292 | to be processed in the root logger, or delegation to the parent in non-root |
| 293 | loggers). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 294 | \end{methoddesc} |
| 295 | |
| 296 | \begin{methoddesc}{isEnabledFor}{lvl} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 297 | Indicates if a message of severity \var{lvl} would be processed by |
| 298 | this logger. This method checks first the module-level level set by |
| 299 | \function{logging.disable(lvl)} and then the logger's effective level as |
| 300 | determined by \method{getEffectiveLevel()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 301 | \end{methoddesc} |
| 302 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 303 | \begin{methoddesc}{getEffectiveLevel}{} |
| 304 | Indicates the effective level for this logger. If a value other than |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 305 | \constant{NOTSET} has been set using \method{setLevel()}, it is returned. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 306 | Otherwise, the hierarchy is traversed towards the root until a value |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 307 | other than \constant{NOTSET} is found, and that value is returned. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 308 | \end{methoddesc} |
| 309 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 310 | \begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}} |
| 311 | Logs a message with level \constant{DEBUG} on this logger. |
| 312 | The \var{msg} is the message format string, and the \var{args} are the |
| 313 | arguments which are merged into \var{msg}. The only keyword argument in |
| 314 | \var{kwargs} which is inspected is \var{exc_info} which, if it does not |
Vinay Sajip | 1dc5b1e | 2004-10-03 19:10:05 +0000 | [diff] [blame] | 315 | evaluate as false, causes exception information to be added to the logging |
| 316 | message. If an exception tuple (as provided by \function{sys.exc_info()}) |
| 317 | is provided, it is used; otherwise, \function{sys.exc_info()} is called |
| 318 | to get the exception information. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 319 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 320 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 321 | \begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}} |
| 322 | Logs a message with level \constant{INFO} on this logger. |
| 323 | The arguments are interpreted as for \method{debug()}. |
| 324 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 325 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 326 | \begin{methoddesc}{warning}{msg\optional{, *args\optional{, **kwargs}}} |
| 327 | Logs a message with level \constant{WARNING} on this logger. |
| 328 | The arguments are interpreted as for \method{debug()}. |
| 329 | \end{methoddesc} |
| 330 | |
| 331 | \begin{methoddesc}{error}{msg\optional{, *args\optional{, **kwargs}}} |
| 332 | Logs a message with level \constant{ERROR} on this logger. |
| 333 | The arguments are interpreted as for \method{debug()}. |
| 334 | \end{methoddesc} |
| 335 | |
| 336 | \begin{methoddesc}{critical}{msg\optional{, *args\optional{, **kwargs}}} |
| 337 | Logs a message with level \constant{CRITICAL} on this logger. |
| 338 | The arguments are interpreted as for \method{debug()}. |
| 339 | \end{methoddesc} |
| 340 | |
| 341 | \begin{methoddesc}{log}{lvl, msg\optional{, *args\optional{, **kwargs}}} |
Vinay Sajip | 1cf56d0 | 2004-08-04 08:36:44 +0000 | [diff] [blame] | 342 | Logs a message with integer level \var{lvl} on this logger. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 343 | The other arguments are interpreted as for \method{debug()}. |
| 344 | \end{methoddesc} |
| 345 | |
| 346 | \begin{methoddesc}{exception}{msg\optional{, *args}} |
| 347 | Logs a message with level \constant{ERROR} on this logger. |
| 348 | The arguments are interpreted as for \method{debug()}. Exception info |
| 349 | is added to the logging message. This method should only be called |
| 350 | from an exception handler. |
| 351 | \end{methoddesc} |
| 352 | |
| 353 | \begin{methoddesc}{addFilter}{filt} |
| 354 | Adds the specified filter \var{filt} to this logger. |
| 355 | \end{methoddesc} |
| 356 | |
| 357 | \begin{methoddesc}{removeFilter}{filt} |
| 358 | Removes the specified filter \var{filt} from this logger. |
| 359 | \end{methoddesc} |
| 360 | |
| 361 | \begin{methoddesc}{filter}{record} |
| 362 | Applies this logger's filters to the record and returns a true value if |
| 363 | the record is to be processed. |
| 364 | \end{methoddesc} |
| 365 | |
| 366 | \begin{methoddesc}{addHandler}{hdlr} |
| 367 | Adds the specified handler \var{hdlr} to this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 368 | \end{methoddesc} |
| 369 | |
| 370 | \begin{methoddesc}{removeHandler}{hdlr} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 371 | Removes the specified handler \var{hdlr} from this logger. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 372 | \end{methoddesc} |
| 373 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 374 | \begin{methoddesc}{findCaller}{} |
| 375 | Finds the caller's source filename and line number. Returns the filename |
| 376 | and line number as a 2-element tuple. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 377 | \end{methoddesc} |
| 378 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 379 | \begin{methoddesc}{handle}{record} |
| 380 | Handles a record by passing it to all handlers associated with this logger |
| 381 | and its ancestors (until a false value of \var{propagate} is found). |
| 382 | This method is used for unpickled records received from a socket, as well |
| 383 | as those created locally. Logger-level filtering is applied using |
| 384 | \method{filter()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 385 | \end{methoddesc} |
| 386 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 387 | \begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info} |
| 388 | This is a factory method which can be overridden in subclasses to create |
| 389 | specialized \class{LogRecord} instances. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 390 | \end{methoddesc} |
| 391 | |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 392 | \subsection{Basic example \label{minimal-example}} |
| 393 | |
| 394 | The \module{logging} package provides a lot of flexibility, and its |
| 395 | configuration can appear daunting. This section demonstrates that simple |
| 396 | use of the logging package is possible. |
| 397 | |
| 398 | The simplest example shows logging to the console: |
| 399 | |
| 400 | \begin{verbatim} |
| 401 | import logging |
| 402 | |
| 403 | logging.debug('A debug message') |
| 404 | logging.info('Some information') |
| 405 | logging.warning('A shot across the bows') |
| 406 | \end{verbatim} |
| 407 | |
| 408 | If you run the above script, you'll see this: |
| 409 | \begin{verbatim} |
| 410 | WARNING:root:A shot across the bows |
| 411 | \end{verbatim} |
| 412 | |
| 413 | Because no particular logger was specified, the system used the root logger. |
| 414 | The debug and info messages didn't appear because by default, the root |
| 415 | logger is configured to only handle messages with a severity of WARNING |
| 416 | or above. The message format is also a configuration default, as is the output |
| 417 | destination of the messages - \code{sys.stderr}. The severity level, |
| 418 | the message format and destination can be easily changed, as shown in |
| 419 | the example below: |
| 420 | |
| 421 | \begin{verbatim} |
| 422 | import logging |
| 423 | |
| 424 | logging.basicConfig(level=logging.DEBUG, |
Vinay Sajip | e3c330b | 2004-07-07 15:59:49 +0000 | [diff] [blame] | 425 | format='%(asctime)s %(levelname)s %(message)s', |
| 426 | filename='/tmp/myapp.log', |
| 427 | filemode='w') |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 428 | logging.debug('A debug message') |
| 429 | logging.info('Some information') |
| 430 | logging.warning('A shot across the bows') |
| 431 | \end{verbatim} |
| 432 | |
| 433 | The \method{basicConfig()} method is used to change the configuration |
| 434 | defaults, which results in output (written to \code{/tmp/myapp.log}) |
| 435 | which should look something like the following: |
| 436 | |
| 437 | \begin{verbatim} |
| 438 | 2004-07-02 13:00:08,743 DEBUG A debug message |
| 439 | 2004-07-02 13:00:08,743 INFO Some information |
| 440 | 2004-07-02 13:00:08,743 WARNING A shot across the bows |
| 441 | \end{verbatim} |
| 442 | |
| 443 | This time, all messages with a severity of DEBUG or above were handled, |
| 444 | and the format of the messages was also changed, and output went to the |
| 445 | specified file rather than the console. |
| 446 | |
| 447 | Formatting uses standard Python string formatting - see section |
| 448 | \ref{typesseq-strings}. The format string takes the following |
| 449 | common specifiers. For a complete list of specifiers, consult the |
| 450 | \class{Formatter} documentation. |
| 451 | |
| 452 | \begin{tableii}{l|l}{code}{Format}{Description} |
| 453 | \lineii{\%(name)s} {Name of the logger (logging channel).} |
| 454 | \lineii{\%(levelname)s}{Text logging level for the message |
| 455 | (\code{'DEBUG'}, \code{'INFO'}, |
| 456 | \code{'WARNING'}, \code{'ERROR'}, |
| 457 | \code{'CRITICAL'}).} |
| 458 | \lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord} |
| 459 | was created. By default this is of the form |
| 460 | ``2003-07-08 16:49:45,896'' (the numbers after the |
| 461 | comma are millisecond portion of the time).} |
| 462 | \lineii{\%(message)s} {The logged message.} |
| 463 | \end{tableii} |
| 464 | |
| 465 | To change the date/time format, you can pass an additional keyword parameter, |
| 466 | \var{datefmt}, as in the following: |
| 467 | |
| 468 | \begin{verbatim} |
| 469 | import logging |
| 470 | |
| 471 | logging.basicConfig(level=logging.DEBUG, |
Vinay Sajip | e3c330b | 2004-07-07 15:59:49 +0000 | [diff] [blame] | 472 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 473 | datefmt='%a, %d %b %Y %H:%M:%S', |
| 474 | filename='/temp/myapp.log', |
| 475 | filemode='w') |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 476 | logging.debug('A debug message') |
| 477 | logging.info('Some information') |
| 478 | logging.warning('A shot across the bows') |
| 479 | \end{verbatim} |
| 480 | |
| 481 | which would result in output like |
| 482 | |
| 483 | \begin{verbatim} |
| 484 | Fri, 02 Jul 2004 13:06:18 DEBUG A debug message |
| 485 | Fri, 02 Jul 2004 13:06:18 INFO Some information |
| 486 | Fri, 02 Jul 2004 13:06:18 WARNING A shot across the bows |
| 487 | \end{verbatim} |
| 488 | |
| 489 | The date format string follows the requirements of \function{strftime()} - |
| 490 | see the documentation for the \refmodule{time} module. |
| 491 | |
| 492 | If, instead of sending logging output to the console or a file, you'd rather |
| 493 | use a file-like object which you have created separately, you can pass it |
| 494 | to \function{basicConfig()} using the \var{stream} keyword argument. Note |
| 495 | that if both \var{stream} and \var{filename} keyword arguments are passed, |
| 496 | the \var{stream} argument is ignored. |
| 497 | |
Vinay Sajip | b4bf62f | 2004-07-21 14:40:11 +0000 | [diff] [blame] | 498 | Of course, you can put variable information in your output. To do this, |
| 499 | simply have the message be a format string and pass in additional arguments |
| 500 | containing the variable information, as in the following example: |
| 501 | |
| 502 | \begin{verbatim} |
| 503 | import logging |
| 504 | |
| 505 | logging.basicConfig(level=logging.DEBUG, |
| 506 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 507 | datefmt='%a, %d %b %Y %H:%M:%S', |
| 508 | filename='/temp/myapp.log', |
| 509 | filemode='w') |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 510 | logging.error('Pack my box with %d dozen %s', 5, 'liquor jugs') |
Vinay Sajip | b4bf62f | 2004-07-21 14:40:11 +0000 | [diff] [blame] | 511 | \end{verbatim} |
| 512 | |
| 513 | which would result in |
| 514 | |
| 515 | \begin{verbatim} |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 516 | Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with 5 dozen liquor jugs |
Vinay Sajip | b4bf62f | 2004-07-21 14:40:11 +0000 | [diff] [blame] | 517 | \end{verbatim} |
| 518 | |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 519 | \subsection{Logging to multiple destinations \label{multiple-destinations}} |
| 520 | |
| 521 | Let's say you want to log to console and file with different message formats |
| 522 | and in differing circumstances. Say you want to log messages with levels |
| 523 | of DEBUG and higher to file, and those messages at level INFO and higher to |
| 524 | the console. Let's also assume that the file should contain timestamps, but |
| 525 | the console messages should not. Here's how you can achieve this: |
| 526 | |
| 527 | \begin{verbatim} |
| 528 | import logging |
| 529 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 530 | # set up logging to file - see previous section for more details |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 531 | logging.basicConfig(level=logging.DEBUG, |
| 532 | format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', |
| 533 | datefmt='%m-%d %H:%M', |
| 534 | filename='/temp/myapp.log', |
| 535 | filemode='w') |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 536 | # define a Handler which writes INFO messages or higher to the sys.stderr |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 537 | console = logging.StreamHandler() |
| 538 | console.setLevel(logging.INFO) |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 539 | # set a format which is simpler for console use |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 540 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 541 | # tell the handler to use this format |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 542 | console.setFormatter(formatter) |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 543 | # add the handler to the root logger |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 544 | logging.getLogger('').addHandler(console) |
| 545 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 546 | # Now, we can log to the root logger, or any other logger. First the root... |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 547 | logging.info('Jackdaws love my big sphinx of quartz.') |
| 548 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 549 | # Now, define a couple of other loggers which might represent areas in your |
| 550 | # application: |
Vinay Sajip | 93ae4c1 | 2004-10-22 21:43:15 +0000 | [diff] [blame] | 551 | |
| 552 | logger1 = logging.getLogger('myapp.area1') |
| 553 | logger2 = logging.getLogger('myapp.area2') |
| 554 | |
| 555 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') |
| 556 | logger1.info('How quickly daft jumping zebras vex.') |
| 557 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') |
| 558 | logger2.error('The five boxing wizards jump quickly.') |
| 559 | \end{verbatim} |
| 560 | |
| 561 | When you run this, on the console you will see |
| 562 | |
| 563 | \begin{verbatim} |
| 564 | root : INFO Jackdaws love my big sphinx of quartz. |
| 565 | myapp.area1 : INFO How quickly daft jumping zebras vex. |
| 566 | myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack. |
| 567 | myapp.area2 : ERROR The five boxing wizards jump quickly. |
| 568 | \end{verbatim} |
| 569 | |
| 570 | and in the file you will see something like |
| 571 | |
| 572 | \begin{verbatim} |
| 573 | 10-22 22:19 root INFO Jackdaws love my big sphinx of quartz. |
| 574 | 10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. |
| 575 | 10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex. |
| 576 | 10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. |
| 577 | 10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly. |
| 578 | \end{verbatim} |
| 579 | |
| 580 | As you can see, the DEBUG message only shows up in the file. The other |
| 581 | messages are sent to both destinations. |
| 582 | |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 583 | This example uses console and file handlers, but you can use any number and |
| 584 | combination of handlers you choose. |
| 585 | |
| 586 | \subsection{Sending and receiving logging events across a network |
| 587 | \label{network-logging}} |
| 588 | |
| 589 | Let's say you want to send logging events across a network, and handle them |
| 590 | at the receiving end. A simple way of doing this is attaching a |
| 591 | \class{SocketHandler} instance to the root logger at the sending end: |
| 592 | |
| 593 | \begin{verbatim} |
| 594 | import logging, logging.handlers |
| 595 | |
| 596 | rootLogger = logging.getLogger('') |
| 597 | rootLogger.setLevel(logging.DEBUG) |
| 598 | socketHandler = logging.handlers.SocketHandler('localhost', |
| 599 | logging.handlers.DEFAULT_TCP_LOGGING_PORT) |
| 600 | # don't bother with a formatter, since a socket handler sends the event as |
| 601 | # an unformatted pickle |
| 602 | rootLogger.addHandler(socketHandler) |
| 603 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 604 | # Now, we can log to the root logger, or any other logger. First the root... |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 605 | logging.info('Jackdaws love my big sphinx of quartz.') |
| 606 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 607 | # Now, define a couple of other loggers which might represent areas in your |
| 608 | # application: |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 609 | |
| 610 | logger1 = logging.getLogger('myapp.area1') |
| 611 | logger2 = logging.getLogger('myapp.area2') |
| 612 | |
| 613 | logger1.debug('Quick zephyrs blow, vexing daft Jim.') |
| 614 | logger1.info('How quickly daft jumping zebras vex.') |
| 615 | logger2.warning('Jail zesty vixen who grabbed pay from quack.') |
| 616 | logger2.error('The five boxing wizards jump quickly.') |
| 617 | \end{verbatim} |
| 618 | |
| 619 | At the receiving end, you can set up a receiver using the |
| 620 | \module{SocketServer} module. Here is a basic working example: |
| 621 | |
| 622 | \begin{verbatim} |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 623 | import cPickle |
| 624 | import logging |
| 625 | import logging.handlers |
| 626 | import SocketServer |
| 627 | import struct |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 628 | |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 629 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 630 | class LogRecordStreamHandler(SocketServer.StreamRequestHandler): |
| 631 | """Handler for a streaming logging request. |
| 632 | |
| 633 | This basically logs the record using whatever logging policy is |
| 634 | configured locally. |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 635 | """ |
| 636 | |
| 637 | def handle(self): |
| 638 | """ |
| 639 | Handle multiple requests - each expected to be a 4-byte length, |
| 640 | followed by the LogRecord in pickle format. Logs the record |
| 641 | according to whatever policy is configured locally. |
| 642 | """ |
| 643 | while 1: |
| 644 | chunk = self.connection.recv(4) |
| 645 | if len(chunk) < 4: |
| 646 | break |
| 647 | slen = struct.unpack(">L", chunk)[0] |
| 648 | chunk = self.connection.recv(slen) |
| 649 | while len(chunk) < slen: |
| 650 | chunk = chunk + self.connection.recv(slen - len(chunk)) |
| 651 | obj = self.unPickle(chunk) |
| 652 | record = logging.makeLogRecord(obj) |
| 653 | self.handleLogRecord(record) |
| 654 | |
| 655 | def unPickle(self, data): |
| 656 | return cPickle.loads(data) |
| 657 | |
| 658 | def handleLogRecord(self, record): |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 659 | # if a name is specified, we use the named logger rather than the one |
| 660 | # implied by the record. |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 661 | if self.server.logname is not None: |
| 662 | name = self.server.logname |
| 663 | else: |
| 664 | name = record.name |
| 665 | logger = logging.getLogger(name) |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 666 | # N.B. EVERY record gets logged. This is because Logger.handle |
| 667 | # is normally called AFTER logger-level filtering. If you want |
| 668 | # to do filtering, do it at the client end to save wasting |
| 669 | # cycles and network bandwidth! |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 670 | logger.handle(record) |
| 671 | |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 672 | class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer): |
| 673 | """simple TCP socket-based logging receiver suitable for testing. |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 674 | """ |
| 675 | |
| 676 | allow_reuse_address = 1 |
| 677 | |
| 678 | def __init__(self, host='localhost', |
Fred Drake | 048840c | 2004-10-29 14:35:42 +0000 | [diff] [blame] | 679 | port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, |
| 680 | handler=LogRecordStreamHandler): |
| 681 | SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler) |
Vinay Sajip | 006483b | 2004-10-29 12:30:28 +0000 | [diff] [blame] | 682 | self.abort = 0 |
| 683 | self.timeout = 1 |
| 684 | self.logname = None |
| 685 | |
| 686 | def serve_until_stopped(self): |
| 687 | import select |
| 688 | abort = 0 |
| 689 | while not abort: |
| 690 | rd, wr, ex = select.select([self.socket.fileno()], |
| 691 | [], [], |
| 692 | self.timeout) |
| 693 | if rd: |
| 694 | self.handle_request() |
| 695 | abort = self.abort |
| 696 | |
| 697 | def main(): |
| 698 | logging.basicConfig( |
| 699 | format="%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s", |
| 700 | datefmt="%H:%M:%S") |
| 701 | tcpserver = LogRecordSocketReceiver() |
| 702 | print "About to start TCP server..." |
| 703 | tcpserver.serve_until_stopped() |
| 704 | |
| 705 | if __name__ == "__main__": |
| 706 | main() |
| 707 | \end{verbatim} |
| 708 | |
| 709 | If you first run the server, and then the client. On the client side, nothing |
| 710 | is printed on the client console; on the server side, you should see something |
| 711 | like this: |
| 712 | |
| 713 | \begin{verbatim} |
| 714 | About to start TCP server... |
| 715 | 59 root INFO Jackdaws love my big sphinx of quartz. |
| 716 | 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. |
| 717 | 69 myapp.area1 INFO How quickly daft jumping zebras vex. |
| 718 | 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. |
| 719 | 69 myapp.area2 ERROR The five boxing wizards jump quickly. |
| 720 | \end{verbatim} |
| 721 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 722 | \subsection{Handler Objects} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 723 | |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 724 | Handlers have the following attributes and methods. Note that |
| 725 | \class{Handler} is never instantiated directly; this class acts as a |
| 726 | base for more useful subclasses. However, the \method{__init__()} |
| 727 | method in subclasses needs to call \method{Handler.__init__()}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 728 | |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 729 | \begin{methoddesc}{__init__}{level=\constant{NOTSET}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 730 | Initializes the \class{Handler} instance by setting its level, setting |
| 731 | the list of filters to the empty list and creating a lock (using |
Raymond Hettinger | c75c3e0 | 2003-09-01 22:50:52 +0000 | [diff] [blame] | 732 | \method{createLock()}) for serializing access to an I/O mechanism. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 733 | \end{methoddesc} |
| 734 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 735 | \begin{methoddesc}{createLock}{} |
| 736 | Initializes a thread lock which can be used to serialize access to |
| 737 | underlying I/O functionality which may not be threadsafe. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 738 | \end{methoddesc} |
| 739 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 740 | \begin{methoddesc}{acquire}{} |
| 741 | Acquires the thread lock created with \method{createLock()}. |
| 742 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 743 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 744 | \begin{methoddesc}{release}{} |
| 745 | Releases the thread lock acquired with \method{acquire()}. |
| 746 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 747 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 748 | \begin{methoddesc}{setLevel}{lvl} |
| 749 | Sets the threshold for this handler to \var{lvl}. Logging messages which are |
| 750 | less severe than \var{lvl} will be ignored. When a handler is created, the |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 751 | level is set to \constant{NOTSET} (which causes all messages to be processed). |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 752 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 753 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 754 | \begin{methoddesc}{setFormatter}{form} |
| 755 | Sets the \class{Formatter} for this handler to \var{form}. |
| 756 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 757 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 758 | \begin{methoddesc}{addFilter}{filt} |
| 759 | Adds the specified filter \var{filt} to this handler. |
| 760 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 761 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 762 | \begin{methoddesc}{removeFilter}{filt} |
| 763 | Removes the specified filter \var{filt} from this handler. |
| 764 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 765 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 766 | \begin{methoddesc}{filter}{record} |
| 767 | Applies this handler's filters to the record and returns a true value if |
| 768 | the record is to be processed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 769 | \end{methoddesc} |
| 770 | |
| 771 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 772 | Ensure all logging output has been flushed. This version does |
| 773 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 774 | \end{methoddesc} |
| 775 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 776 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 777 | Tidy up any resources used by the handler. This version does |
| 778 | nothing and is intended to be implemented by subclasses. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 779 | \end{methoddesc} |
| 780 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 781 | \begin{methoddesc}{handle}{record} |
| 782 | Conditionally emits the specified logging record, depending on |
| 783 | filters which may have been added to the handler. Wraps the actual |
| 784 | emission of the record with acquisition/release of the I/O thread |
| 785 | lock. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 786 | \end{methoddesc} |
| 787 | |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 788 | \begin{methoddesc}{handleError}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 789 | This method should be called from handlers when an exception is |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 790 | encountered during an \method{emit()} call. By default it does nothing, |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 791 | which means that exceptions get silently ignored. This is what is |
| 792 | mostly wanted for a logging system - most users will not care |
| 793 | about errors in the logging system, they are more interested in |
| 794 | application errors. You could, however, replace this with a custom |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 795 | handler if you wish. The specified record is the one which was being |
| 796 | processed when the exception occurred. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 797 | \end{methoddesc} |
| 798 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 799 | \begin{methoddesc}{format}{record} |
| 800 | Do formatting for a record - if a formatter is set, use it. |
| 801 | Otherwise, use the default formatter for the module. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 802 | \end{methoddesc} |
| 803 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 804 | \begin{methoddesc}{emit}{record} |
| 805 | Do whatever it takes to actually log the specified logging record. |
| 806 | This version is intended to be implemented by subclasses and so |
| 807 | raises a \exception{NotImplementedError}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 808 | \end{methoddesc} |
| 809 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 810 | \subsubsection{StreamHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 811 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 812 | The \class{StreamHandler} class sends logging output to streams such as |
| 813 | \var{sys.stdout}, \var{sys.stderr} or any file-like object (or, more |
| 814 | precisely, any object which supports \method{write()} and \method{flush()} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 815 | methods). |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 816 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 817 | \begin{classdesc}{StreamHandler}{\optional{strm}} |
| 818 | Returns a new instance of the \class{StreamHandler} class. If \var{strm} is |
| 819 | specified, the instance will use it for logging output; otherwise, |
| 820 | \var{sys.stderr} will be used. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 821 | \end{classdesc} |
| 822 | |
| 823 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 824 | If a formatter is specified, it is used to format the record. |
| 825 | The record is then written to the stream with a trailing newline. |
| 826 | If exception information is present, it is formatted using |
| 827 | \function{traceback.print_exception()} and appended to the stream. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 828 | \end{methoddesc} |
| 829 | |
| 830 | \begin{methoddesc}{flush}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 831 | Flushes the stream by calling its \method{flush()} method. Note that |
| 832 | the \method{close()} method is inherited from \class{Handler} and |
| 833 | so does nothing, so an explicit \method{flush()} call may be needed |
| 834 | at times. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 835 | \end{methoddesc} |
| 836 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 837 | \subsubsection{FileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 838 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 839 | The \class{FileHandler} class sends logging output to a disk file. |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 840 | It inherits the output functionality from \class{StreamHandler}. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 841 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 842 | \begin{classdesc}{FileHandler}{filename\optional{, mode}} |
| 843 | Returns a new instance of the \class{FileHandler} class. The specified |
| 844 | file is opened and used as the stream for logging. If \var{mode} is |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 845 | not specified, \constant{'a'} is used. By default, the file grows |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 846 | indefinitely. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 847 | \end{classdesc} |
| 848 | |
| 849 | \begin{methoddesc}{close}{} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 850 | Closes the file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 851 | \end{methoddesc} |
| 852 | |
| 853 | \begin{methoddesc}{emit}{record} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 854 | Outputs the record to the file. |
| 855 | \end{methoddesc} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 856 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 857 | \subsubsection{RotatingFileHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 858 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 859 | The \class{RotatingFileHandler} class supports rotation of disk log files. |
| 860 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 861 | \begin{classdesc}{RotatingFileHandler}{filename\optional{, mode\optional{, |
| 862 | maxBytes\optional{, backupCount}}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 863 | Returns a new instance of the \class{RotatingFileHandler} class. The |
| 864 | specified file is opened and used as the stream for logging. If |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 865 | \var{mode} is not specified, \code{'a'} is used. By default, the |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 866 | file grows indefinitely. |
Andrew M. Kuchling | 7cf4d9b | 2003-09-26 13:45:18 +0000 | [diff] [blame] | 867 | |
| 868 | You can use the \var{maxBytes} and |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 869 | \var{backupCount} values to allow the file to \dfn{rollover} at a |
| 870 | predetermined size. When the size is about to be exceeded, the file is |
Andrew M. Kuchling | 7cf4d9b | 2003-09-26 13:45:18 +0000 | [diff] [blame] | 871 | closed and a new file is silently opened for output. Rollover occurs |
| 872 | whenever the current log file is nearly \var{maxBytes} in length; if |
| 873 | \var{maxBytes} is zero, rollover never occurs. If \var{backupCount} |
| 874 | is non-zero, the system will save old log files by appending the |
| 875 | extensions ".1", ".2" etc., to the filename. For example, with |
| 876 | a \var{backupCount} of 5 and a base file name of |
| 877 | \file{app.log}, you would get \file{app.log}, |
| 878 | \file{app.log.1}, \file{app.log.2}, up to \file{app.log.5}. The file being |
| 879 | written to is always \file{app.log}. When this file is filled, it is |
| 880 | closed and renamed to \file{app.log.1}, and if files \file{app.log.1}, |
| 881 | \file{app.log.2}, etc. exist, then they are renamed to \file{app.log.2}, |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 882 | \file{app.log.3} etc. respectively. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 883 | \end{classdesc} |
| 884 | |
| 885 | \begin{methoddesc}{doRollover}{} |
| 886 | Does a rollover, as described above. |
| 887 | \end{methoddesc} |
| 888 | |
| 889 | \begin{methoddesc}{emit}{record} |
| 890 | Outputs the record to the file, catering for rollover as described |
| 891 | in \method{setRollover()}. |
| 892 | \end{methoddesc} |
| 893 | |
| 894 | \subsubsection{SocketHandler} |
| 895 | |
| 896 | The \class{SocketHandler} class sends logging output to a network |
| 897 | socket. The base class uses a TCP socket. |
| 898 | |
| 899 | \begin{classdesc}{SocketHandler}{host, port} |
| 900 | Returns a new instance of the \class{SocketHandler} class intended to |
| 901 | communicate with a remote machine whose address is given by \var{host} |
| 902 | and \var{port}. |
| 903 | \end{classdesc} |
| 904 | |
| 905 | \begin{methoddesc}{close}{} |
| 906 | Closes the socket. |
| 907 | \end{methoddesc} |
| 908 | |
| 909 | \begin{methoddesc}{handleError}{} |
| 910 | \end{methoddesc} |
| 911 | |
| 912 | \begin{methoddesc}{emit}{} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 913 | Pickles the record's attribute dictionary and writes it to the socket in |
| 914 | binary format. If there is an error with the socket, silently drops the |
| 915 | packet. If the connection was previously lost, re-establishes the connection. |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 916 | To unpickle the record at the receiving end into a \class{LogRecord}, use the |
| 917 | \function{makeLogRecord()} function. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 918 | \end{methoddesc} |
| 919 | |
| 920 | \begin{methoddesc}{handleError}{} |
| 921 | Handles an error which has occurred during \method{emit()}. The |
| 922 | most likely cause is a lost connection. Closes the socket so that |
| 923 | we can retry on the next event. |
| 924 | \end{methoddesc} |
| 925 | |
| 926 | \begin{methoddesc}{makeSocket}{} |
| 927 | This is a factory method which allows subclasses to define the precise |
| 928 | type of socket they want. The default implementation creates a TCP |
| 929 | socket (\constant{socket.SOCK_STREAM}). |
| 930 | \end{methoddesc} |
| 931 | |
| 932 | \begin{methoddesc}{makePickle}{record} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 933 | Pickles the record's attribute dictionary in binary format with a length |
| 934 | prefix, and returns it ready for transmission across the socket. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 935 | \end{methoddesc} |
| 936 | |
| 937 | \begin{methoddesc}{send}{packet} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 938 | Send a pickled string \var{packet} to the socket. This function allows |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 939 | for partial sends which can happen when the network is busy. |
| 940 | \end{methoddesc} |
| 941 | |
| 942 | \subsubsection{DatagramHandler} |
| 943 | |
| 944 | The \class{DatagramHandler} class inherits from \class{SocketHandler} |
| 945 | to support sending logging messages over UDP sockets. |
| 946 | |
| 947 | \begin{classdesc}{DatagramHandler}{host, port} |
| 948 | Returns a new instance of the \class{DatagramHandler} class intended to |
| 949 | communicate with a remote machine whose address is given by \var{host} |
| 950 | and \var{port}. |
| 951 | \end{classdesc} |
| 952 | |
| 953 | \begin{methoddesc}{emit}{} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 954 | Pickles the record's attribute dictionary and writes it to the socket in |
| 955 | binary format. If there is an error with the socket, silently drops the |
| 956 | packet. |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 957 | To unpickle the record at the receiving end into a \class{LogRecord}, use the |
| 958 | \function{makeLogRecord()} function. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 959 | \end{methoddesc} |
| 960 | |
| 961 | \begin{methoddesc}{makeSocket}{} |
| 962 | The factory method of \class{SocketHandler} is here overridden to create |
| 963 | a UDP socket (\constant{socket.SOCK_DGRAM}). |
| 964 | \end{methoddesc} |
| 965 | |
| 966 | \begin{methoddesc}{send}{s} |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 967 | Send a pickled string to a socket. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 968 | \end{methoddesc} |
| 969 | |
| 970 | \subsubsection{SysLogHandler} |
| 971 | |
| 972 | The \class{SysLogHandler} class supports sending logging messages to a |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 973 | remote or local \UNIX{} syslog. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 974 | |
| 975 | \begin{classdesc}{SysLogHandler}{\optional{address\optional{, facility}}} |
| 976 | Returns a new instance of the \class{SysLogHandler} class intended to |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 977 | communicate with a remote \UNIX{} machine whose address is given by |
| 978 | \var{address} in the form of a \code{(\var{host}, \var{port})} |
| 979 | tuple. If \var{address} is not specified, \code{('localhost', 514)} is |
| 980 | used. The address is used to open a UDP socket. If \var{facility} is |
| 981 | not specified, \constant{LOG_USER} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 982 | \end{classdesc} |
| 983 | |
| 984 | \begin{methoddesc}{close}{} |
| 985 | Closes the socket to the remote host. |
| 986 | \end{methoddesc} |
| 987 | |
| 988 | \begin{methoddesc}{emit}{record} |
| 989 | The record is formatted, and then sent to the syslog server. If |
| 990 | exception information is present, it is \emph{not} sent to the server. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 991 | \end{methoddesc} |
| 992 | |
| 993 | \begin{methoddesc}{encodePriority}{facility, priority} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 994 | Encodes the facility and priority into an integer. You can pass in strings |
| 995 | or integers - if strings are passed, internal mapping dictionaries are used |
| 996 | to convert them to integers. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 997 | \end{methoddesc} |
| 998 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 999 | \subsubsection{NTEventLogHandler} |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1000 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1001 | The \class{NTEventLogHandler} class supports sending logging messages |
| 1002 | to a local Windows NT, Windows 2000 or Windows XP event log. Before |
| 1003 | you can use it, you need Mark Hammond's Win32 extensions for Python |
| 1004 | installed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1005 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1006 | \begin{classdesc}{NTEventLogHandler}{appname\optional{, |
| 1007 | dllname\optional{, logtype}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1008 | Returns a new instance of the \class{NTEventLogHandler} class. The |
| 1009 | \var{appname} is used to define the application name as it appears in the |
| 1010 | event log. An appropriate registry entry is created using this name. |
| 1011 | The \var{dllname} should give the fully qualified pathname of a .dll or .exe |
| 1012 | which contains message definitions to hold in the log (if not specified, |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1013 | \code{'win32service.pyd'} is used - this is installed with the Win32 |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1014 | extensions and contains some basic placeholder message definitions. |
| 1015 | Note that use of these placeholders will make your event logs big, as the |
| 1016 | entire message source is held in the log. If you want slimmer logs, you have |
| 1017 | to pass in the name of your own .dll or .exe which contains the message |
| 1018 | definitions you want to use in the event log). The \var{logtype} is one of |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1019 | \code{'Application'}, \code{'System'} or \code{'Security'}, and |
| 1020 | defaults to \code{'Application'}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1021 | \end{classdesc} |
| 1022 | |
| 1023 | \begin{methoddesc}{close}{} |
| 1024 | At this point, you can remove the application name from the registry as a |
| 1025 | source of event log entries. However, if you do this, you will not be able |
| 1026 | to see the events as you intended in the Event Log Viewer - it needs to be |
| 1027 | able to access the registry to get the .dll name. The current version does |
| 1028 | not do this (in fact it doesn't do anything). |
| 1029 | \end{methoddesc} |
| 1030 | |
| 1031 | \begin{methoddesc}{emit}{record} |
| 1032 | Determines the message ID, event category and event type, and then logs the |
| 1033 | message in the NT event log. |
| 1034 | \end{methoddesc} |
| 1035 | |
| 1036 | \begin{methoddesc}{getEventCategory}{record} |
| 1037 | Returns the event category for the record. Override this if you |
| 1038 | want to specify your own categories. This version returns 0. |
| 1039 | \end{methoddesc} |
| 1040 | |
| 1041 | \begin{methoddesc}{getEventType}{record} |
| 1042 | Returns the event type for the record. Override this if you want |
| 1043 | to specify your own types. This version does a mapping using the |
| 1044 | handler's typemap attribute, which is set up in \method{__init__()} |
| 1045 | to a dictionary which contains mappings for \constant{DEBUG}, |
| 1046 | \constant{INFO}, \constant{WARNING}, \constant{ERROR} and |
| 1047 | \constant{CRITICAL}. If you are using your own levels, you will either need |
| 1048 | to override this method or place a suitable dictionary in the |
| 1049 | handler's \var{typemap} attribute. |
| 1050 | \end{methoddesc} |
| 1051 | |
| 1052 | \begin{methoddesc}{getMessageID}{record} |
| 1053 | Returns the message ID for the record. If you are using your |
| 1054 | own messages, you could do this by having the \var{msg} passed to the |
| 1055 | logger being an ID rather than a format string. Then, in here, |
| 1056 | you could use a dictionary lookup to get the message ID. This |
| 1057 | version returns 1, which is the base message ID in |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1058 | \file{win32service.pyd}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1059 | \end{methoddesc} |
| 1060 | |
| 1061 | \subsubsection{SMTPHandler} |
| 1062 | |
| 1063 | The \class{SMTPHandler} class supports sending logging messages to an email |
| 1064 | address via SMTP. |
| 1065 | |
| 1066 | \begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddrs, subject} |
| 1067 | Returns a new instance of the \class{SMTPHandler} class. The |
| 1068 | instance is initialized with the from and to addresses and subject |
| 1069 | line of the email. The \var{toaddrs} should be a list of strings without |
| 1070 | domain names (That's what the \var{mailhost} is for). To specify a |
| 1071 | non-standard SMTP port, use the (host, port) tuple format for the |
| 1072 | \var{mailhost} argument. If you use a string, the standard SMTP port |
| 1073 | is used. |
| 1074 | \end{classdesc} |
| 1075 | |
| 1076 | \begin{methoddesc}{emit}{record} |
| 1077 | Formats the record and sends it to the specified addressees. |
| 1078 | \end{methoddesc} |
| 1079 | |
| 1080 | \begin{methoddesc}{getSubject}{record} |
| 1081 | If you want to specify a subject line which is record-dependent, |
| 1082 | override this method. |
| 1083 | \end{methoddesc} |
| 1084 | |
| 1085 | \subsubsection{MemoryHandler} |
| 1086 | |
| 1087 | The \class{MemoryHandler} supports buffering of logging records in memory, |
| 1088 | periodically flushing them to a \dfn{target} handler. Flushing occurs |
| 1089 | whenever the buffer is full, or when an event of a certain severity or |
| 1090 | greater is seen. |
| 1091 | |
| 1092 | \class{MemoryHandler} is a subclass of the more general |
| 1093 | \class{BufferingHandler}, which is an abstract class. This buffers logging |
| 1094 | records in memory. Whenever each record is added to the buffer, a |
| 1095 | check is made by calling \method{shouldFlush()} to see if the buffer |
| 1096 | should be flushed. If it should, then \method{flush()} is expected to |
| 1097 | do the needful. |
| 1098 | |
| 1099 | \begin{classdesc}{BufferingHandler}{capacity} |
| 1100 | Initializes the handler with a buffer of the specified capacity. |
| 1101 | \end{classdesc} |
| 1102 | |
| 1103 | \begin{methoddesc}{emit}{record} |
| 1104 | Appends the record to the buffer. If \method{shouldFlush()} returns true, |
| 1105 | calls \method{flush()} to process the buffer. |
| 1106 | \end{methoddesc} |
| 1107 | |
| 1108 | \begin{methoddesc}{flush}{} |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 1109 | You can override this to implement custom flushing behavior. This version |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1110 | just zaps the buffer to empty. |
| 1111 | \end{methoddesc} |
| 1112 | |
| 1113 | \begin{methoddesc}{shouldFlush}{record} |
| 1114 | Returns true if the buffer is up to capacity. This method can be |
| 1115 | overridden to implement custom flushing strategies. |
| 1116 | \end{methoddesc} |
| 1117 | |
| 1118 | \begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel |
Neal Norwitz | 6fa635d | 2003-02-18 14:20:07 +0000 | [diff] [blame] | 1119 | \optional{, target}}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1120 | Returns a new instance of the \class{MemoryHandler} class. The |
| 1121 | instance is initialized with a buffer size of \var{capacity}. If |
| 1122 | \var{flushLevel} is not specified, \constant{ERROR} is used. If no |
| 1123 | \var{target} is specified, the target will need to be set using |
| 1124 | \method{setTarget()} before this handler does anything useful. |
| 1125 | \end{classdesc} |
| 1126 | |
| 1127 | \begin{methoddesc}{close}{} |
| 1128 | Calls \method{flush()}, sets the target to \constant{None} and |
| 1129 | clears the buffer. |
| 1130 | \end{methoddesc} |
| 1131 | |
| 1132 | \begin{methoddesc}{flush}{} |
| 1133 | For a \class{MemoryHandler}, flushing means just sending the buffered |
| 1134 | records to the target, if there is one. Override if you want |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 1135 | different behavior. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1136 | \end{methoddesc} |
| 1137 | |
| 1138 | \begin{methoddesc}{setTarget}{target} |
| 1139 | Sets the target handler for this handler. |
| 1140 | \end{methoddesc} |
| 1141 | |
| 1142 | \begin{methoddesc}{shouldFlush}{record} |
| 1143 | Checks for buffer full or a record at the \var{flushLevel} or higher. |
| 1144 | \end{methoddesc} |
| 1145 | |
| 1146 | \subsubsection{HTTPHandler} |
| 1147 | |
| 1148 | The \class{HTTPHandler} class supports sending logging messages to a |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 1149 | Web server, using either \samp{GET} or \samp{POST} semantics. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1150 | |
| 1151 | \begin{classdesc}{HTTPHandler}{host, url\optional{, method}} |
| 1152 | Returns a new instance of the \class{HTTPHandler} class. The |
| 1153 | instance is initialized with a host address, url and HTTP method. |
Fred Drake | 68e6d57 | 2003-01-28 22:02:35 +0000 | [diff] [blame] | 1154 | If no \var{method} is specified, \samp{GET} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1155 | \end{classdesc} |
| 1156 | |
| 1157 | \begin{methoddesc}{emit}{record} |
| 1158 | Sends the record to the Web server as an URL-encoded dictionary. |
| 1159 | \end{methoddesc} |
| 1160 | |
| 1161 | \subsection{Formatter Objects} |
| 1162 | |
| 1163 | \class{Formatter}s have the following attributes and methods. They are |
| 1164 | responsible for converting a \class{LogRecord} to (usually) a string |
| 1165 | which can be interpreted by either a human or an external system. The |
| 1166 | base |
| 1167 | \class{Formatter} allows a formatting string to be specified. If none is |
Fred Drake | 8efc74d | 2004-04-15 06:18:48 +0000 | [diff] [blame] | 1168 | supplied, the default value of \code{'\%(message)s'} is used. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1169 | |
| 1170 | A Formatter can be initialized with a format string which makes use of |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 1171 | knowledge of the \class{LogRecord} attributes - such as the default value |
| 1172 | mentioned above making use of the fact that the user's message and |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1173 | arguments are pre-formatted into a \class{LogRecord}'s \var{message} |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 1174 | attribute. This format string contains standard python \%-style |
| 1175 | mapping keys. See section \ref{typesseq-strings}, ``String Formatting |
| 1176 | Operations,'' for more information on string formatting. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1177 | |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1178 | Currently, the useful mapping keys in a \class{LogRecord} are: |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 1179 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1180 | \begin{tableii}{l|l}{code}{Format}{Description} |
| 1181 | \lineii{\%(name)s} {Name of the logger (logging channel).} |
| 1182 | \lineii{\%(levelno)s} {Numeric logging level for the message |
| 1183 | (\constant{DEBUG}, \constant{INFO}, |
| 1184 | \constant{WARNING}, \constant{ERROR}, |
| 1185 | \constant{CRITICAL}).} |
| 1186 | \lineii{\%(levelname)s}{Text logging level for the message |
| 1187 | (\code{'DEBUG'}, \code{'INFO'}, |
| 1188 | \code{'WARNING'}, \code{'ERROR'}, |
| 1189 | \code{'CRITICAL'}).} |
| 1190 | \lineii{\%(pathname)s} {Full pathname of the source file where the logging |
| 1191 | call was issued (if available).} |
| 1192 | \lineii{\%(filename)s} {Filename portion of pathname.} |
| 1193 | \lineii{\%(module)s} {Module (name portion of filename).} |
| 1194 | \lineii{\%(lineno)d} {Source line number where the logging call was issued |
| 1195 | (if available).} |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1196 | \lineii{\%(created)f} {Time when the \class{LogRecord} was created (as |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1197 | returned by \function{time.time()}).} |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1198 | \lineii{\%(asctime)s} {Human-readable time when the \class{LogRecord} |
| 1199 | was created. By default this is of the form |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1200 | ``2003-07-08 16:49:45,896'' (the numbers after the |
| 1201 | comma are millisecond portion of the time).} |
| 1202 | \lineii{\%(msecs)d} {Millisecond portion of the time when the |
| 1203 | \class{LogRecord} was created.} |
| 1204 | \lineii{\%(thread)d} {Thread ID (if available).} |
| 1205 | \lineii{\%(process)d} {Process ID (if available).} |
| 1206 | \lineii{\%(message)s} {The logged message, computed as \code{msg \% args}.} |
Anthony Baxter | a6b7d34 | 2003-07-08 08:40:20 +0000 | [diff] [blame] | 1207 | \end{tableii} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1208 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1209 | \begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}} |
| 1210 | Returns a new instance of the \class{Formatter} class. The |
| 1211 | instance is initialized with a format string for the message as a whole, |
| 1212 | as well as a format string for the date/time portion of a message. If |
Neal Norwitz | dd3afa7 | 2003-07-08 16:26:34 +0000 | [diff] [blame] | 1213 | no \var{fmt} is specified, \code{'\%(message)s'} is used. If no \var{datefmt} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1214 | is specified, the ISO8601 date format is used. |
| 1215 | \end{classdesc} |
| 1216 | |
| 1217 | \begin{methoddesc}{format}{record} |
| 1218 | The record's attribute dictionary is used as the operand to a |
| 1219 | string formatting operation. Returns the resulting string. |
| 1220 | Before formatting the dictionary, a couple of preparatory steps |
| 1221 | are carried out. The \var{message} attribute of the record is computed |
| 1222 | using \var{msg} \% \var{args}. If the formatting string contains |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1223 | \code{'(asctime)'}, \method{formatTime()} is called to format the |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1224 | event time. If there is exception information, it is formatted using |
| 1225 | \method{formatException()} and appended to the message. |
| 1226 | \end{methoddesc} |
| 1227 | |
| 1228 | \begin{methoddesc}{formatTime}{record\optional{, datefmt}} |
| 1229 | This method should be called from \method{format()} by a formatter which |
| 1230 | wants to make use of a formatted time. This method can be overridden |
| 1231 | in formatters to provide for any specific requirement, but the |
Raymond Hettinger | 2ef85a7 | 2003-01-25 21:46:53 +0000 | [diff] [blame] | 1232 | basic behavior is as follows: if \var{datefmt} (a string) is specified, |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 1233 | 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] | 1234 | record. Otherwise, the ISO8601 format is used. The resulting |
| 1235 | string is returned. |
| 1236 | \end{methoddesc} |
| 1237 | |
| 1238 | \begin{methoddesc}{formatException}{exc_info} |
| 1239 | Formats the specified exception information (a standard exception tuple |
Fred Drake | c23e019 | 2003-01-28 22:09:16 +0000 | [diff] [blame] | 1240 | as returned by \function{sys.exc_info()}) as a string. This default |
| 1241 | implementation just uses \function{traceback.print_exception()}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1242 | The resulting string is returned. |
| 1243 | \end{methoddesc} |
| 1244 | |
| 1245 | \subsection{Filter Objects} |
| 1246 | |
| 1247 | \class{Filter}s can be used by \class{Handler}s and \class{Logger}s for |
| 1248 | more sophisticated filtering than is provided by levels. The base filter |
| 1249 | class only allows events which are below a certain point in the logger |
| 1250 | hierarchy. For example, a filter initialized with "A.B" will allow events |
| 1251 | logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB", |
| 1252 | "B.A.B" etc. If initialized with the empty string, all events are passed. |
| 1253 | |
| 1254 | \begin{classdesc}{Filter}{\optional{name}} |
| 1255 | Returns an instance of the \class{Filter} class. If \var{name} is specified, |
| 1256 | it names a logger which, together with its children, will have its events |
| 1257 | allowed through the filter. If no name is specified, allows every event. |
| 1258 | \end{classdesc} |
| 1259 | |
| 1260 | \begin{methoddesc}{filter}{record} |
| 1261 | Is the specified record to be logged? Returns zero for no, nonzero for |
| 1262 | yes. If deemed appropriate, the record may be modified in-place by this |
| 1263 | method. |
| 1264 | \end{methoddesc} |
| 1265 | |
| 1266 | \subsection{LogRecord Objects} |
| 1267 | |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1268 | \class{LogRecord} instances are created every time something is logged. They |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1269 | contain all the information pertinent to the event being logged. The |
| 1270 | main information passed in is in msg and args, which are combined |
| 1271 | using msg \% args to create the message field of the record. The record |
| 1272 | also includes information such as when the record was created, the |
| 1273 | source line where the logging call was made, and any exception |
| 1274 | information to be logged. |
| 1275 | |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1276 | \class{LogRecord} has no methods; it's just a repository for |
| 1277 | information about the logging event. The only reason it's a class |
| 1278 | rather than a dictionary is to facilitate extension. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1279 | |
| 1280 | \begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args, |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1281 | exc_info} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1282 | Returns an instance of \class{LogRecord} initialized with interesting |
| 1283 | information. The \var{name} is the logger name; \var{lvl} is the |
| 1284 | numeric level; \var{pathname} is the absolute pathname of the source |
| 1285 | file in which the logging call was made; \var{lineno} is the line |
| 1286 | number in that file where the logging call is found; \var{msg} is the |
| 1287 | user-supplied message (a format string); \var{args} is the tuple |
| 1288 | which, together with \var{msg}, makes up the user message; and |
| 1289 | \var{exc_info} is the exception tuple obtained by calling |
| 1290 | \function{sys.exc_info() }(or \constant{None}, if no exception information |
| 1291 | is available). |
| 1292 | \end{classdesc} |
| 1293 | |
| 1294 | \subsection{Thread Safety} |
| 1295 | |
| 1296 | The logging module is intended to be thread-safe without any special work |
| 1297 | needing to be done by its clients. It achieves this though using threading |
| 1298 | locks; there is one lock to serialize access to the module's shared data, |
| 1299 | and each handler also creates a lock to serialize access to its underlying |
| 1300 | I/O. |
| 1301 | |
| 1302 | \subsection{Configuration} |
| 1303 | |
| 1304 | |
Fred Drake | 94ffbb7 | 2004-04-08 19:44:31 +0000 | [diff] [blame] | 1305 | \subsubsection{Configuration functions% |
| 1306 | \label{logging-config-api}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1307 | |
Fred Drake | 9a5b6a6 | 2003-07-08 15:38:40 +0000 | [diff] [blame] | 1308 | The following functions allow the logging module to be |
| 1309 | configured. Before they can be used, you must import |
| 1310 | \module{logging.config}. Their use is optional --- you can configure |
| 1311 | the logging module entirely by making calls to the main API (defined |
| 1312 | in \module{logging} itself) and defining handlers which are declared |
Raymond Hettinger | 6f3eaa6 | 2003-06-27 21:43:39 +0000 | [diff] [blame] | 1313 | either in \module{logging} or \module{logging.handlers}. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1314 | |
| 1315 | \begin{funcdesc}{fileConfig}{fname\optional{, defaults}} |
| 1316 | Reads the logging configuration from a ConfigParser-format file named |
| 1317 | \var{fname}. This function can be called several times from an application, |
| 1318 | allowing an end user the ability to select from various pre-canned |
| 1319 | configurations (if the developer provides a mechanism to present the |
| 1320 | choices and load the chosen configuration). Defaults to be passed to |
| 1321 | ConfigParser can be specified in the \var{defaults} argument. |
| 1322 | \end{funcdesc} |
| 1323 | |
| 1324 | \begin{funcdesc}{listen}{\optional{port}} |
| 1325 | Starts up a socket server on the specified port, and listens for new |
| 1326 | configurations. If no port is specified, the module's default |
| 1327 | \constant{DEFAULT_LOGGING_CONFIG_PORT} is used. Logging configurations |
| 1328 | will be sent as a file suitable for processing by \function{fileConfig()}. |
| 1329 | Returns a \class{Thread} instance on which you can call \method{start()} |
| 1330 | to start the server, and which you can \method{join()} when appropriate. |
| 1331 | To stop the server, call \function{stopListening()}. |
| 1332 | \end{funcdesc} |
| 1333 | |
| 1334 | \begin{funcdesc}{stopListening}{} |
| 1335 | Stops the listening server which was created with a call to |
| 1336 | \function{listen()}. This is typically called before calling \method{join()} |
| 1337 | on the return value from \function{listen()}. |
| 1338 | \end{funcdesc} |
| 1339 | |
Fred Drake | 94ffbb7 | 2004-04-08 19:44:31 +0000 | [diff] [blame] | 1340 | \subsubsection{Configuration file format% |
| 1341 | \label{logging-config-fileformat}} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1342 | |
Fred Drake | 6b3b046 | 2004-04-09 18:26:40 +0000 | [diff] [blame] | 1343 | The configuration file format understood by \function{fileConfig()} is |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1344 | based on ConfigParser functionality. The file must contain sections |
| 1345 | called \code{[loggers]}, \code{[handlers]} and \code{[formatters]} |
| 1346 | which identify by name the entities of each type which are defined in |
| 1347 | the file. For each such entity, there is a separate section which |
| 1348 | identified how that entity is configured. Thus, for a logger named |
| 1349 | \code{log01} in the \code{[loggers]} section, the relevant |
| 1350 | configuration details are held in a section |
| 1351 | \code{[logger_log01]}. Similarly, a handler called \code{hand01} in |
| 1352 | the \code{[handlers]} section will have its configuration held in a |
| 1353 | section called \code{[handler_hand01]}, while a formatter called |
| 1354 | \code{form01} in the \code{[formatters]} section will have its |
| 1355 | configuration specified in a section called |
| 1356 | \code{[formatter_form01]}. The root logger configuration must be |
| 1357 | specified in a section called \code{[logger_root]}. |
| 1358 | |
| 1359 | Examples of these sections in the file are given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1360 | |
| 1361 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1362 | [loggers] |
| 1363 | keys=root,log02,log03,log04,log05,log06,log07 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1364 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1365 | [handlers] |
| 1366 | keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09 |
| 1367 | |
| 1368 | [formatters] |
| 1369 | keys=form01,form02,form03,form04,form05,form06,form07,form08,form09 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1370 | \end{verbatim} |
| 1371 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1372 | The root logger must specify a level and a list of handlers. An |
| 1373 | example of a root logger section is given below. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1374 | |
| 1375 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1376 | [logger_root] |
| 1377 | level=NOTSET |
| 1378 | handlers=hand01 |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1379 | \end{verbatim} |
| 1380 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1381 | The \code{level} entry can be one of \code{DEBUG, INFO, WARNING, |
| 1382 | ERROR, CRITICAL} or \code{NOTSET}. For the root logger only, |
| 1383 | \code{NOTSET} means that all messages will be logged. Level values are |
| 1384 | \function{eval()}uated in the context of the \code{logging} package's |
| 1385 | namespace. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1386 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1387 | The \code{handlers} entry is a comma-separated list of handler names, |
| 1388 | which must appear in the \code{[handlers]} section. These names must |
| 1389 | appear in the \code{[handlers]} section and have corresponding |
| 1390 | sections in the configuration file. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1391 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1392 | For loggers other than the root logger, some additional information is |
| 1393 | required. This is illustrated by the following example. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1394 | |
| 1395 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1396 | [logger_parser] |
| 1397 | level=DEBUG |
| 1398 | handlers=hand01 |
| 1399 | propagate=1 |
| 1400 | qualname=compiler.parser |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1401 | \end{verbatim} |
| 1402 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1403 | The \code{level} and \code{handlers} entries are interpreted as for |
| 1404 | the root logger, except that if a non-root logger's level is specified |
| 1405 | as \code{NOTSET}, the system consults loggers higher up the hierarchy |
| 1406 | to determine the effective level of the logger. The \code{propagate} |
| 1407 | entry is set to 1 to indicate that messages must propagate to handlers |
| 1408 | higher up the logger hierarchy from this logger, or 0 to indicate that |
| 1409 | messages are \strong{not} propagated to handlers up the hierarchy. The |
| 1410 | \code{qualname} entry is the hierarchical channel name of the logger, |
Vinay Sajip | a13c60b | 2004-07-03 11:45:53 +0000 | [diff] [blame] | 1411 | that is to say the name used by the application to get the logger. |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1412 | |
| 1413 | Sections which specify handler configuration are exemplified by the |
| 1414 | following. |
| 1415 | |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1416 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1417 | [handler_hand01] |
| 1418 | class=StreamHandler |
| 1419 | level=NOTSET |
| 1420 | formatter=form01 |
| 1421 | args=(sys.stdout,) |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1422 | \end{verbatim} |
| 1423 | |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1424 | The \code{class} entry indicates the handler's class (as determined by |
| 1425 | \function{eval()} in the \code{logging} package's namespace). The |
| 1426 | \code{level} is interpreted as for loggers, and \code{NOTSET} is taken |
| 1427 | to mean "log everything". |
| 1428 | |
| 1429 | The \code{formatter} entry indicates the key name of the formatter for |
| 1430 | this handler. If blank, a default formatter |
| 1431 | (\code{logging._defaultFormatter}) is used. If a name is specified, it |
| 1432 | must appear in the \code{[formatters]} section and have a |
| 1433 | corresponding section in the configuration file. |
| 1434 | |
| 1435 | The \code{args} entry, when \function{eval()}uated in the context of |
| 1436 | the \code{logging} package's namespace, is the list of arguments to |
| 1437 | the constructor for the handler class. Refer to the constructors for |
| 1438 | the relevant handlers, or to the examples below, to see how typical |
| 1439 | entries are constructed. |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1440 | |
| 1441 | \begin{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1442 | [handler_hand02] |
| 1443 | class=FileHandler |
| 1444 | level=DEBUG |
| 1445 | formatter=form02 |
| 1446 | args=('python.log', 'w') |
| 1447 | |
| 1448 | [handler_hand03] |
| 1449 | class=handlers.SocketHandler |
| 1450 | level=INFO |
| 1451 | formatter=form03 |
| 1452 | args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT) |
| 1453 | |
| 1454 | [handler_hand04] |
| 1455 | class=handlers.DatagramHandler |
| 1456 | level=WARN |
| 1457 | formatter=form04 |
| 1458 | args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT) |
| 1459 | |
| 1460 | [handler_hand05] |
| 1461 | class=handlers.SysLogHandler |
| 1462 | level=ERROR |
| 1463 | formatter=form05 |
| 1464 | args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER) |
| 1465 | |
| 1466 | [handler_hand06] |
Vinay Sajip | 20f42c4 | 2004-07-12 15:48:04 +0000 | [diff] [blame] | 1467 | class=handlers.NTEventLogHandler |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1468 | level=CRITICAL |
| 1469 | formatter=form06 |
| 1470 | args=('Python Application', '', 'Application') |
| 1471 | |
| 1472 | [handler_hand07] |
Vinay Sajip | 20f42c4 | 2004-07-12 15:48:04 +0000 | [diff] [blame] | 1473 | class=handlers.SMTPHandler |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1474 | level=WARN |
| 1475 | formatter=form07 |
| 1476 | args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject') |
| 1477 | |
| 1478 | [handler_hand08] |
Vinay Sajip | 20f42c4 | 2004-07-12 15:48:04 +0000 | [diff] [blame] | 1479 | class=handlers.MemoryHandler |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1480 | level=NOTSET |
| 1481 | formatter=form08 |
| 1482 | target= |
| 1483 | args=(10, ERROR) |
| 1484 | |
| 1485 | [handler_hand09] |
Vinay Sajip | 20f42c4 | 2004-07-12 15:48:04 +0000 | [diff] [blame] | 1486 | class=handlers.HTTPHandler |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1487 | level=NOTSET |
| 1488 | formatter=form09 |
| 1489 | args=('localhost:9022', '/log', 'GET') |
Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame] | 1490 | \end{verbatim} |
Neal Norwitz | cd5c8c2 | 2003-01-25 21:29:41 +0000 | [diff] [blame] | 1491 | |
| 1492 | Sections which specify formatter configuration are typified by the following. |
| 1493 | |
| 1494 | \begin{verbatim} |
| 1495 | [formatter_form01] |
| 1496 | format=F1 %(asctime)s %(levelname)s %(message)s |
| 1497 | datefmt= |
| 1498 | \end{verbatim} |
| 1499 | |
| 1500 | The \code{format} entry is the overall format string, and the |
| 1501 | \code{datefmt} entry is the \function{strftime()}-compatible date/time format |
| 1502 | string. If empty, the package substitutes ISO8601 format date/times, which |
| 1503 | is almost equivalent to specifying the date format string "%Y-%m-%d %H:%M:%S". |
| 1504 | The ISO8601 format also specifies milliseconds, which are appended to the |
| 1505 | result of using the above format string, with a comma separator. An example |
| 1506 | time in ISO8601 format is \code{2003-01-23 00:29:50,411}. |