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