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