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