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