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