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