Skip Montanaro | 649698f | 2002-11-14 03:57:19 +0000 | [diff] [blame^] | 1 | \section{\module{logging} --- |
| 2 | Logging facility for Python} |
| 3 | |
| 4 | \declaremodule{standard}{logging} % standard library, in Python |
| 5 | |
| 6 | % These apply to all modules, and may be given more than once: |
| 7 | |
| 8 | \moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com} |
| 9 | \sectionauthor{Skip Montanaro}{skip@pobox.com} |
| 10 | |
| 11 | \modulesynopsis{Logging module for Python based on PEP 282.} |
| 12 | |
| 13 | |
| 14 | There is a need for a standard logging system in Python, as documented in |
| 15 | {}\pep{282} and enthusiastically endorsed by the Guido van Rossum in the |
| 16 | {}\citetitle[http://www.python.org/doc/essays/pepparade.html]{Parade of the |
| 17 | PEPs}. By a happy coincidence, the package described here was already in |
| 18 | development and fairly close in intent and design to the description in the |
| 19 | aforementioned PEP, borrowing as it did heavily from JSR-47 (now JDK 1.4's |
| 20 | java.util.logging package) and |
| 21 | {}\ulink{log4j}{http://jakarta.apache.org/log4j/}. This section page |
| 22 | describes it in more detail. |
| 23 | |
| 24 | This package owes its greatest debt to Apache |
| 25 | {}\ulink{log4j}{http://jakarta.apache.org/log4j/}. Due notice was also taken |
| 26 | of log4j's comprehensive |
| 27 | {}\ulink{critique}{http://jakarta.apache.org/log4j/docs/critique.html} of |
| 28 | JSR-47. This package bears a close resemblance to log4j, but is not a close |
| 29 | translation (as, for example, {}\ulink{log4p}{http://log4p.sourceforge.net/} |
| 30 | appears to be). |
| 31 | |
| 32 | |
| 33 | |
| 34 | \subsection{Functions} |
| 35 | |
| 36 | The \module{logging} module defines the following functions: |
| 37 | |
| 38 | |
| 39 | \begin{funcdesc}{getLevelName}{level} |
| 40 | |
| 41 | Return the textual representation of logging level \var{level}. |
| 42 | |
| 43 | If the level is one of the predefined levels (\var{CRITICAL}, \var{ERROR}, |
| 44 | {}\var{WARN}, \var{INFO}, \var{DEBUG}) then you get the corresponding |
| 45 | string. If you have associated levels with names using |
| 46 | {}\function{addLevelName} then the name you have associated with \var{level} |
| 47 | is returned. Otherwise, the string "Level \%s" \% level is returned. |
| 48 | |
| 49 | \end{funcdesc} |
| 50 | |
| 51 | |
| 52 | \begin{funcdesc}{addLevelName}{level, levelName} |
| 53 | |
| 54 | Associate \var{levelName} with \var{level}. This is used when converting |
| 55 | levels to text during message formatting. |
| 56 | |
| 57 | \end{funcdesc} |
| 58 | |
| 59 | |
| 60 | \begin{funcdesc}{setLoggerClass}{klass} |
| 61 | |
| 62 | Set the class to be used when instantiating a logger. The class should |
| 63 | define \code{__init__()} such that only a name argument is required, and the |
| 64 | {}\code{__init__()} should call Logger.__init__() |
| 65 | |
| 66 | \end{funcdesc} |
| 67 | |
| 68 | |
| 69 | \begin{funcdesc}{basicConfig}{} |
| 70 | |
| 71 | Do basic configuration for the logging system by creating a |
| 72 | {}\class{StreamHandler} with a default {}\class{Formatter} and adding it to |
| 73 | the root logger. |
| 74 | |
| 75 | \end{funcdesc} |
| 76 | |
| 77 | |
| 78 | \begin{funcdesc}{getLogger}{\optional{name=None}} |
| 79 | |
| 80 | Return a logger with the specified name, creating it if necessary. If no |
| 81 | name is specified, return the root logger. |
| 82 | |
| 83 | \end{funcdesc} |
| 84 | |
| 85 | |
| 86 | \begin{funcdesc}{critical}{msg, *args, **kwargs} |
| 87 | |
| 88 | Log a message with severity \code{CRITICAL} on the root logger. |
| 89 | |
| 90 | \end{funcdesc} |
| 91 | |
| 92 | |
| 93 | \begin{funcdesc}{error}{msg, *args, **kwargs} |
| 94 | |
| 95 | Log a message with severity \var{ERROR} on the root logger. |
| 96 | |
| 97 | \end{funcdesc} |
| 98 | |
| 99 | |
| 100 | \begin{funcdesc}{exception}{msg, *args} |
| 101 | |
| 102 | Log a message with severity \code{ERROR} on the root logger, |
| 103 | with exception information. |
| 104 | |
| 105 | \end{funcdesc} |
| 106 | |
| 107 | \begin{funcdesc}{warn}{msg, *args, **kwargs} |
| 108 | |
| 109 | Log a message with severity \code{WARN} on the root logger. |
| 110 | |
| 111 | \end{funcdesc} |
| 112 | |
| 113 | |
| 114 | \begin{funcdesc}{info}{msg, *args, **kwargs} |
| 115 | |
| 116 | Log a message with severity \code{INFO} on the root logger. |
| 117 | |
| 118 | \end{funcdesc} |
| 119 | |
| 120 | |
| 121 | \begin{funcdesc}{debug}{msg, *args, **kwargs} |
| 122 | |
| 123 | Log a message with severity \code{DEBUG} on the root logger. |
| 124 | |
| 125 | \end{funcdesc} |
| 126 | |
| 127 | |
| 128 | \begin{funcdesc}{disable}{level} |
| 129 | |
| 130 | Disable all logging calls less severe than \code{level}. |
| 131 | |
| 132 | \end{funcdesc} |
| 133 | |
| 134 | |
| 135 | \begin{funcdesc}{shutdown}{} |
| 136 | |
| 137 | Perform any cleanup actions in the logging system (e.g. flushing buffers). |
| 138 | Should be called at application exit. |
| 139 | |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | |
| 143 | |
| 144 | \subsection{Data} |
| 145 | |
| 146 | The \module{logging} module defines the following data objects: |
| 147 | |
| 148 | \begin{datadesc}{root} |
| 149 | |
| 150 | The default logger. |
| 151 | |
| 152 | \end{datadesc} |
| 153 | |
| 154 | |
| 155 | \begin{datadesc}{BASIC_FORMAT} |
| 156 | |
| 157 | The default message format. |
| 158 | |
| 159 | \end{datadesc} |
| 160 | |
| 161 | |
| 162 | \begin{datadesc}{CRITICAL} |
| 163 | |
| 164 | The \code{CRITICAL} level. |
| 165 | |
| 166 | \end{datadesc} |
| 167 | |
| 168 | |
| 169 | \begin{datadesc}{FATAL} |
| 170 | |
| 171 | The \code {FATAL} level. A synonym for \code{CRITICAL}. |
| 172 | |
| 173 | \end{datadesc} |
| 174 | |
| 175 | |
| 176 | \begin{datadesc}{WARN} |
| 177 | |
| 178 | The \code {WARN} level. |
| 179 | |
| 180 | \end{datadesc} |
| 181 | |
| 182 | |
| 183 | \begin{datadesc}{INFO} |
| 184 | |
| 185 | The \code{INFO} level. |
| 186 | |
| 187 | \end{datadesc} |
| 188 | |
| 189 | |
| 190 | \begin{datadesc}{DEBUG} |
| 191 | |
| 192 | The \code{DEBUG} level. |
| 193 | |
| 194 | \end{datadesc} |
| 195 | |
| 196 | |
| 197 | \begin{datadesc}{NOTSET} |
| 198 | |
| 199 | The \code{NOTSET} level. |
| 200 | |
| 201 | \end{datadesc} |
| 202 | |
| 203 | |
| 204 | \begin{datadesc}{raiseExceptions} |
| 205 | |
| 206 | Indicates whether exceptions during handling should be propagated. True by |
| 207 | default. |
| 208 | |
| 209 | \end{datadesc} |
| 210 | |
| 211 | |
| 212 | |
| 213 | \subsection{Classes} |
| 214 | |
| 215 | The \module{logging} module defines the following classes: |
| 216 | |
| 217 | |
| 218 | \begin{classdesc}{Formatter}{\optional{fmt=None\optional{, datefmt=None}}} |
| 219 | |
| 220 | Formatters need to know how a LogRecord is constructed. They are responsible |
| 221 | for converting a LogRecord to (usually) a string which can be interpreted by |
| 222 | either a human or an external system. The base Formatter allows a formatting |
| 223 | string to be specified. If none is supplied, the default value of |
| 224 | \code{"\%s(message)\e n"} is used. |
| 225 | |
| 226 | The Formatter can be initialized with a format string which makes use of |
| 227 | knowledge of the LogRecord attributes - e.g. the default value mentioned |
| 228 | above makes use of the fact that the user's message and arguments are pre- |
| 229 | formatted into a LogRecord's message attribute. Currently, the useful |
| 230 | attributes in a LogRecord are described by: |
| 231 | |
| 232 | \begin{description} |
| 233 | |
| 234 | \item[\%(name)s]{Name of the logger (logging channel)} |
| 235 | |
| 236 | \item[\%(levelno)s]{Numeric logging level for the message (DEBUG, INFO, |
| 237 | WARN, ERROR, CRITICAL)} |
| 238 | |
| 239 | \item[\%(levelname)s]{Text logging level for the message ("DEBUG", "INFO", |
| 240 | "WARN", "ERROR", "CRITICAL")} |
| 241 | |
| 242 | \item[\%(pathname)s]{Full pathname of the source file where the logging |
| 243 | call was issued (if available)} |
| 244 | |
| 245 | \item[\%(filename)s]{Filename portion of pathname} |
| 246 | |
| 247 | \item[\%(module)s]{Module (name portion of filename)} |
| 248 | |
| 249 | \item[\%(lineno)d]{Source line number where the logging call was issued |
| 250 | (if available)} |
| 251 | |
| 252 | \item[\%(created)f]{Time when the LogRecord was created (time.time() |
| 253 | return value)} |
| 254 | |
| 255 | \item[\%(asctime)s]{Textual time when the LogRecord was created} |
| 256 | |
| 257 | \item[\%(msecs)d]{Millisecond portion of the creation time} |
| 258 | |
| 259 | \item[\%(relativeCreated)d]{Time in milliseconds when the LogRecord was |
| 260 | created, relative to the time the logging module was loaded (typically at |
| 261 | application startup time)} |
| 262 | |
| 263 | \item[\%(thread)d]{Thread ID (if available)} |
| 264 | |
| 265 | \item[\%(message)s]{The result of record.getMessage(), computed just as the |
| 266 | record is emitted} |
| 267 | |
| 268 | \end{description} |
| 269 | |
| 270 | \end{classdesc} |
| 271 | |
| 272 | \begin{methoddesc}{format}{self, record} |
| 273 | |
| 274 | The record's attribute dictionary is used as the operand to a string |
| 275 | formatting operation which yields the returned string. Before formatting |
| 276 | the dictionary, a couple of preparatory steps are carried out. The message |
| 277 | attribute of the record is computed using \code{LogRecord.getMessage()}. If |
| 278 | the formatting string contains "\%(asctime)", \code{formatTime()} is called |
| 279 | to format the event time. If there is exception information, it is |
| 280 | formatted using \code{formatException()} and appended to the message. |
| 281 | |
| 282 | \end{methoddesc} |
| 283 | |
| 284 | \begin{methoddesc}{formatException}{self, ei} |
| 285 | |
| 286 | Format the specified exception information as a string. This default |
| 287 | implementation just uses \code{traceback.print_exception()} |
| 288 | |
| 289 | \end{methoddesc} |
| 290 | |
| 291 | \begin{methoddesc}{formatTime}{self, record\optional{, datefmt=None}} |
| 292 | |
| 293 | This method should be called from \code{format()} by a formatter which wants |
| 294 | to make use of a formatted time. This method can be overridden in formatters |
| 295 | to provide for any specific requirement, but the basic behaviour is as |
| 296 | follows: if datefmt (a string) is specified, it is used with time.strftime() |
| 297 | to format the creation time of the record. Otherwise, the ISO8601 format is |
| 298 | used. The resulting string is returned. This function uses a |
| 299 | user-configurable function to convert the creation time to a tuple. By |
| 300 | default, \code{time.localtime()} is used; to change this for a particular |
| 301 | formatter instance, set the 'converter' attribute to a function with the |
| 302 | same signature as \code{time.localtime()} or \code{time.gmtime()}. To change |
| 303 | it for all formatters, for example if you want all logging times to be shown |
| 304 | in GMT, set the 'converter' attribute in the \class{Formatter} class. |
| 305 | |
| 306 | \end{methoddesc} |
| 307 | |
| 308 | |
| 309 | \begin{classdesc}{Filterer}{} |
| 310 | |
| 311 | A base class for loggers and handlers which allows them to share common |
| 312 | code. |
| 313 | |
| 314 | \end{classdesc} |
| 315 | |
| 316 | \begin{methoddesc}{addFilter}{filter} |
| 317 | |
| 318 | Add the specified filter to this handler. |
| 319 | |
| 320 | \end{methoddesc} |
| 321 | |
| 322 | \begin{methoddesc}{filter}{self, record} |
| 323 | |
| 324 | Determine if a record is loggable by consulting all the filters. The default |
| 325 | is to allow the record to be logged; any filter can veto this and the record |
| 326 | is then dropped. Returns a boolean value. |
| 327 | |
| 328 | \end{methoddesc} |
| 329 | |
| 330 | \begin{methoddesc}{removeFilter}{filter} |
| 331 | |
| 332 | Remove the specified filter from this handler. |
| 333 | |
| 334 | \end{methoddesc} |
| 335 | |
| 336 | |
| 337 | \begin{classdesc}{BufferingFormatter}{\optional{linefmt=None}} |
| 338 | |
| 339 | A formatter suitable for formatting a number of records. Optionally specify |
| 340 | a formatter which will be used to format each individual record. |
| 341 | |
| 342 | \end{classdesc} |
| 343 | |
| 344 | |
| 345 | \begin{methoddesc}{format}{records} |
| 346 | |
| 347 | Format the specified records and return the result as a string. |
| 348 | |
| 349 | \end{methoddesc} |
| 350 | |
| 351 | \begin{methoddesc}{formatFooter}{records} |
| 352 | |
| 353 | Return the footer string for the specified records. |
| 354 | |
| 355 | \end{methoddesc} |
| 356 | |
| 357 | \begin{methoddesc}{formatHeader}{records} |
| 358 | |
| 359 | Return the header string for the specified records. |
| 360 | |
| 361 | \end{methoddesc} |
| 362 | |
| 363 | \begin{classdesc}{BufferingHandler}{capacity} |
| 364 | |
| 365 | A handler class which buffers logging records in memory. Whenever each |
| 366 | record is added to the buffer, a check is made to see if the buffer should |
| 367 | be flushed. If it should, then \code{flush()} is expected to do the needful. |
| 368 | The handler is initialized with the buffer size. |
| 369 | |
| 370 | \end{classdesc} |
| 371 | |
| 372 | \begin{methoddesc}{emit}{record} |
| 373 | |
| 374 | Append the record. If \code{shouldFlush()} tells us to, call \code{flush()} |
| 375 | to process the buffer. |
| 376 | |
| 377 | \end{methoddesc} |
| 378 | |
| 379 | \begin{methoddesc}{flush}{} |
| 380 | |
| 381 | Override to implement custom flushing behaviour. This version just zaps the |
| 382 | buffer to empty. |
| 383 | |
| 384 | \end{methoddesc} |
| 385 | |
| 386 | \begin{methoddesc}{shouldFlush}{record} |
| 387 | |
| 388 | Returns true if the buffer is up to capacity. This method can be overridden |
| 389 | to implement custom flushing strategies. |
| 390 | |
| 391 | \end{methoddesc} |
| 392 | |
| 393 | |
| 394 | \begin{classdesc}{DatagramHandler}{host,port} |
| 395 | |
| 396 | A handler class which writes logging records, in pickle format, to a |
| 397 | datagram socket. Note that the very simple wire protocol used means that |
| 398 | packet sizes are expected to be encodable within 16 bits (i.e. < 32767 |
| 399 | bytes). |
| 400 | |
| 401 | Initializes the handler with a specific \code{host} and \code{port}. |
| 402 | |
| 403 | \end{classdesc} |
| 404 | |
| 405 | \begin{methoddesc}{makeSocket}{} |
| 406 | |
| 407 | The factory method of SocketHandler is here overridden to create a UDP |
| 408 | socket (SOCK_DGRAM). |
| 409 | |
| 410 | \end{methoddesc} |
| 411 | |
| 412 | \begin{methoddesc}{send}{s} |
| 413 | |
| 414 | Send a pickled string to a socket. This function allows for partial sends |
| 415 | which can happen when the network is busy. |
| 416 | |
| 417 | \end{methoddesc} |
| 418 | |
| 419 | \begin{classdesc}{FileHandler}{filename\optional{, mode='a+'}} |
| 420 | |
| 421 | A handler class which writes formatted logging records to disk files. The |
| 422 | specified file is opened and used as the stream for logging. By default, |
| 423 | the file grows indefinitely. You can call \code{setRollover()} to allow the |
| 424 | file to rollover at a predetermined size. |
| 425 | |
| 426 | \end{classdesc} |
| 427 | |
| 428 | \begin{methoddesc}{close}{} |
| 429 | |
| 430 | Closes the stream. |
| 431 | |
| 432 | \end{methoddesc} |
| 433 | |
| 434 | \begin{methoddesc}{doRollover}{} |
| 435 | |
| 436 | Do a rollover, as described in \code{setRollover()}. |
| 437 | |
| 438 | \end{methoddesc} |
| 439 | |
| 440 | \begin{methoddesc}{emit}{record} |
| 441 | |
| 442 | Output the record to the file, catering for rollover as described |
| 443 | in \code{setRollover()}. |
| 444 | |
| 445 | \end{methoddesc} |
| 446 | |
| 447 | \begin{methoddesc}{setRollover}{maxBytes, backupCount} |
| 448 | |
| 449 | Set the rollover parameters so that rollover occurs whenever the current log |
| 450 | file is nearly \var{maxBytes} in length. If \var{backupCount} is >= 1, the |
| 451 | system will successively create new files with the same pathname as the base |
| 452 | file, but with extensions ".1", ".2" etc. appended to it. For example, with |
| 453 | a \var{backupCount} of 5 and a base file name of "app.log", you would get |
| 454 | "app.log", "app.log.1", "app.log.2", ... through to "app.log.5". When the |
| 455 | last file reaches its size limit, the logging reverts to "app.log" which is |
| 456 | truncated xto zero length. If maxBytes is zero, rollover never occurs. |
| 457 | |
| 458 | \end{methoddesc} |
| 459 | |
| 460 | \begin{classdesc}{Filter}{\optional{name=''}} |
| 461 | |
| 462 | The base filter class. \class{Logger} and \class{Handler} instances can |
| 463 | optionally use \class{Filter} instances to filter records as desired. The |
| 464 | base filter class only allows events which are below a certain point in the |
| 465 | logger hierarchy. For example, a filter initialized with "A.B" will allow |
| 466 | events logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not |
| 467 | "A.BB", "B.A.B" etc. If initialized with the empty string, all events are |
| 468 | passed. |
| 469 | |
| 470 | The instance is initialized with the name of the logger which, together with |
| 471 | its children, will have its events allowed through the filter. If no name is |
| 472 | specified, allow every event. |
| 473 | |
| 474 | \end{classdesc} |
| 475 | |
| 476 | \begin{methoddesc}{filter}{record} |
| 477 | |
| 478 | Is the specified record to be logged? Returns 0 for no, nonzero for yes. If |
| 479 | deemed appropriate, the record may be modified in-place. |
| 480 | |
| 481 | \end{methoddesc} |
| 482 | |
| 483 | |
| 484 | \begin{classdesc}{HTTPHandler}{host, url\optional{, method='GET'}} |
| 485 | |
| 486 | A class which sends records to a Web server, using either GET or POST |
| 487 | semantics. The instance is initialized with the \var{host}, the request |
| 488 | \var{url}, and the \var{method} ("GET" or "POST") |
| 489 | |
| 490 | \end{classdesc} |
| 491 | |
| 492 | \begin{methoddesc}{emit}{record} |
| 493 | |
| 494 | Send the \var{record} to the Web server as an URL-encoded dictionary |
| 495 | |
| 496 | \end{methoddesc} |
| 497 | |
| 498 | \begin{classdesc}{Handler}{\optional{level=0}} |
| 499 | |
| 500 | The base handler class. Acts as a placeholder which defines the Handler |
| 501 | interface. \class{Handler} instances can optionally use \class{Formatter} |
| 502 | instances to format records as desired. By default, no formatter is |
| 503 | specified; in this case, the 'raw' message as determined by record.message |
| 504 | is logged. Initializes the instance - basically setting the formatter to |
| 505 | None and the filter list to empty. |
| 506 | |
| 507 | XXX - what does the level do? |
| 508 | |
| 509 | \end{classdesc} |
| 510 | |
| 511 | \begin{methoddesc}{acquire}{} |
| 512 | |
| 513 | Acquire the I/O thread lock. |
| 514 | |
| 515 | \end{methoddesc} |
| 516 | |
| 517 | \begin{methoddesc}{close}{} |
| 518 | |
| 519 | Tidy up any resources used by the handler. This version does nothing and is |
| 520 | intended to be implemented by subclasses. |
| 521 | |
| 522 | \end{methoddesc} |
| 523 | |
| 524 | \begin{methoddesc}{createLock}{} |
| 525 | |
| 526 | Acquire a thread lock for serializing access to the underlying I/O. |
| 527 | |
| 528 | \end{methoddesc} |
| 529 | |
| 530 | \begin{methoddesc}{emit}{record} |
| 531 | |
| 532 | Do whatever it takes to actually log the specified logging record. This |
| 533 | version is intended to be implemented by subclasses and so raises a |
| 534 | \exception{NotImplementedError}. |
| 535 | |
| 536 | \end{methoddesc} |
| 537 | |
| 538 | \begin{methoddesc}{flush}{} |
| 539 | |
| 540 | Ensure all logging output has been flushed. This version does nothing and is |
| 541 | intended to be implemented by subclasses. |
| 542 | |
| 543 | \end{methoddesc} |
| 544 | |
| 545 | \begin{methoddesc}{format}{record} |
| 546 | |
| 547 | Do formatting for a \var{record} - if a formatter is set, use it. |
| 548 | Otherwise, use the default formatter for the module. |
| 549 | |
| 550 | \end{methoddesc} |
| 551 | |
| 552 | \begin{methoddesc}{handle}{record} |
| 553 | |
| 554 | Conditionally emit the specified logging \var{record}, depending on filters |
| 555 | which may have been added to the handler. Wrap the actual emission of the |
| 556 | record with acquisition/release of the I/O thread lock. |
| 557 | |
| 558 | \end{methoddesc} |
| 559 | |
| 560 | \begin{methoddesc}{handleError}{} |
| 561 | |
| 562 | This method should be called from handlers when an exception is encountered |
| 563 | during an \code{emit()} call. By default it does nothing, because by default |
| 564 | {}\var{raiseExceptions} is false, which means that exceptions get silently |
| 565 | ignored. This is what is mostly wanted for a logging system - most users |
| 566 | will not care about errors in the logging system, they are more interested |
| 567 | in application errors. You could, however, replace this with a custom |
| 568 | handler if you wish. |
| 569 | |
| 570 | XXX looks to me like raiseExceptions defaults to 1. |
| 571 | |
| 572 | \end{methoddesc} |
| 573 | |
| 574 | \begin{methoddesc}{release}{} |
| 575 | |
| 576 | Release the I/O thread lock. |
| 577 | |
| 578 | \end{methoddesc} |
| 579 | |
| 580 | \begin{methoddesc}{setFormatter}{formatter} |
| 581 | |
| 582 | Set the \var{formatter} for this handler. |
| 583 | |
| 584 | \end{methoddesc} |
| 585 | |
| 586 | \begin{methoddesc}{setLevel}{level} |
| 587 | |
| 588 | Set the logging \var{level} of this handler. |
| 589 | |
| 590 | \end{methoddesc} |
| 591 | |
| 592 | \begin{classdesc}{LogRecord}{name,lvl,pathname,lineno,msg,args,exc_info} |
| 593 | |
| 594 | \class{LogRecord} instances are created every time something is logged. They |
| 595 | contain all the information pertinent to the event being logged. The main |
| 596 | information passed in is in msg and args, which are combined using |
| 597 | \code{str(msg) \% args} to create the message field of the record. The |
| 598 | record also includes information such as when the record was created, the |
| 599 | source line where the logging call was made, and any exception information |
| 600 | to be logged. |
| 601 | |
| 602 | \end{classdesc} |
| 603 | |
| 604 | \begin{methoddesc}{getMessage}{} |
| 605 | |
| 606 | Return the message for this LogRecord, merging any user-supplied arguments |
| 607 | with the message. |
| 608 | |
| 609 | \end{methoddesc} |
| 610 | |
| 611 | \begin{classdesc}{Logger}{name\optional{, level=0}} |
| 612 | |
| 613 | Instances of the \class{Logger} class represent a single logging channel. A |
| 614 | "logging channel" indicates an area of an application. Exactly how an "area" |
| 615 | is defined is up to the application developer. Since an application can have |
| 616 | any number of areas, logging channels are identified by a unique |
| 617 | string. Application areas can be nested (e.g. an area of "input processing" |
| 618 | might include sub-areas "read CSV files", "read XLS files" and "read |
| 619 | Gnumeric files"). To cater for this natural nesting, channel names are |
| 620 | organized into a namespace hierarchy where levels are separated by periods, |
| 621 | much like the Java or Python package namespace. So in the instance given |
| 622 | above, channel names might be "input" for the upper level, and "input.csv", |
| 623 | "input.xls" and "input.gnu" for the sub-levels. There is no arbitrary limit |
| 624 | to the depth of nesting. |
| 625 | |
| 626 | The logger is initialized with a \var{name} and an optional \var{level}. |
| 627 | |
| 628 | \end{classdesc} |
| 629 | |
| 630 | \begin{methoddesc}{_log}{lvl, msg, args\optional{, exc_info=None}} |
| 631 | |
| 632 | Low-level logging routine which creates a \class{LogRecord} and then calls |
| 633 | all the handlers of this logger to handle the record. |
| 634 | |
| 635 | \end{methoddesc} |
| 636 | |
| 637 | \begin{methoddesc}{addHandler}{hdlr} |
| 638 | |
| 639 | Add the specified handler to this logger. |
| 640 | |
| 641 | \end{methoddesc} |
| 642 | |
| 643 | \begin{methoddesc}{callHandlers}{record} |
| 644 | |
| 645 | Loop through all handlers for this logger and its parents in the logger |
| 646 | hierarchy. If no handler was found, output a one-off error message to |
| 647 | sys.stderr. Stop searching up the hierarchy whenever a logger with the |
| 648 | "propagate" attribute set to zero is found - that will be the last logger |
| 649 | whose handlers are called. |
| 650 | |
| 651 | \end{methoddesc} |
| 652 | |
| 653 | \begin{methoddesc}{critical}{msg, *args, **kwargs} |
| 654 | |
| 655 | Log \code{msg \% args} with severity \code{CRITICAL}. To pass exception |
| 656 | information, use the keyword argument \var{exc_info} with a true value, |
| 657 | e.g., \code{logger.critical("Houston, we have a \%s", "major disaster", |
| 658 | exc_info=1)}. |
| 659 | |
| 660 | \end{methoddesc} |
| 661 | |
| 662 | \begin{methoddesc}{fatal}{msg, *args, **kwargs} |
| 663 | |
| 664 | Synonym for \method{critical}. |
| 665 | |
| 666 | \end{methoddesc} |
| 667 | |
| 668 | \begin{methoddesc}{debug}{msg, *args, **kwargs} |
| 669 | |
| 670 | Log \code{msg \% args} with severity \code{DEBUG}. To pass exception |
| 671 | information, use the keyword argument exc_info with a true value, e.g., |
| 672 | \code{logger.debug("Houston, we have a \%s", "thorny problem", exc_info=1)}. |
| 673 | |
| 674 | \end{methoddesc} |
| 675 | |
| 676 | \begin{methoddesc}{error}{msg, *args, **kwargs} |
| 677 | |
| 678 | Log \code{msg \% args} with severity \code{ERROR}. To pass exception |
| 679 | information, use the keyword argument exc_info with a true value, e.g., |
| 680 | \code{logger.error("Houston, we have a \%s", "major problem", exc_info=1)} |
| 681 | |
| 682 | \end{methoddesc} |
| 683 | |
| 684 | \begin{methoddesc}{exception}{msg, *args} |
| 685 | |
| 686 | Convenience method for logging an \code{ERROR} with exception information. |
| 687 | |
| 688 | \end{methoddesc} |
| 689 | |
| 690 | |
| 691 | \begin{methoddesc}{findCaller}{} |
| 692 | |
| 693 | Find the stack frame of the caller so that we can note the source file name |
| 694 | and line number. |
| 695 | |
| 696 | \end{methoddesc} |
| 697 | |
| 698 | \begin{methoddesc}{getEffectiveLevel}{} |
| 699 | |
| 700 | Loop through this logger and its parents in the logger hierarchy, looking |
| 701 | for a non-zero logging level. Return the first one found. |
| 702 | |
| 703 | \end{methoddesc} |
| 704 | |
| 705 | \begin{methoddesc}{handle}{record} |
| 706 | |
| 707 | Call the handlers for the specified \var{record}. This method is used for |
| 708 | unpickled records received from a socket, as well as those created |
| 709 | locally. Logger-level filtering is applied. |
| 710 | |
| 711 | \end{methoddesc} |
| 712 | |
| 713 | \begin{methoddesc}{info}{msg, *args, **kwargs} |
| 714 | |
| 715 | Log \code{msg \% args} with severity \code{INFO}. To pass exception |
| 716 | information, use the keyword argument exc_info with a true value, e.g., |
| 717 | \code{logger.info("Houston, we have a \%s", "interesting problem", |
| 718 | exc_info=1)} |
| 719 | |
| 720 | \end{methoddesc} |
| 721 | |
| 722 | \begin{methoddesc}{isEnabledFor}{lvl} |
| 723 | |
| 724 | Is this logger enabled for level \var{lvl}? |
| 725 | |
| 726 | \end{methoddesc} |
| 727 | |
| 728 | \begin{methoddesc}{log}{lvl, msg, *args, **kwargs} |
| 729 | |
| 730 | Log \code{msg \% args} with the severity \var{lvl}. To pass exception |
| 731 | information, use the keyword argument \var{exc_info} with a true value, |
| 732 | e.g., \code{logger.log(lvl, "We have a \%s", "mysterious problem", |
| 733 | exc_info=1)} |
| 734 | |
| 735 | \end{methoddesc} |
| 736 | |
| 737 | \begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info} |
| 738 | |
| 739 | A factory method which can be overridden in subclasses to create specialized |
| 740 | \code{LogRecord} instances. |
| 741 | |
| 742 | \end{methoddesc} |
| 743 | |
| 744 | \begin{methoddesc}{removeHandler}{hdlr} |
| 745 | |
| 746 | Remove the specified handler from this logger. |
| 747 | |
| 748 | \end{methoddesc} |
| 749 | |
| 750 | \begin{methoddesc}{setLevel}{level} |
| 751 | |
| 752 | Set the logging \var{level} of this logger. |
| 753 | |
| 754 | \end{methoddesc} |
| 755 | |
| 756 | \begin{methoddesc}{warn}{msg, *args, **kwargs} |
| 757 | |
| 758 | Log \code{msg \% args} with severity \code{WARN}. To pass exception |
| 759 | information, use the keyword argument exc_info with a true value, e.g., |
| 760 | \code{logger.warn("Houston, we have a \%s", "bit of a problem", exc_info=1)} |
| 761 | |
| 762 | \end{methoddesc} |
| 763 | |
| 764 | |
| 765 | \begin{classdesc}{Manager}{root} |
| 766 | |
| 767 | There is (under normal circumstances) just one \code{Manager} instance, |
| 768 | which holds the hierarchy of loggers. |
| 769 | |
| 770 | The manager is initialized with the \var{root} node of the logger hierarchy. |
| 771 | |
| 772 | \end{classdesc} |
| 773 | |
| 774 | \begin{methoddesc}{_fixupChildren}{ph, logger} |
| 775 | |
| 776 | Ensure that children of the placeholder \var{ph} are connected to the |
| 777 | specified \code{logger}. |
| 778 | |
| 779 | \end{methoddesc} |
| 780 | |
| 781 | \begin{methoddesc}{_fixupParents}{logger} |
| 782 | |
| 783 | Ensure that there are either loggers or placeholders all the way from the |
| 784 | specified \var{logger} to the root of the logger hierarchy. |
| 785 | |
| 786 | \end{methoddesc} |
| 787 | |
| 788 | \begin{methoddesc}{getLogger}{name} |
| 789 | |
| 790 | Get a logger with the specified \var{name} (channel name), creating it if it |
| 791 | doesn't yet exist. If a PlaceHolder existed for the specified name (i.e. the |
| 792 | logger didn't exist but a child of it did), replace it with the created |
| 793 | logger and fix up the parent/child references which pointed to the |
| 794 | placeholder to now point to the logger. |
| 795 | |
| 796 | \end{methoddesc} |
| 797 | |
| 798 | \begin{classdesc}{MemoryHandler}{capacity\optional{, |
| 799 | flushLevel=40\optional{, target=None}}} |
| 800 | |
| 801 | A handler class which buffers logging records in memory, periodically |
| 802 | flushing them to a target handler. Flushing occurs whenever the buffer is |
| 803 | full, or when an event of a certain severity or greater is seen. |
| 804 | |
| 805 | The handler is initialized with the buffer size (\var{capacity}), the level |
| 806 | at which flushing should occur (\var{flushLevel}) and an optional |
| 807 | {}\var{target}. Note that without a target being set either here or via |
| 808 | \code{setTarget()}, a \class{MemoryHandler} is no use to anyone! |
| 809 | |
| 810 | \end{classdesc} |
| 811 | |
| 812 | \begin{methoddesc}{close}{} |
| 813 | |
| 814 | Flush, set the target to None and lose the buffer. |
| 815 | |
| 816 | \end{methoddesc} |
| 817 | |
| 818 | \begin{methoddesc}{flush}{} |
| 819 | |
| 820 | For a \class{MemoryHandler}, flushing means just sending the buffered |
| 821 | records to the target, if there is one. Override if you want different |
| 822 | behavior. |
| 823 | |
| 824 | \end{methoddesc} |
| 825 | |
| 826 | \begin{methoddesc}{setTarget}{target} |
| 827 | |
| 828 | Set the \var{target} handler for this handler. |
| 829 | |
| 830 | \end{methoddesc} |
| 831 | |
| 832 | \begin{methoddesc}{shouldFlush}{record} |
| 833 | |
| 834 | Check for buffer full or a \var{record} at the flushLevel or higher. |
| 835 | |
| 836 | \end{methoddesc} |
| 837 | |
| 838 | |
| 839 | \begin{classdesc}{NTEventLogHandler}{appname\optional{, |
| 840 | dllname=None\optional{, logtype='Application'}}} |
| 841 | |
| 842 | A handler class which sends events to the NT Event Log. Adds a registry |
| 843 | entry for the specified application name. If no \var{dllname} is provided, |
| 844 | \code{win32service.pyd} (which contains some basic message placeholders) is |
| 845 | used. Note that use of these placeholders will make your event logs big, as |
| 846 | the entire message source is held in the log. If you want slimmer logs, you |
| 847 | have to pass in the name of your own DLL which contains the message |
| 848 | definitions you want to use in the event log. |
| 849 | |
| 850 | XXX what is \var{logtype}? |
| 851 | |
| 852 | \end{classdesc} |
| 853 | |
| 854 | \begin{methoddesc}{close}{} |
| 855 | |
| 856 | You can remove the application name from the registry as a source of event |
| 857 | log entries. However, if you do this, you will not be able to see the events |
| 858 | as you intended in the Event Log Viewer - it needs to be able to access the |
| 859 | registry to get the DLL name. |
| 860 | |
| 861 | \end{methoddesc} |
| 862 | |
| 863 | \begin{methoddesc}{emit}{record} |
| 864 | |
| 865 | Determine the message ID, event category and event type. Then log the |
| 866 | \var{record} in the NT event log. |
| 867 | |
| 868 | \end{methoddesc} |
| 869 | |
| 870 | \begin{methoddesc}{getEventCategory}{record} |
| 871 | |
| 872 | Return the event category for the \var{record}. Override this if you want |
| 873 | to specify your own categories. This version returns 0. |
| 874 | |
| 875 | \end{methoddesc} |
| 876 | |
| 877 | \begin{methoddesc}{getEventType}{record} |
| 878 | |
| 879 | Return the event type for the \var{record}. Override this if you want to |
| 880 | specify your own types. This version does a mapping using the handler's |
| 881 | typemap attribute, which is set up in the constructor to a dictionary which |
| 882 | contains mappings for \var{DEBUG}, \var{INFO}, \var{WARN}, \var{ERROR} and |
| 883 | {}\var{CRITICAL}. If you are using your own levels you will either need to |
| 884 | override this method or place a suitable dictionary in the handler's typemap |
| 885 | attribute. |
| 886 | |
| 887 | \end{methoddesc} |
| 888 | |
| 889 | \begin{methoddesc}{getMessageID}{record} |
| 890 | |
| 891 | Return the message ID for the event \var{record}. If you are using your own |
| 892 | messages, you could do this by having the msg passed to the logger being an |
| 893 | ID rather than a formatting string. Then, in here, you could use a |
| 894 | dictionary lookup to get the message ID. This version returns 1, which is |
| 895 | the base message ID in \code{win32service.pyd}. |
| 896 | |
| 897 | \end{methoddesc} |
| 898 | |
| 899 | |
| 900 | \begin{classdesc}PlaceHolder{logger} |
| 901 | |
| 902 | \class{PlaceHolder} instances are used in the \class{Manager} logger |
| 903 | hierarchy to take the place of nodes for which no loggers have been defined |
| 904 | |
| 905 | Initialize with the specified \var{logger} being a child of this |
| 906 | \class{PlaceHolder}. |
| 907 | |
| 908 | \end{classdesc} |
| 909 | |
| 910 | \begin{methoddesc}{append}{logger} |
| 911 | |
| 912 | Add the specified \var{logger} as a child of this placeholder. |
| 913 | |
| 914 | \end{methoddesc} |
| 915 | |
| 916 | \begin{classdesc}RootLogger{level} |
| 917 | |
| 918 | A root logger is not that different to any other logger, except that it must |
| 919 | have a logging \var{level} and there is only one instance of it in the |
| 920 | hierarchy. |
| 921 | |
| 922 | \end{classdesc} |
| 923 | |
| 924 | |
| 925 | \begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddr, subject} |
| 926 | |
| 927 | A handler class which sends an SMTP email for each logging event. |
| 928 | |
| 929 | The instance is initialized with the from (\var{fromaddr}) and to |
| 930 | (\var{toaddr}) addresses and \var{subject} line of the email. To specify a |
| 931 | non-standard SMTP port, use the (host, port) tuple format for the |
| 932 | \var{mailhost} argument. |
| 933 | |
| 934 | \end{classdesc} |
| 935 | |
| 936 | |
| 937 | \begin{methoddesc}{emit}{record} |
| 938 | |
| 939 | Format the \var{record} and send it to the specified addressees. |
| 940 | |
| 941 | \end{methoddesc} |
| 942 | |
| 943 | \begin{methoddesc}{getSubject}{record} |
| 944 | |
| 945 | If you want to specify a subject line which is \var{record}-dependent, |
| 946 | override this method. |
| 947 | |
| 948 | \end{methoddesc} |
| 949 | |
| 950 | \begin{classdesc}{SocketHandler}{host, port} |
| 951 | |
| 952 | A handler class which writes pickled logging records to a streaming |
| 953 | socket. The socket is kept open across logging calls. If the peer resets |
| 954 | it, an attempt is made to reconnect on the next call. Note that the very |
| 955 | simple wire protocol used means that packet sizes are expected to be |
| 956 | encodable within 16 bits (i.e. < 32767 bytes). |
| 957 | |
| 958 | The handler is initialized with a specific \var{host} address and |
| 959 | {}\var{port}. The attribute \var{closeOnError} is set to 1 - which means |
| 960 | that if a socket error occurs, the socket is silently closed and then |
| 961 | reopened on the next logging call. |
| 962 | |
| 963 | \end{classdesc} |
| 964 | |
| 965 | \begin{methoddesc}{close}{} |
| 966 | |
| 967 | Closes the socket. |
| 968 | |
| 969 | \end{methoddesc} |
| 970 | |
| 971 | \begin{methoddesc}{emit}{record} |
| 972 | |
| 973 | Pickles the \var{record} and writes it to the socket in binary format. If |
| 974 | there is an error with the socket, silently drop the packet. If there was a |
| 975 | problem with the socket, re-establishes the socket. |
| 976 | |
| 977 | \end{methoddesc} |
| 978 | |
| 979 | \begin{methoddesc}{handleError}{} |
| 980 | |
| 981 | An error has occurred during logging. Most likely cause - connection lost. |
| 982 | Close the socket so that we can retry on the next event. |
| 983 | |
| 984 | \end{methoddesc} |
| 985 | |
| 986 | \begin{methoddesc}{makePickle}{record} |
| 987 | |
| 988 | Pickles the \var{record} in binary format with a length prefix, and returns |
| 989 | it ready for transmission across the socket. |
| 990 | |
| 991 | \end{methoddesc} |
| 992 | |
| 993 | \begin{methoddesc}{makeSocket}{} |
| 994 | |
| 995 | A factory method which allows subclasses to define the precise type of |
| 996 | socket they want. |
| 997 | |
| 998 | \end{methoddesc} |
| 999 | |
| 1000 | \begin{methoddesc}{send}{s} |
| 1001 | |
| 1002 | Send a pickled string (\var{s}) to the socket. This function allows for |
| 1003 | partial sends which can happen when the network is busy. |
| 1004 | |
| 1005 | \end{methoddesc} |
| 1006 | |
| 1007 | \begin{classdesc}{StreamHandler}{\optional{strm=None}} |
| 1008 | |
| 1009 | A handler class which writes logging records, appropriately formatted, to a |
| 1010 | stream. Note that this class does not close the stream, as \var{sys.stdout} |
| 1011 | or \var{sys.stderr} may be used. |
| 1012 | |
| 1013 | If \var{strm} is not specified, \var{sys.stderr} is used. |
| 1014 | |
| 1015 | \end{classdesc} |
| 1016 | |
| 1017 | \begin{methoddesc}{emit}{record} |
| 1018 | |
| 1019 | If a formatter is specified, it is used to format the \var{record}. The |
| 1020 | record is then written to the stream with a trailing newline (N.B. this may |
| 1021 | be removed depending on feedback). If exception information is present, it |
| 1022 | is formatted using \var{traceback.print_exception} and appended to the |
| 1023 | stream. |
| 1024 | |
| 1025 | \end{methoddesc} |
| 1026 | |
| 1027 | \begin{methoddesc}{flush}{} |
| 1028 | |
| 1029 | Flushes the stream. |
| 1030 | |
| 1031 | \end{methoddesc} |
| 1032 | |
| 1033 | \begin{classdesc}{SysLogHandler}{\optional{address=('localhost', |
| 1034 | 514)\optional{, facility=1}}} |
| 1035 | |
| 1036 | A handler class which sends formatted logging records to a syslog |
| 1037 | server. Based on Sam Rushing's |
| 1038 | \ulink{http://www.nightmare.com/squirl/python-ext/misc/syslog.py}{syslog |
| 1039 | module}. Contributed by Nicolas Untz (after which minor refactoring changes |
| 1040 | have been made). |
| 1041 | |
| 1042 | If \var{address} is specified as a string, UNIX socket is used. If |
| 1043 | \var{facility} is not specified, \code{LOG_USER} is used. |
| 1044 | |
| 1045 | \end{classdesc} |
| 1046 | |
| 1047 | \begin{methoddesc}{close}{} |
| 1048 | |
| 1049 | Closes the socket. |
| 1050 | |
| 1051 | \end{methoddesc} |
| 1052 | |
| 1053 | \begin{methoddesc}{emit}{record} |
| 1054 | |
| 1055 | The \var{record} is formatted, and then sent to the syslog server. If |
| 1056 | exception information is present, it is not sent to the server. |
| 1057 | |
| 1058 | \end{methoddesc} |
| 1059 | |
| 1060 | \begin{methoddesc}{encodePriority}{facility, priority} |
| 1061 | |
| 1062 | Encode the \var{facility} and \var{priority}. You can pass in strings or |
| 1063 | integers - if strings are passed, the \var{facility_names} and |
| 1064 | \var{priority_names} mapping dictionaries are used to convert them to |
| 1065 | integers. |
| 1066 | |
| 1067 | \end{methoddesc} |
| 1068 | |
| 1069 | |
| 1070 | \subsection{Examples \label{logging-example}} |
| 1071 | |
| 1072 | Using the package doesn't get much simpler. It is packaged as a Python |
| 1073 | package. You just need to \code{import logging} and you're ready to |
| 1074 | go. Minimal example: |
| 1075 | |
| 1076 | \begin{verbatim} |
| 1077 | # -- app.py -- |
| 1078 | import logging |
| 1079 | |
| 1080 | logging.info("Starting...") |
| 1081 | logging.warn("Nothing to do!") |
| 1082 | logging.info("Done...") |
| 1083 | \end{verbatim} |
| 1084 | |
| 1085 | When you run \code{app.py}, the results are: |
| 1086 | |
| 1087 | \begin{verbatim} |
| 1088 | 2002-03-15 01:09:10,440 root INFO - Starting... |
| 1089 | 2002-03-15 01:09:10,440 root WARN - Nothing to do! |
| 1090 | 2002-03-15 01:09:10,440 root INFO - Done... |
| 1091 | \end{verbatim} |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | Here's a |
| 1096 | slightly more involved example; if you've just looked at \pep{282} you will |
| 1097 | probably get a feeling of dejà vu. (This is intentional.) |
| 1098 | |
| 1099 | \begin{verbatim} |
| 1100 | # -- mymodule.py -- |
| 1101 | import logging |
| 1102 | log = logging.getLogger("MyModule") |
| 1103 | |
| 1104 | def doIt(): |
| 1105 | log.debug("doin' stuff") |
| 1106 | #do stuff...but suppose an error occurs? |
| 1107 | raise TypeError, "bogus type error for testing" |
| 1108 | \end{verbatim} |
| 1109 | |
| 1110 | \begin{verbatim} |
| 1111 | # -- myapp.py -- |
| 1112 | import logging, mymodule |
| 1113 | |
| 1114 | logging.basicConfig() # basic configuration - console output |
| 1115 | |
| 1116 | log = logging.getLogger("MyApp") |
| 1117 | |
| 1118 | log.info("start my app") |
| 1119 | try: |
| 1120 | mymodule.doIt() |
| 1121 | except Exception, e: |
| 1122 | log.exception("There was a problem doin' stuff.") |
| 1123 | log.info("end my app") |
| 1124 | \end{verbatim} |
| 1125 | |
| 1126 | When you run \code{myapp.py}, the results are: |
| 1127 | |
| 1128 | \begin{verbatim} |
| 1129 | 2002-03-14 23:40:49,299 MyApp INFO - start my app |
| 1130 | 2002-03-14 23:40:49,299 MyModule DEBUG - doin' stuff |
| 1131 | 2002-03-14 23:40:49,299 MyApp ERROR - There was a problem doin' stuff. |
| 1132 | Traceback (innermost last): |
| 1133 | File "myapp.py", line 9, in ? |
| 1134 | mymodule.doIt() |
| 1135 | File "mymodule.py", line 7, in doIt |
| 1136 | raise TypeError, "bogus type error for testing" |
| 1137 | TypeError: bogus type error for testing |
| 1138 | 2002-03-14 23:40:49,409 MyApp INFO - end my app |
| 1139 | \end{verbatim} |